Load image pixels for 4-bit RLE compressed dib @param io FreeImage IO @param handle FreeImage IO handle @param width Image width @param height Image height @param dib Image to be loaded @return Returns TRUE if successful, returns FALSE otherwise */
| 205 | @return Returns TRUE if successful, returns FALSE otherwise |
| 206 | */ |
| 207 | static BOOL |
| 208 | LoadPixelDataRLE4(FreeImageIO *io, fi_handle handle, int width, int height, FIBITMAP *dib) { |
| 209 | int status_byte = 0; |
| 210 | BYTE second_byte = 0; |
| 211 | int bits = 0; |
| 212 | |
| 213 | BYTE *pixels = NULL; // temporary 8-bit buffer |
| 214 | |
| 215 | try { |
| 216 | height = abs(height); |
| 217 | |
| 218 | pixels = (BYTE*)malloc(width * height * sizeof(BYTE)); |
| 219 | if(!pixels) throw(1); |
| 220 | memset(pixels, 0, width * height * sizeof(BYTE)); |
| 221 | |
| 222 | BYTE *q = pixels; |
| 223 | BYTE *end = pixels + height * width; |
| 224 | |
| 225 | for (int scanline = 0; scanline < height; ) { |
| 226 | if (q < pixels || q >= end) { |
| 227 | break; |
| 228 | } |
| 229 | if(io->read_proc(&status_byte, sizeof(BYTE), 1, handle) != 1) { |
| 230 | throw(1); |
| 231 | } |
| 232 | if (status_byte != 0) { |
| 233 | status_byte = (int)MIN((size_t)status_byte, (size_t)(end - q)); |
| 234 | // Encoded mode |
| 235 | if(io->read_proc(&second_byte, sizeof(BYTE), 1, handle) != 1) { |
| 236 | throw(1); |
| 237 | } |
| 238 | for (int i = 0; i < status_byte; i++) { |
| 239 | *q++=(BYTE)((i & 0x01) ? (second_byte & 0x0f) : ((second_byte >> 4) & 0x0f)); |
| 240 | } |
| 241 | bits += status_byte; |
| 242 | } |
| 243 | else { |
| 244 | // Escape mode |
| 245 | if(io->read_proc(&status_byte, sizeof(BYTE), 1, handle) != 1) { |
| 246 | throw(1); |
| 247 | } |
| 248 | switch (status_byte) { |
| 249 | case RLE_ENDOFLINE: |
| 250 | { |
| 251 | // End of line |
| 252 | bits = 0; |
| 253 | scanline++; |
| 254 | q = pixels + scanline*width; |
| 255 | } |
| 256 | break; |
| 257 | |
| 258 | case RLE_ENDOFBITMAP: |
| 259 | // End of bitmap |
| 260 | q = end; |
| 261 | break; |
| 262 | |
| 263 | case RLE_DELTA: |
| 264 | { |
no test coverage detected