ByteArray.st
changeset 4978 0d75a92f1e56
parent 4969 bd9f02a6a74f
child 4983 07e6aab900d9
equal deleted inserted replaced
4977:17a819360ad5 4978:0d75a92f1e56
  2031 
  2031 
  2032     if ((__qClass(self) == @global(ByteArray)) 
  2032     if ((__qClass(self) == @global(ByteArray)) 
  2033      && (__qClass(aByteArray) == @global(ByteArray))
  2033      && (__qClass(aByteArray) == @global(ByteArray))
  2034      && __isSmallInteger(nBitsPerPixel)
  2034      && __isSmallInteger(nBitsPerPixel)
  2035      && __bothSmallInteger(height, width)) {
  2035      && __bothSmallInteger(height, width)) {
  2036 	if ((aMapByteArray != nil)
  2036         if ((aMapByteArray != nil)
  2037 	 && (__Class(aMapByteArray) == @global(ByteArray))) {
  2037          && (__Class(aMapByteArray) == @global(ByteArray))) {
  2038 	    map = __ByteArrayInstPtr(aMapByteArray)->ba_element;
  2038             map = __ByteArrayInstPtr(aMapByteArray)->ba_element;
  2039 	} else {
  2039         } else {
  2040 	    map = (unsigned char *)0;
  2040             map = (unsigned char *)0;
  2041 	}
  2041         }
  2042 
  2042 
  2043 	bitsPerPixel = __intVal(nBitsPerPixel);
  2043         bitsPerPixel = __intVal(nBitsPerPixel);
  2044 	w = __intVal(width);
  2044         w = __intVal(width);
  2045 	h = __intVal(height);
  2045         h = __intVal(height);
  2046 	src = __ByteArrayInstPtr(self)->ba_element;
  2046         src = __ByteArrayInstPtr(self)->ba_element;
  2047 	dst = __ByteArrayInstPtr(aByteArray)->ba_element;
  2047         dst = __ByteArrayInstPtr(aByteArray)->ba_element;
  2048 	switch (bitsPerPixel) {
  2048         switch (bitsPerPixel) {
  2049 	    case 1:
  2049             case 1:
  2050 		mask = 0x01;
  2050                 mask = 0x01;
  2051 		break;
  2051                 break;
  2052 	    case 2:
  2052             case 2:
  2053 		mask = 0x03;
  2053                 mask = 0x03;
  2054 		break;
  2054                 break;
  2055 	    case 4:
  2055             case 4:
  2056 		mask = 0x0F;
  2056                 mask = 0x0F;
  2057 		break;
  2057                 break;
  2058 	    case 8:
  2058             case 8:
  2059 		mask = 0xFF;
  2059                 mask = 0xFF;
  2060 		break;
  2060                 break;
  2061 	    default:
  2061             default:
  2062 		printf("invalid depth in expandPixels\n");
  2062                 printf("invalid depth in expandPixels\n");
  2063 		goto fail;
  2063                 goto fail;
  2064 	}
  2064         }
  2065 	ncells = mask + 1;
  2065         ncells = mask + 1;
  2066 	if (map) {
  2066         if (map) {
  2067 	    /*
  2067             /*
  2068 	     * if a map is present, it must have the correct size
  2068              * if a map is present, it must have the correct size
  2069 	     * (i.e. 2 raisedTo:nBitsPerPixel)
  2069              * (i.e. 2 raisedTo:nBitsPerPixel)
  2070 	     */
  2070              */
  2071 	    if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) {
  2071             if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) {
  2072 		printf("invalid map in expandPixels\n");
  2072                 printf("invalid map in expandPixels\n");
  2073 		goto fail;
  2073                 goto fail;
  2074 	    }
  2074             }
  2075 	}
  2075         }
  2076 
  2076 
  2077 	bytesPerRow = (w * bitsPerPixel + 7) / 8;
  2077         bytesPerRow = (w * bitsPerPixel + 7) / 8;
  2078 	shift0 = 8 - bitsPerPixel;
  2078         shift0 = 8 - bitsPerPixel;
  2079 	srcBytes = bytesPerRow * h;
  2079         srcBytes = bytesPerRow * h;
  2080 	dstBytes = w * h;
  2080         dstBytes = w * h;
  2081 
  2081 
  2082 	if ((__byteArraySize(self) >= srcBytes)
  2082         if ((__byteArraySize(self) >= srcBytes)
  2083 	 && (__byteArraySize(aByteArray) >= dstBytes)) {
  2083          && (__byteArraySize(aByteArray) >= dstBytes)) {
  2084 	    for (hrun=h; hrun; hrun--) {
  2084             for (hrun=h; hrun; hrun--) {
  2085 		srcNext = src + bytesPerRow;
  2085                 srcNext = src + bytesPerRow;
  2086 		shift = shift0;
  2086                 shift = shift0;
  2087 		if (map) {
  2087                 if (map) {
  2088 		    if (shift0 == 0) {
  2088                     if (shift0 == 0) {
  2089 			/* translate only */
  2089                         /* translate only */
  2090 			for (wrun=w; wrun; wrun--) {
  2090                         for (wrun=w; wrun; wrun--) {
  2091 			    bits = *src++;
  2091                             bits = *src++;
  2092 			    *dst++ = map[bits];
  2092                             *dst++ = map[bits];
  2093 			}
  2093                         }
  2094 		    } else {
  2094                     } else {
  2095 			for (wrun=w; wrun; wrun--) {
  2095                         for (wrun=w; wrun; wrun--) {
  2096 			    if (shift == shift0) {
  2096                             if (shift == shift0) {
  2097 				bits = *src++;
  2097                                 bits = *src++;
  2098 			    }
  2098                             }
  2099 			    *dst++ = map[(bits >> shift) & mask];
  2099                             *dst++ = map[(bits >> shift) & mask];
  2100 			    shift -= bitsPerPixel;
  2100                             shift -= bitsPerPixel;
  2101 			    if (shift < 0) {
  2101                             if (shift < 0) {
  2102 				shift = shift0;
  2102                                 shift = shift0;
  2103 			    }
  2103                             }
  2104 			}
  2104                         }
  2105 		    }
  2105                     }
  2106 		} else {
  2106                 } else {
  2107 		    for (wrun=w; wrun; wrun--) {
  2107                     for (wrun=w; wrun; wrun--) {
  2108 			if (shift == shift0) {
  2108                         if (shift == shift0) {
  2109 			    bits = *src++;
  2109                             bits = *src++;
  2110 			}
  2110                         }
  2111 			*dst++ = (bits >> shift) & mask;
  2111                         *dst++ = (bits >> shift) & mask;
  2112 			shift -= bitsPerPixel;
  2112                         shift -= bitsPerPixel;
  2113 			if (shift < 0) {
  2113                         if (shift < 0) {
  2114 			    shift = shift0;
  2114                             shift = shift0;
  2115 			}
  2115                         }
  2116 		    }
  2116                     }
  2117 		}
  2117                 }
  2118 		src = srcNext;
  2118                 src = srcNext;
  2119 	    }
  2119             }
  2120 	    RETURN ( self );
  2120             RETURN ( self );
  2121 	}
  2121         }
  2122 	printf("buffer size: self:%d expect at least:%d\n", 
  2122         printf("buffer size: self:%d expect at least:%d\n", 
  2123 		__byteArraySize(self), srcBytes);
  2123                 __byteArraySize(self), srcBytes);
  2124 	printf("buffer size: arg:%d expect at least:%d\n", 
  2124         printf("buffer size: arg:%d expect at least:%d\n", 
  2125 		__byteArraySize(aByteArray), dstBytes);
  2125                 __byteArraySize(aByteArray), dstBytes);
  2126     }
  2126     }
  2127     printf("invalid args\n");
  2127     printf("invalid args\n");
  2128 
  2128 
  2129 fail: ;
  2129 fail: ;
  2130 %}.
  2130 %}.
  2135     "
  2135     "
  2136     "
  2136     "
  2137      |inBits outBits|
  2137      |inBits outBits|
  2138 
  2138 
  2139      inBits := #[2r11110000 
  2139      inBits := #[2r11110000 
  2140 		 2r11001100 
  2140                  2r11001100 
  2141 		 2r01010101 
  2141                  2r01010101 
  2142 		 2r00001111].
  2142                  2r00001111].
  2143      outBits := ByteArray new:(8*4).
  2143      outBits := ByteArray new:(8*4).
  2144      inBits expandPixels:1 width:8 height:4
  2144      inBits expandPixels:1 width:8 height:4
  2145 		    into:outBits mapping:nil.
  2145                     into:outBits mapping:nil.
  2146      outBits inspect
  2146      outBits inspect
  2147     "
  2147     "
  2148 
  2148 
  2149     "Example2:
  2149     "Example2:
  2150      expand bit-array into a byteArray, translating 0-bits to 99,
  2150      expand bit-array into a byteArray, translating 0-bits to 99,
  2154      |inBits outBits|
  2154      |inBits outBits|
  2155 
  2155 
  2156      inBits := #[2r11110000 2r11001100].
  2156      inBits := #[2r11110000 2r11001100].
  2157      outBits := ByteArray new:16.
  2157      outBits := ByteArray new:16.
  2158      inBits expandPixels:1 width:16 height:1 
  2158      inBits expandPixels:1 width:16 height:1 
  2159 		    into:outBits mapping:#[99 176].
  2159                     into:outBits mapping:#[99 176].
  2160      outBits inspect
  2160      outBits inspect
  2161     "
  2161     "
       
  2162 
       
  2163     "This Can also be used to extract nibbles ..."
       
  2164     "
       
  2165      |inBits outBits|
       
  2166 
       
  2167      inBits := #[ 16r12 16r34 16r56 16r78 16r9A 16rBC 16rDE 16rF0 ]. 
       
  2168      outBits := ByteArray new:(inBits size * 2).
       
  2169      inBits expandPixels:4 width:outBits size height:1 into:outBits mapping:nil.
       
  2170      outBits inspect
       
  2171     "
       
  2172 
  2162 !
  2173 !
  2163 
  2174 
  2164 invert
  2175 invert
  2165     "invert all bytes - used with image manipulations
  2176     "invert all bytes - used with image manipulations
  2166      written as a primitive for speed.
  2177      written as a primitive for speed.
  2850 ! !
  2861 ! !
  2851 
  2862 
  2852 !ByteArray class methodsFor:'documentation'!
  2863 !ByteArray class methodsFor:'documentation'!
  2853 
  2864 
  2854 version
  2865 version
  2855     ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.132 1999-11-08 10:04:39 cg Exp $'
  2866     ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.133 1999-11-10 16:20:51 cg Exp $'
  2856 ! !
  2867 ! !