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 %}. |