int imFileFormatLED::Open(const char* file_name) { char sig[4]; unsigned char byte_value; int found = 0; /* opens the binary file for reading */ handle = imBinFileOpen(file_name); if (!handle) return IM_ERR_OPEN; this->image_count = 1; strcpy(this->compression, "NONE"); imBinFileRead(handle, sig, 3, 1); sig[3] = 0; if (imBinFileError(handle)) { imBinFileClose(handle); return IM_ERR_ACCESS; } if (!imStrEqual(sig, "LED")) { imBinFileClose(handle); return IM_ERR_FORMAT; } unsigned long offset = imBinFileTell(handle); /* count the number of colors */ this->pal_count = -1; // will count the first '=' that is not a color while (!found) { imBinFileRead(handle, &byte_value, 1, 1); if (byte_value == '(') found = 1; if (byte_value == '=') this->pal_count++; if (imBinFileError(handle)) { imBinFileClose(handle); return IM_ERR_ACCESS; } } imBinFileSeekTo(handle, offset); return IM_ERR_NONE; }
static int iBMPDecodeScanLine(imBinFile* handle, unsigned char* DecodedBuffer, int Width) { unsigned char runCount; /* Number of pixels in the run */ unsigned char runValue; /* Value of pixels in the run */ int Index = 0; /* The index of DecodedBuffer */ int cont = 1, remain; while (cont) { imBinFileRead(handle, &runCount, 1, 1); /* Number of pixels in the run */ imBinFileRead(handle, &runValue, 1, 1); /* Value of pixels in the run */ if (imBinFileError(handle)) return IM_ERR_ACCESS; if (runCount) { while (runCount-- && Index < Width) DecodedBuffer[Index++] = runValue; } else /* Abssolute Mode or Escape Code */ { switch(runValue) { case 0: /* End of Scan Line Escape Code */ case 1: /* End of Bitmap Escape Code */ cont = 0; break; case 2: /* Delta Escape Code (ignored) */ imBinFileRead(handle, &runCount, 1, 1); imBinFileRead(handle, &runCount, 1, 1); break; default: /* Abssolute Mode */ remain = runValue % 2; runValue = (unsigned char)(Index + runValue < (Width + 1)? runValue: (Width - 1) - Index); imBinFileRead(handle, DecodedBuffer + Index, runValue, 1); if (remain) imBinFileSeekOffset(handle, 1); Index += runValue; } } if (imBinFileError(handle) || Index > Width) return IM_ERR_ACCESS; } return IM_ERR_NONE; }
int imFileFormatPFM::Open(const char* file_name) { unsigned char sig[2]; /* opens the binary file for reading */ handle = imBinFileOpen(file_name); if (!handle) return IM_ERR_OPEN; /* reads the PFM format identifier */ imBinFileRead(handle, sig, 2, 1); if (imBinFileError(handle)) { imBinFileClose(handle); return IM_ERR_ACCESS; } if (sig[0] != 'P' || (sig[1] != 'f' && sig[1] != 'F')) { imBinFileClose(handle); return IM_ERR_FORMAT; } this->image_type = sig[1]; // 'F' means color, 'f' means grayscale this->image_count = 1; strcpy(this->compression, "NONE"); return IM_ERR_NONE; }
int imBinFileReadInteger(imBinFile* handle, int *value) { int i = 0, found = 0; char buffer[11], c; while (!found) { imBinFileRead(handle, &c, 1, 1); /* if it's an integer, increments the number of characters read */ if ((c >= '0' && c <= '9') || (c == '-')) { buffer[i] = c; i++; } else { /* if it's not, and we read some characters, convert them to an integer */ if (i > 0) { buffer[i] = 0; *value = atoi(buffer); found = 1; } } if (imBinFileError(handle) || i > 10) return 0; } return 1; }
int imFileFormatRAS::ReadImageData(void* data) { imCounterTotal(this->counter, this->height, "Reading RAS..."); for (int row = 0; row < this->height; row++) { /* read and decompress the data */ if (this->comp_type != RAS_BYTE_ENCODED) { imBinFileRead(handle, this->line_buffer, this->line_raw_size, 1); if (imBinFileError(handle)) return IM_ERR_ACCESS; } else { if (iRASDecodeScanLine(handle, (imbyte*)this->line_buffer, this->line_raw_size) == IM_ERR_ACCESS) return IM_ERR_ACCESS; } if (this->bpp > 8) FixRGB(); imFileLineBufferRead(this, data, row, 0); if (!imCounterInc(this->counter)) return IM_ERR_COUNTER; } return IM_ERR_NONE; }
int imFileFormatRAS::ReadPalette() { unsigned char ras_colors[256 * 3]; /* reads the color palette */ imBinFileRead(handle, ras_colors, this->palette_count * 3, 1); if (imBinFileError(handle)) return IM_ERR_ACCESS; /* convert the color map to the IM format */ for (int c = 0; c < this->palette_count; c++) { if (this->map_type == RAS_RAW) { int i = c * 3; this->palette[c] = imColorEncode(ras_colors[i], ras_colors[i+1], ras_colors[i+2]); } else { this->palette[c] = imColorEncode(ras_colors[c], ras_colors[c+this->palette_count], ras_colors[c+2*this->palette_count]); } } return IM_ERR_NONE; }
int imBinFileReadFloat(imBinFile* handle, float *value) { int i = 0, found = 0; char buffer[17], c; while (!found) { imBinFileRead(handle, &c, 1, 1); /* if it's a floating point number, increments the number of characters read */ if ((c >= '0' && c <= '9') || c == '-' || c == '+' || c == '.' || c == 'e' || c == 'E') { buffer[i] = c; i++; } else { /* if it's not, and we read some characters convert them to an integer */ if (i > 0) { buffer[i] = 0; *value = (float)atof(buffer); found = 1; } } if (imBinFileError(handle) || i > 16) return 0; } return 1; }
int imFileFormatRAS::Open(const char* file_name) { unsigned int dword_value; /* opens the binary file for reading with motorola byte order */ handle = imBinFileOpen(file_name); if (!handle) return IM_ERR_OPEN; imBinFileByteOrder(handle, IM_BIGENDIAN); /* reads the RAS format identifier */ imBinFileRead(handle, &dword_value, 1, 4); if (imBinFileError(handle)) { imBinFileClose(handle); return IM_ERR_ACCESS; } if (dword_value != RAS_ID) { imBinFileClose(handle); return IM_ERR_FORMAT; } /* reads the compression information */ imBinFileSeekOffset(handle, 16); imBinFileRead(handle, &this->comp_type, 1, 4); if (this->comp_type == RAS_BYTE_ENCODED) strcpy(this->compression, "RLE"); else if (this->comp_type == RAS_OLD || this->comp_type == RAS_STANDARD) strcpy(this->compression, "NONE"); else { imBinFileClose(handle); return IM_ERR_COMPRESS; } imBinFileSeekOffset(handle, -20); this->image_count = 1; return IM_ERR_NONE; }
int imFileFormatSGI::ReadImageData(void* data) { int count = imFileLineBufferCount(this); imCounterTotal(this->counter, count, "Reading SGI..."); imbyte* compressed_buffer = NULL; if (this->comp_type == SGI_RLE) // point to the extra buffer compressed_buffer = (imbyte*)this->line_buffer + this->line_buffer_size; int row = 0, plane = 0; for (int i = 0; i < count; i++) { if (this->comp_type == SGI_VERBATIM) { imBinFileRead(handle, this->line_buffer, this->line_buffer_size/this->bpc, this->bpc); if (imBinFileError(handle)) return IM_ERR_ACCESS; } else { int row_index = row + plane*this->height; imBinFileSeekTo(handle, this->starttab[row_index]); imBinFileRead(handle, compressed_buffer, this->lengthtab[row_index] / this->bpc, this->bpc); if (imBinFileError(handle)) return IM_ERR_ACCESS; if (this->bpc == 1) iSGIDecodeScanLine((imbyte*)this->line_buffer, compressed_buffer, this->width); else iSGIDecodeScanLine((imushort*)this->line_buffer, (imushort*)compressed_buffer, this->width); } imFileLineBufferRead(this, data, row, plane); if (!imCounterInc(this->counter)) return IM_ERR_COUNTER; imFileLineBufferInc(this, &row, &plane); } return IM_ERR_NONE; }
int imFileFormatSGI::Open(const char* file_name) { unsigned short word_value; /* opens the binary file for reading with motorola byte order */ handle = imBinFileOpen(file_name); if (!handle) return IM_ERR_OPEN; imBinFileByteOrder(handle, IM_BIGENDIAN); /* reads the SGI format identifier */ imBinFileRead(handle, &word_value, 1, 2); if (imBinFileError(handle)) { imBinFileClose(handle); return IM_ERR_ACCESS; } if (word_value != SGI_ID) { imBinFileClose(handle); return IM_ERR_FORMAT; } /* reads the compression information */ imBinFileRead(handle, &this->comp_type, 1, 1); if (this->comp_type == SGI_RLE) strcpy(this->compression, "RLE"); else if (this->comp_type == SGI_VERBATIM) strcpy(this->compression, "NONE"); else { imBinFileClose(handle); return IM_ERR_COMPRESS; } this->starttab = NULL; this->lengthtab = NULL; this->image_count = 1; return IM_ERR_NONE; }
static int iRASDecodeScanLine(imBinFile* handle, unsigned char* DecodedBuffer, int BufferSize) { int index = 0; unsigned char count = 0; unsigned char value = 0; while (index < BufferSize) { imBinFileRead(handle, &value, 1, 1); if (value == RAS_ESCAPE) { imBinFileRead(handle, &count, 1, 1); if (count != 0) { imBinFileRead(handle, &value, 1, 1); count++; while (count-- && index < BufferSize) { *DecodedBuffer++ = value; index++; } } else { *DecodedBuffer++ = RAS_ESCAPE; index++; } } else { *DecodedBuffer++ = value; index++; } if (imBinFileError(handle)) return IM_ERR_ACCESS; } return IM_ERR_NONE; }
int imFileFormatJPEG::Open(const char* file_name) { this->handle = imBinFileOpen(file_name); if (this->handle == NULL) return IM_ERR_OPEN; unsigned char sig[2]; if (!imBinFileRead(this->handle, sig, 2, 1)) { imBinFileClose(this->handle); return IM_ERR_ACCESS; } if (sig[0] != 0xFF || sig[1] != 0xD8) { imBinFileClose(this->handle); return IM_ERR_FORMAT; } imBinFileSeekTo(this->handle, 0); strcpy(this->compression, "JPEG"); this->image_count = 1; this->dinfo.err = jpeg_std_error(&this->jerr.pub); this->jerr.pub.error_exit = JPEGerror_exit; this->jerr.pub.output_message = JPEGoutput_message; this->jerr.pub.emit_message = JPEGemit_message; /* Establish the setjmp return context for error_exit to use. */ if (setjmp(this->jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jpeg_destroy_decompress(&this->dinfo); imBinFileClose(this->handle); return IM_ERR_FORMAT; } /* Now we can initialize the JPEG decompression object. */ jpeg_create_decompress(&this->dinfo); /* Step 2: specify data source (eg, a file) */ jpeg_stdio_src(&this->dinfo, (FILE*)this->handle); return IM_ERR_NONE; }
int imFileFormatPFM::ReadImageData(void* data) { imCounterTotal(this->counter, this->height, "Reading PFM..."); int line_raw_size = imImageLineSize(this->width, this->file_color_mode, this->file_data_type); for (int row = 0; row < this->height; row++) { imBinFileRead(handle, this->line_buffer, line_raw_size, 1); if (imBinFileError(handle)) return IM_ERR_ACCESS; imFileLineBufferRead(this, data, row, 0); if (!imCounterInc(this->counter)) return IM_ERR_COUNTER; } return IM_ERR_NONE; }
int imFileFormatRAW::ReadImageData(void* data) { int count = imFileLineBufferCount(this); int line_count = imImageLineCount(this->width, this->file_color_mode); int type_size = iFileDataTypeSize(this->file_data_type, this->switch_type); // treat complex as 2 real if (this->file_data_type == IM_CFLOAT) { type_size /= 2; line_count *= 2; } int ascii; if (imStrEqual(this->compression, "ASCII")) ascii = 1; else ascii = 0; imCounterTotal(this->counter, count, "Reading RAW..."); int row = 0, plane = 0; for (int i = 0; i < count; i++) { if (ascii) { for (int col = 0; col < line_count; col++) { if (this->file_data_type == IM_FLOAT) { float value; if (!imBinFileReadFloat(handle, &value)) return IM_ERR_ACCESS; ((float*)this->line_buffer)[col] = value; } else { int value; if (!imBinFileReadInteger(handle, &value)) return IM_ERR_ACCESS; if (this->file_data_type == IM_INT) ((int*)this->line_buffer)[col] = value; else if (this->file_data_type == IM_SHORT) ((short*)this->line_buffer)[col] = (short)value; else if (this->file_data_type == IM_USHORT) ((imushort*)this->line_buffer)[col] = (imushort)value; else ((imbyte*)this->line_buffer)[col] = (unsigned char)value; } } } else { imBinFileRead(this->handle, (imbyte*)this->line_buffer, line_count, type_size); if (imBinFileError(this->handle)) return IM_ERR_ACCESS; } imFileLineBufferRead(this, data, row, plane); if (!imCounterInc(this->counter)) return IM_ERR_COUNTER; imFileLineBufferInc(this, &row, &plane); if (this->padding) imBinFileSeekOffset(this->handle, this->padding); } return IM_ERR_NONE; }
int imFileFormatRAS::ReadImageInfo(int index) { (void)index; unsigned int dword_value; this->file_data_type = IM_BYTE; /* reads the image width */ imBinFileRead(handle, &dword_value, 1, 4); this->width = (int)dword_value; /* reads the image height */ imBinFileRead(handle, &dword_value, 1, 4); this->height = (int)dword_value; /* reads the number of bits per pixel */ imBinFileRead(handle, &this->bpp, 1, 4); if (imBinFileError(handle)) return IM_ERR_ACCESS; // sanity check if (this->bpp != 1 && this->bpp != 8 && this->bpp != 24 && this->bpp != 32) return IM_ERR_DATA; if (this->bpp > 8) { this->file_color_mode = IM_RGB; this->file_color_mode |= IM_PACKED; if (this->bpp == 32) this->file_color_mode |= IM_ALPHA; } else { this->file_color_mode = IM_MAP; if (this->bpp == 1) { this->convert_bpp = 1; this->palette_count = 2; } } this->file_color_mode |= IM_TOPDOWN; this->line_raw_size = imFileLineSizeAligned(this->width, this->bpp, 2); this->line_buffer_extra = 2; // room enough for padding /* jump 8 bytes (Length+Compression) */ imBinFileSeekOffset(handle, 8); /* reads the palette information */ imBinFileRead(handle, &this->map_type, 1, 4); /* reads the palette size */ imBinFileRead(handle, &dword_value, 1, 4); if (imBinFileError(handle)) return IM_ERR_ACCESS; /* updates the pal_size based on the palette size */ if (this->bpp <= 8 && this->map_type != RAS_NONE) { this->palette_count = dword_value / 3; return ReadPalette(); } if (this->bpp <= 8 && this->map_type == RAS_NONE) { if (this->bpp == 1) this->file_color_mode = IM_BINARY; else this->file_color_mode = IM_GRAY; this->file_color_mode |= IM_TOPDOWN; } return IM_ERR_NONE; }
int imFileFormatSGI::ReadImageInfo(int index) { (void)index; unsigned short word_value, dimension, depth; /* reads the number of bits per channel */ imBinFileRead(handle, &this->bpc, 1, 1); /* reads the number of dimensions */ imBinFileRead(handle, &dimension, 1, 2); /* reads the image width */ imBinFileRead(handle, &word_value, 1, 2); this->width = word_value; /* reads the image height */ imBinFileRead(handle, &word_value, 1, 2); this->height = word_value; /* reads the number of channels */ imBinFileRead(handle, &depth, 1, 2); /* jump 12 bytes (min, max, dummy) */ imBinFileSeekOffset(handle, 12); /* reads the image name */ char image_name[80]; imBinFileRead(handle, image_name, 80, 1); if (image_name[0] != 0) AttribTable()->Set("Description", IM_BYTE, imStrNLen(image_name, 80)+1, image_name); /* reads the color map information */ unsigned int color_map_id; imBinFileRead(handle, &color_map_id, 1, 4); if (imBinFileError(handle)) return IM_ERR_ACCESS; this->file_data_type = IM_BYTE; if (this->bpc == 2) this->file_data_type = IM_USHORT; switch (dimension) { case 1: this->height = 1; depth = 1; case 2: depth = 1; break; case 3: break; default: return IM_ERR_DATA; } switch (color_map_id) { case SGI_NORMAL: switch(depth) { case 1: this->file_color_mode = IM_GRAY; break; case 3: this->file_color_mode = IM_RGB; break; case 4: this->file_color_mode = IM_RGB | IM_ALPHA; break; default: return IM_ERR_DATA; } break; case SGI_DITHERED: this->file_color_mode = IM_MAP; break; case SGI_COLORMAP: this->file_color_mode = IM_RGB; break; case SGI_SCREEN: this->file_color_mode = IM_GRAY; break; default: return IM_ERR_DATA; } /* jump 404 bytes (dummy) */ imBinFileSeekOffset(handle, 404); if (this->comp_type == SGI_RLE) { int tablen = this->height * depth; this->starttab = (unsigned int *)malloc(tablen * sizeof(int)); this->lengthtab = (unsigned int *)malloc(tablen * sizeof(int)); /* reads the compression control information */ imBinFileRead(handle, this->starttab, tablen, 4); imBinFileRead(handle, this->lengthtab, tablen, 4); // allocates more than enough since compression algoritm can be ineficient this->line_buffer_extra = 2*imImageLineSize(this->width, this->file_color_mode, this->file_data_type); } if (imBinFileError(handle)) return IM_ERR_ACCESS; if (color_map_id == SGI_DITHERED) { static int red[8] = {0, 36, 73, 109, 146, 182, 218, 255}; static int green[8] = {0, 36, 73, 109, 146, 182, 218, 255}; static int blue[4] = {0, 85, 170, 255}; int c = 0; for (int b = 0; b < 4; b++) { for (int g = 0; g < 8; g++) { for (int r = 0; r < 8; r++) { this->palette[c] = imColorEncode((imbyte)red[r], (imbyte)green[g], (imbyte)blue[b]); c++; } } } } return IM_ERR_NONE; }
int imFileFormatBMP::ReadImageInfo(int index) { (void)index; unsigned int dword; this->file_data_type = IM_BYTE; if (this->is_os2) { short word; /* reads the image width */ imBinFileRead(handle, &word, 1, 2); this->width = (int)word; /* reads the image height */ imBinFileRead(handle, &word, 1, 2); this->height = (int)((word < 0)? -word: word); dword = word; // it will be used later } else { /* reads the image width */ imBinFileRead(handle, &dword, 1, 4); this->width = (int)dword; /* reads the image height */ imBinFileRead(handle, &dword, 1, 4); this->height = (int)dword; if (this->height < 0) this->height = -this->height; } /* jump 2 bytes (planes) */ imBinFileSeekOffset(handle, 2); /* reads the number of bits per pixel */ imBinFileRead(handle, &this->bpp, 1, 2); if (imBinFileError(handle)) return IM_ERR_ACCESS; // sanity check if (this->bpp != 1 && this->bpp != 4 && this->bpp != 8 && this->bpp != 16 && this->bpp != 24 && this->bpp != 32) return IM_ERR_DATA; // another sanity check if (this->comp_type == BMP_BITFIELDS && this->bpp != 16 && this->bpp != 32) return IM_ERR_DATA; if (this->bpp > 8) { this->file_color_mode = IM_RGB; this->file_color_mode |= IM_PACKED; } else { this->palette_count = 1 << bpp; this->file_color_mode = IM_MAP; } if (this->bpp < 8) this->convert_bpp = this->bpp; if (this->bpp == 32) this->file_color_mode |= IM_ALPHA; if (dword < 0) this->file_color_mode |= IM_TOPDOWN; this->line_raw_size = imFileLineSizeAligned(this->width, this->bpp, 4); this->line_buffer_extra = 4; // room enough for padding if (this->is_os2) { if (this->bpp < 24) return ReadPalette(); return IM_ERR_NONE; } /* we already read the compression information */ /* jump 8 bytes (compression, image size) */ imBinFileSeekOffset(handle, 8); /* read the x resolution */ imBinFileRead(handle, &dword, 1, 4); float xres = (float)dword / 100.0f; /* read the y resolution */ imBinFileRead(handle, &dword, 1, 4); float yres = (float)dword / 100.0f; if (xres && yres) { imAttribTable* attrib_table = AttribTable(); attrib_table->Set("XResolution", IM_FLOAT, 1, &xres); attrib_table->Set("YResolution", IM_FLOAT, 1, &yres); attrib_table->Set("ResolutionUnit", IM_BYTE, -1, "DPC"); } if (this->bpp <= 8) { /* reads the number of colors used */ imBinFileRead(handle, &dword, 1, 4); /* updates the palette_count based on the number of colors used */ if (dword != 0 && dword < (unsigned int)this->palette_count) this->palette_count = dword; /* jump 4 bytes (important colors) */ imBinFileSeekOffset(handle, 4); } else { /* jump 8 bytes (used colors, important colors) */ imBinFileSeekOffset(handle, 8); } if (imBinFileError(handle)) return IM_ERR_ACCESS; if (this->bpp <= 8) return ReadPalette(); if (this->bpp == 16 || this->bpp == 32) { if (this->comp_type == BMP_BITFIELDS) { unsigned int Mask; unsigned int PalMask[3]; imBinFileRead(handle, PalMask, 3, 4); if (imBinFileError(handle)) return IM_ERR_ACCESS; this->roff = 0; this->rmask = Mask = PalMask[0]; while (!(Mask & 0x01) && (Mask != 0)) {Mask >>= 1; this->roff++;} this->goff = 0; this->gmask = Mask = PalMask[1]; while (!(Mask & 0x01) && (Mask != 0)) {Mask >>= 1; this->goff++;} this->boff = 0; this->bmask = Mask = PalMask[2]; while (!(Mask & 0x01) && (Mask != 0)) {Mask >>= 1; this->boff++;} } else { if (this->bpp == 16)
int imFileFormatBMP::Open(const char* file_name) { unsigned short id; unsigned int dword; /* opens the binary file for reading with intel byte order */ handle = imBinFileOpen(file_name); if (!handle) return IM_ERR_OPEN; imBinFileByteOrder(handle, IM_LITTLEENDIAN); /* reads the BMP format identifier */ imBinFileRead(handle, &id, 1, 2); if (imBinFileError(handle)) { imBinFileClose(handle); return IM_ERR_ACCESS; } if (id != BMP_ID) { imBinFileClose(handle); return IM_ERR_FORMAT; } /* jump 8 bytes (file size,reserved) */ imBinFileSeekOffset(handle, 8); /* reads the image offset */ imBinFileRead(handle, &this->offset, 1, 4); /* reads the header size */ imBinFileRead(handle, &dword, 1, 4); if (dword == 40) this->is_os2 = 0; else if (dword == 12) this->is_os2 = 1; else { imBinFileClose(handle); return IM_ERR_FORMAT; } this->image_count = 1; /* reads the compression information */ if (this->is_os2) { this->comp_type = BMP_COMPRESS_RGB; strcpy(this->compression, "NONE"); } else { imBinFileSeekOffset(handle, 12); imBinFileRead(handle, &this->comp_type, 1, 4); switch (this->comp_type) { case BMP_COMPRESS_RGB: strcpy(this->compression, "NONE"); break; case BMP_COMPRESS_RLE8: strcpy(this->compression, "RLE"); break; case BMP_COMPRESS_RLE4: default: imBinFileClose(handle); return IM_ERR_COMPRESS; } imBinFileSeekOffset(handle, -16); } return IM_ERR_NONE; }
static int file_read(jas_stream_obj_t *obj, char *buf, int cnt) { imBinFile* file_bin = (imBinFile*)obj; return imBinFileRead(file_bin, buf, cnt, 1); }