int imFileFormatRAW::iRawUpdateParam(int index) { (void)index; imAttribTable* attrib_table = AttribTable(); // update image count int* icount = (int*)attrib_table->Get("ImageCount"); if (icount) this->image_count = *icount; else this->image_count = 1; // update file byte order int* byte_order = (int*)attrib_table->Get("ByteOrder"); if (byte_order) imBinFileByteOrder(this->handle, *byte_order); // position at start offset, the default is at 0 int* start_offset = (int*)attrib_table->Get("StartOffset"); if (!start_offset) imBinFileSeekOffset(this->handle, 0); else imBinFileSeekOffset(this->handle, *start_offset); if (imBinFileError(this->handle)) return IM_ERR_ACCESS; int* stype = (int*)attrib_table->Get("SwitchType"); if (stype) this->switch_type = *stype; // The following attributes MUST exist this->width = *(int*)attrib_table->Get("Width"); this->height = *(int*)attrib_table->Get("Height"); this->file_color_mode = *(int*)attrib_table->Get("ColorMode"); this->file_data_type = *(int*)attrib_table->Get("DataType"); int* pad = (int*)attrib_table->Get("Padding"); if (pad) { int line_size = imImageLineSize(this->width, this->file_color_mode, this->file_data_type); if (this->switch_type && (this->file_data_type == IM_FLOAT || this->file_data_type == IM_CFLOAT)) line_size *= 2; this->padding = iCalcPad(*pad, line_size); } return IM_ERR_NONE; }
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; }
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; }
static long file_seek(jas_stream_obj_t *obj, long offset, int origin) { imBinFile* file_bin = (imBinFile*)obj; switch (origin) { case SEEK_SET: imBinFileSeekTo(file_bin, offset); break; case SEEK_CUR: imBinFileSeekOffset(file_bin, offset); break; case SEEK_END: imBinFileSeekFrom(file_bin, offset); break; } return imBinFileError(file_bin); }
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; }
int imFileFormatRAW::WriteImageData(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, "Writing RAW..."); int row = 0, plane = 0; for (int i = 0; i < count; i++) { imFileLineBufferWrite(this, data, row, plane); if (ascii) { for (int col = 0; col < line_count; col++) { if (this->file_data_type == IM_FLOAT) { float value = ((float*)this->line_buffer)[col]; if (!imBinFilePrintf(handle, "%f ", (double)value)) return IM_ERR_ACCESS; } else { int value; if (this->file_data_type == IM_INT) value = ((int*)this->line_buffer)[col]; else if (this->file_data_type == IM_SHORT) value = ((short*)this->line_buffer)[col]; else if (this->file_data_type == IM_USHORT) value = ((imushort*)this->line_buffer)[col]; else value = ((imbyte*)this->line_buffer)[col]; if (!imBinFilePrintf(handle, "%d ", value)) return IM_ERR_ACCESS; } } imBinFileWrite(handle, (void*)"\n", 1, 1); } else { imBinFileWrite(this->handle, (imbyte*)this->line_buffer, line_count, type_size); } if (imBinFileError(this->handle)) return IM_ERR_ACCESS; if (!imCounterInc(this->counter)) return IM_ERR_COUNTER; imFileLineBufferInc(this, &row, &plane); if (this->padding) imBinFileSeekOffset(this->handle, this->padding); } this->image_count++; 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; }