Beispiel #1
0
int imFileFormatPFM::WriteImageInfo()
{
  this->file_data_type = this->user_data_type;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  if (this->file_color_mode == IM_GRAY)
    this->image_type = 'f';
  else
  {
    this->image_type = 'F';
    this->file_color_mode |= IM_PACKED;
  }

  imBinFilePrintf(handle, "P%c\n", (int)this->image_type);

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  imBinFilePrintf(handle, "%d ", this->width);
  imBinFilePrintf(handle, "%d\n", this->height);

  if (imBinCPUByteOrder() == IM_BIGENDIAN)
    imBinFilePrintf(handle, "1.0\n");
  else
    imBinFilePrintf(handle, "-1.0\n");

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Beispiel #2
0
int imFileFormatLED::WriteImageData(void* data)
{
  imCounterTotal(this->counter, this->height, "Writing LED...");

  for (int row = 0; row < this->height; row++)
  {
    imFileLineBufferWrite(this, data, row, 0);

    for (int col = 0; col < this->width; col++)
    {
      if (!imBinFilePrintf(handle, ",%d", (int)((imbyte*)this->line_buffer)[col]))
        return IM_ERR_ACCESS;
    }
  
    imBinFileWrite(handle, (void*)"\n", 1, 1);
    if (imBinFileError(handle))
      return IM_ERR_ACCESS;     

    if (!imCounterInc(this->counter))
      return IM_ERR_COUNTER;
  }

  imBinFileWrite(handle, (void*)")", 1, 1);
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;     

  return IM_ERR_NONE;
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
Beispiel #5
0
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;
}
Beispiel #6
0
int imFileFormatRAS::WriteImageData(void* data)
{
  imCounterTotal(this->counter, this->height, "Writing RAS...");

  imbyte* compressed_buffer = NULL;
  if (this->comp_type == RAS_BYTE_ENCODED)  // point to the extra buffer
    compressed_buffer = (imbyte*)this->line_buffer + this->line_buffer_size+2;

  for (int row = 0; row < this->height; row++)
  {
    imFileLineBufferWrite(this, data, row, 0);

    if (this->bpp > 8)
      FixRGB();

    if (this->comp_type == RAS_BYTE_ENCODED)
    {
      int compressed_size = iRASEncodeScanLine(compressed_buffer, (imbyte*)this->line_buffer, this->line_raw_size);
      imBinFileWrite(handle, compressed_buffer, compressed_size, 1);
    }
    else
    {
      imBinFileWrite(handle, this->line_buffer, this->line_raw_size, 1);
    }

    if (imBinFileError(handle))
      return IM_ERR_ACCESS;     

    if (!imCounterInc(this->counter))
      return IM_ERR_COUNTER;
  }

  return IM_ERR_NONE;
}
Beispiel #7
0
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;
}
Beispiel #8
0
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;
}
Beispiel #9
0
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;
}
Beispiel #10
0
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;
}
Beispiel #11
0
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;
}
Beispiel #12
0
int imFileFormatSGI::WriteImageData(void* data)
{
  int count = imFileLineBufferCount(this);

  imCounterTotal(this->counter, count, "Writing 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++)
  {
    imFileLineBufferWrite(this, data, row, plane);

    if (this->comp_type == SGI_VERBATIM)
      imBinFileWrite(handle, this->line_buffer, this->line_buffer_size/this->bpc, this->bpc);
    else
    {
      int length;
      if (this->bpc == 1)
        length = iSGIEncodeScanLine(compressed_buffer, (imbyte*)this->line_buffer, this->width);
      else
        length = iSGIEncodeScanLine((imushort*)compressed_buffer, (imushort*)this->line_buffer, this->width);

      int row_index = row + plane*this->height;
      this->starttab[row_index] = imBinFileTell(handle);
      this->lengthtab[row_index] = length*this->bpc;

      imBinFileWrite(handle, compressed_buffer, length, this->bpc);
    }

    if (imBinFileError(handle))
      return IM_ERR_ACCESS;     

    if (!imCounterInc(this->counter))
      return IM_ERR_COUNTER;

    imFileLineBufferInc(this, &row, &plane);
  }

  if (this->comp_type == SGI_RLE)
  {
    imBinFileSeekTo(this->handle, 512);
    int tablen = this->height * imColorModeDepth(this->file_color_mode);
    imBinFileWrite(handle, this->starttab, tablen, 4);
    imBinFileWrite(handle, this->lengthtab, tablen, 4);
  }

  return IM_ERR_NONE;
}
Beispiel #13
0
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;
}
Beispiel #14
0
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;
}
Beispiel #15
0
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);
}
Beispiel #16
0
int imFileFormatLED::New(const char* file_name)
{
  /* opens the binary file for writing */
  handle = imBinFileNew(file_name);
  if (!handle)
    return IM_ERR_OPEN;

  imBinFileWrite(handle, (void*)"LEDImage = IMAGE", 16, 1);

  /* tests if everything was ok */
  if (imBinFileError(handle))
  {
    imBinFileClose(handle);
    return IM_ERR_ACCESS;
  }

  return IM_ERR_NONE;
}
Beispiel #17
0
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;
}
Beispiel #18
0
int imFileFormatRAS::WritePalette()
{
  int c;
  unsigned char ras_colors[256 * 3];

  /* convert the color map to the IM format */
  for (c = 0; c < this->palette_count; c++)
  {
    imColorDecode(&ras_colors[c], &ras_colors[c+this->palette_count], &ras_colors[c+2*this->palette_count], this->palette[c]);
  }

  /* writes the color palette */
  imBinFileWrite(handle, ras_colors, this->palette_count * 3, 1);

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Beispiel #19
0
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;
}
Beispiel #20
0
int imFileFormatLED::ReadPalette()
{
  int c, r, g, b, i;

  /* convert the color map to the IM format */
  for (c = 0; c < this->palette_count; c++)
  {
    imBinFileReadInteger(handle, &i);
    imBinFileReadInteger(handle, &r);
    imBinFileReadInteger(handle, &g);
    imBinFileReadInteger(handle, &b);

    this->palette[i] = imColorEncode((unsigned char)r, (unsigned char)g, (unsigned char)b);

    if (imBinFileError(handle))
      return IM_ERR_ACCESS;
  }

  return IM_ERR_NONE;
}
Beispiel #21
0
int imFileFormatPFM::WriteImageData(void* data)
{
  imCounterTotal(this->counter, this->height, "Writing PFM...");

  int line_raw_size = imImageLineSize(this->width, this->file_color_mode, this->file_data_type);

  for (int row = 0; row < this->height; row++)
  {
    imFileLineBufferWrite(this, data, row, 0);

    imBinFileWrite(handle, this->line_buffer, line_raw_size, 1);

    if (imBinFileError(handle))
      return IM_ERR_ACCESS;     

    if (!imCounterInc(this->counter))
      return IM_ERR_COUNTER;
  }

  return IM_ERR_NONE;
}
Beispiel #22
0
int imFileFormatLED::ReadImageInfo(int index)
{
  (void)index;

  this->palette_count = this->pal_count;

  if (ReadPalette() != IM_ERR_NONE)
    return IM_ERR_ACCESS;

  imBinFileReadInteger(handle, &this->width);
  imBinFileReadInteger(handle, &this->height);
 
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  this->file_data_type = IM_BYTE;
  this->file_color_mode = IM_MAP;
  this->file_color_mode |= IM_TOPDOWN;

  return IM_ERR_NONE;
}
Beispiel #23
0
int imFileFormatLED::WritePalette()
{
  int c;
  unsigned char r, g, b;

  imBinFileWrite(handle, (void*)"[\n", 2, 1);

  /* convert the color map from the IM format */
  for (c = 0; c < this->palette_count; c++)
  {
    imColorDecode(&r, &g, &b, this->palette[c]);
    imBinFilePrintf(handle, "%d = \"%d %d %d\"", c, (int)r, (int)g, (int)b);

    if (c != this->palette_count - 1)
      imBinFileWrite(handle, (void*)",\n", 2, 1);
  }

  imBinFileWrite(handle, (void*)"]\n", 2, 1);

  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Beispiel #24
0
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)
Beispiel #25
0
int imFileFormatRAS::WriteImageInfo()
{
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  if (imStrEqual(this->compression, "RLE"))
    this->comp_type = RAS_BYTE_ENCODED;
  else
    this->comp_type = RAS_STANDARD;

  // Force the palette, even for Binary and Gray.
  this->map_type = RAS_EQUAL_RGB;

  if (this->file_color_mode == IM_BINARY)
  {
    this->bpp = 1;
    this->convert_bpp = 1;
  }
  else if (this->file_color_mode == IM_RGB)
  {
    this->file_color_mode |= IM_PACKED;
    this->map_type = RAS_NONE;
    this->bpp = 24;

    if (imColorModeHasAlpha(this->user_color_mode))
    {
      this->file_color_mode |= IM_ALPHA;
      this->bpp = 32;
    }
  }
  else
    this->bpp = 8;

  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

  if (this->comp_type == RAS_BYTE_ENCODED)
  {
    // allocates more than enough since compression algoritm can be ineficient
    this->line_buffer_extra += 2*this->line_raw_size;
  }

  /* writes the RAS file header */

  unsigned int dword_value = RAS_ID;
  imBinFileWrite(handle, &dword_value, 1, 4); /* identifier */
  dword_value = this->width;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image width */
  dword_value = this->height;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image height */
  dword_value = this->bpp;
  imBinFileWrite(handle, &dword_value, 1, 4); /* bits per pixel */
  dword_value = this->height * this->line_raw_size;
  imBinFileWrite(handle, &dword_value, 1, 4); /* image lenght */
  dword_value = this->comp_type;
  imBinFileWrite(handle, &dword_value, 1, 4); /* compression information */
  dword_value = this->map_type;
  imBinFileWrite(handle, &dword_value, 1, 4); /* palette information */
  dword_value = (this->map_type == RAS_NONE)? 0: this->palette_count * 3;
  imBinFileWrite(handle, &dword_value, 1, 4); /* palette lenght */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  if (this->map_type != RAS_NONE)
    return WritePalette();

  return IM_ERR_NONE;
}
Beispiel #26
0
int imFileFormatSGI::WriteImageInfo()
{
  unsigned int dword_value;
  unsigned short word_value;
  unsigned char dummy[404];
  memset(dummy, 0, 404);

  this->comp_type = SGI_VERBATIM;
  if (imStrEqual(this->compression, "RLE"))
    this->comp_type = SGI_RLE;

  unsigned int color_map_id = SGI_NORMAL;

  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  int dimension = 2;
  if (this->file_color_mode == IM_BINARY)
    this->convert_bpp = -1; // expand 1 to 255
  else if (this->file_color_mode == IM_RGB)
  {
    dimension = 3;
    if (imColorModeHasAlpha(this->user_color_mode))
      this->file_color_mode |= IM_ALPHA;
  }

  this->file_data_type = this->user_data_type;

  this->bpc = 1;
  int max = 255;
  if (this->file_data_type == IM_USHORT)
  {
    max = 65535;
    this->bpc = 2;
  }

  this->starttab = NULL;
  this->lengthtab = NULL;

  /* writes the SGI file header */
  word_value = SGI_ID;
  imBinFileWrite(handle, &word_value, 1, 2); /* identifier */
  imBinFileWrite(handle, &this->comp_type, 1, 1); /* storage */
  imBinFileWrite(handle, &this->bpc, 1, 1); /* bpc */
  word_value = (imushort)dimension;
  imBinFileWrite(handle, &word_value, 1, 2); /* dimension */
  word_value = (unsigned short)this->width;
  imBinFileWrite(handle, &word_value, 1, 2); /* image width */
  word_value = (unsigned short)this->height;
  imBinFileWrite(handle, &word_value, 1, 2); /* image height */
  word_value = (imushort)imColorModeDepth(this->file_color_mode);
  imBinFileWrite(handle, &word_value, 1, 2); /* depth */
  dword_value = 0;
  imBinFileWrite(handle, &dword_value, 1, 4); /* min */
  dword_value = max;
  imBinFileWrite(handle, &dword_value, 1, 4); /* max */
  imBinFileWrite(handle, dummy, 4, 1); /* dummy */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  int size;
  char* image_name = (char*)AttribTable()->Get("Description", NULL, &size);
  if (image_name)
  {
    if (size < 80)
    {
      imBinFileWrite(handle, image_name, size, 1); 
      imBinFileWrite(handle, dummy, 80-size, 1); 
    }
    else
    {
      imBinFileWrite(handle, image_name, 79, 1); 
      imBinFileWrite(handle, (void*)"\0", 1, 1); 
    }
  }
  else
    imBinFileWrite(handle, dummy, 80, 1); /* empty image name */

  dword_value = color_map_id;
  imBinFileWrite(handle, &dword_value, 1, 4); /* color_map_id */
  imBinFileWrite(handle, dummy, 404, 1); /* dummy */

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  if (this->comp_type == SGI_RLE)
  {
    int tablen = this->height * imColorModeDepth(this->file_color_mode);
    this->starttab = (unsigned int *)malloc(tablen*4);
    this->lengthtab = (unsigned int *)malloc(tablen*4);

    /* writes the empty compression control information */
    /* we will write again at the end */
    imBinFileWrite(handle, this->starttab, tablen*4, 1);
    imBinFileWrite(handle, this->lengthtab, tablen*4, 1);

    // 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);
  }

  /* tests if everything was ok */
  if (imBinFileError(handle))
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Beispiel #27
0
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;
}
Beispiel #28
0
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;
}
Beispiel #29
0
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;
}
Beispiel #30
0
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;
}