Esempio n. 1
0
int imFileFormatJP2::WriteImageData(void* data)
{
  int count = imFileLineBufferCount(this);
  imCounterTotal(this->counter, count, "Writing JP2...");  /* first time count */

  int depth = imColorModeDepth(this->file_color_mode);
  if (imColorModeHasAlpha(this->user_color_mode) && imColorModeHasAlpha(this->file_color_mode))
  {
    jas_image_setcmpttype(image, depth-1, JAS_IMAGE_CT_OPACITY);
    depth--;
  }

  for (int d = 0; d < depth; d++)
    jas_image_setcmpttype(image, d, JAS_IMAGE_CT_COLOR(d));

  int row = 0, plane = 0;
  for (int i = 0; i < count; i++)
  {
    imFileLineBufferWrite(this, data, row, plane);

    int ret = 1;
    if (this->file_data_type == IM_BYTE)
      ret = iJP2WriteLine(image, row, plane, (imbyte*)this->line_buffer);
    else
      ret = iJP2WriteLine(image, row, plane, (imushort*)this->line_buffer);

    if (!ret)
      return IM_ERR_ACCESS;

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

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

  char outopts[512] = "";
  imAttribTable* attrib_table = AttribTable();

  float* ratio = (float*)attrib_table->Get("CompressionRatio");
  if (ratio)
    sprintf(outopts, "rate=%g", (double)(1.0 / *ratio));

  // The counter continuous because in Jasper all image writing is done here. BAD!
  ijp2_counter = this->counter;
  ijp2_abort = 0;
  ijp2_message = NULL;  /* other counts */
  int err = jas_image_encode(image, stream, 0 /*JP2 format always */, outopts);
  ijp2_counter = -1;
  if (err)
    return IM_ERR_ACCESS;

  jas_stream_flush(stream);

  return IM_ERR_NONE;
}
Esempio n. 2
0
int imColorModeDepth(int color_mode)
{
  int depth = 0;

  int color_space = imColorModeSpace(color_mode);
  switch (color_space)
  {
  case IM_GRAY:
  case IM_BINARY:
  case IM_MAP:   
    depth = 1; 
    break;
  case IM_CMYK:
    depth = 4; 
    break;
  default:
    depth = 3; 
    break;
  }

  if (imColorModeHasAlpha(color_mode))
    depth++;

  return depth;
}
Esempio n. 3
0
int imFileFormatJP2::ReadImageData(void* data)
{
  int count = imFileLineBufferCount(this);

  imCounterTotal(this->counter, count, NULL);

  int alpha_plane = -1;
  if (imColorModeHasAlpha(this->user_color_mode) && imColorModeHasAlpha(this->file_color_mode))
    alpha_plane = imColorModeDepth(this->file_color_mode) - 1;

  int row = 0, plane = 0;
  for (int i = 0; i < count; i++)
  {
    int cmpno;
    if (plane == alpha_plane)
      cmpno = jas_image_getcmptbytype(image, JAS_IMAGE_CT_OPACITY);
    else
      cmpno = jas_image_getcmptbytype(image, JAS_IMAGE_CT_COLOR(plane));

    if (cmpno == -1)
      return IM_ERR_DATA;

    int ret = 1;
    if (this->file_data_type == IM_BYTE)
      ret = iJP2ReadLine(image, row, cmpno, (imbyte*)this->line_buffer);
    else
      ret = iJP2ReadLine(image, row, cmpno, (imushort*)this->line_buffer);

    if (!ret)
      return IM_ERR_ACCESS;

    imFileLineBufferRead(this, data, row, plane);

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

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

  return IM_ERR_NONE;
}
Esempio n. 4
0
/*****************************************************************************\
 im.ColorModeHasAlpha(color_mode)
\*****************************************************************************/
static int imluaColorModeHasAlpha (lua_State *L)
{
  lua_pushboolean(L, imColorModeHasAlpha(luaL_checkinteger(L, 1)));
  return 1;
}
Esempio n. 5
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;
}
Esempio n. 6
0
int imFileFormatJP2::WriteImageInfo()
{
  this->file_data_type = this->user_data_type;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);
  this->file_color_mode |= IM_TOPDOWN;

  int prec = 8;
  if (this->file_data_type == IM_USHORT)
    prec = 16;

  jas_clrspc_t clrspc;
  switch (imColorModeSpace(this->user_color_mode))
  {
  case IM_BINARY:
    prec = 1;    
  case IM_GRAY:
    clrspc = JAS_CLRSPC_SGRAY;
    break;
  case IM_RGB:   
    clrspc = JAS_CLRSPC_SRGB;
    break;
  case IM_XYZ:
    clrspc = JAS_CLRSPC_CIEXYZ;
    break;
  case IM_LAB:
    clrspc = JAS_CLRSPC_CIELAB;
    break;
  case IM_YCBCR:
    clrspc = JAS_CLRSPC_SYCBCR;
    break;
  default:
    return IM_ERR_DATA;
  }

  if (imColorModeHasAlpha(this->user_color_mode))
    this->file_color_mode |= IM_ALPHA;

  int numcmpts = imColorModeDepth(this->file_color_mode);
  
  jas_image_cmptparm_t cmptparms[4];
  for (int i = 0; i < numcmpts; i++) 
  {
    jas_image_cmptparm_t* cmptparm = &cmptparms[i];

    cmptparm->tlx = 0;
    cmptparm->tly = 0;
    cmptparm->hstep = 1;
    cmptparm->vstep = 1;
    cmptparm->width = this->width;
    cmptparm->height = this->height;
    cmptparm->prec = prec;
    cmptparm->sgnd = 0;
  }

  this->image = jas_image_create(numcmpts, cmptparms, clrspc);
  if (!this->image)
    return IM_ERR_DATA;

  if (this->image->metadata.count > 0) 
  {
    const void* data;
    int size;
    imAttribTable* attrib_table = AttribTable();

    // GeoTIFF first
    data = attrib_table->Get("GeoTIFFBox", NULL, &size);
    if (data)
    {
      jas_metadata_box_t *metabox = &image->metadata.boxes[JAS_IMAGE_BOX_GEO]; 
      jas_box_alloc(metabox, size);
      memcpy(metabox->buf, data, size);
      memcpy(metabox->id, msi_uuid, sizeof(msi_uuid));
    }
   
    // Adobe XMP
    data = attrib_table->Get("XMLPacket", NULL, &size);
    if (data)
    {
      jas_metadata_box_t *metabox = &image->metadata.boxes[JAS_IMAGE_BOX_XMP]; 
      jas_box_alloc(metabox, size);
      memcpy(metabox->buf, data, size);
      memcpy(metabox->id, xmp_uuid, sizeof(xmp_uuid));
    }
  }

  return IM_ERR_NONE;
}
Esempio n. 7
0
int imFileFormatAVI::WriteImageInfo()
{
  if (dib)
  {
    if (dib->bmih->biWidth != width || dib->bmih->biHeight != height ||
        imColorModeSpace(file_color_mode) != imColorModeSpace(user_color_mode))
      return IM_ERR_DATA;

    return IM_ERR_NONE;  // parameters can be set only once
  }

  // force bottom up orientation
  this->file_data_type = IM_BYTE;
  this->file_color_mode = imColorModeSpace(this->user_color_mode);

  int bpp;
  if (this->file_color_mode == IM_RGB)
  {
    this->file_color_mode |= IM_PACKED;
    bpp = 24;

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

      this->rmask = 0x00FF0000;
      this->roff = 16;

      this->gmask = 0x0000FF00;
      this->goff = 8;

      this->bmask = 0x000000FF;
      this->boff = 0;
    }
  }
  else
    bpp = 8;

  this->line_buffer_extra = 4; // room enough for padding

  imAttribTable* attrib_table = AttribTable();

  const void* attrib_data = attrib_table->Get("FPS");
  if (attrib_data)
    fps = *(float*)attrib_data;
  else
    fps = 15;

  if (this->compression[0] == 0 || imStrEqual(this->compression, "NONE"))
    this->use_compressor = 0;
  else
    this->use_compressor = 1;

  dib = imDibCreate(width, height, bpp);

  if (use_compressor)
  {
    memset(&compvars, 0, sizeof(COMPVARS));
    compvars.cbSize = sizeof(COMPVARS);

    if (imStrEqual(this->compression, "CUSTOM"))
    {
      if (ICCompressorChoose(NULL, ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME, dib->dib, NULL, &compvars, "Choose Compression") == FALSE)
        return IM_ERR_COMPRESS;
    }
    else
    {
      compvars.dwFlags = ICMF_COMPVARS_VALID;
      compvars.fccType = ICTYPE_VIDEO;

      int* attrib = (int*)attrib_table->Get("KeyFrameRate");
      if (attrib)
        compvars.lKey = *attrib;
      else
        compvars.lKey = 15;        // same defaults of the dialog

      attrib = (int*)attrib_table->Get("DataRate");
      if (attrib)
        compvars.lDataRate = *attrib / 8;
      else
        compvars.lDataRate = 300;  // same defaults of the dialog

      attrib = (int*)attrib_table->Get("AVIQuality");
      if (attrib)
        compvars.lQ = *attrib;
      else
        compvars.lQ = (DWORD)ICQUALITY_DEFAULT;

      if (imStrEqual(this->compression, "RLE"))
        compvars.fccHandler = mmioFOURCC('M','R','L','E');
      else if (imStrEqual(this->compression, "CINEPACK"))
        compvars.fccHandler = mmioFOURCC('c','v','i','d');    
      else
        compvars.fccHandler = mmioFOURCC(compression[0],compression[1],compression[2],compression[3]);

      compvars.hic = ICOpen(ICTYPE_VIDEO, compvars.fccHandler, ICMODE_COMPRESS);
    }

    if (compvars.hic == NULL)
      use_compressor = 0;
  }

  AVISTREAMINFO streaminfo;
  memset(&streaminfo, 0, sizeof(AVISTREAMINFO));
  streaminfo.fccType = streamtypeVIDEO;
  streaminfo.dwScale = 1000;
  streaminfo.dwRate  = (DWORD)(fps*1000);
  SetRect(&streaminfo.rcFrame, 0, 0, width, height);

  if (use_compressor)
  {
    streaminfo.fccHandler = compvars.fccHandler;
    streaminfo.dwQuality = compvars.lQ;
  }
  else
  {
    streaminfo.fccHandler = mmioFOURCC('D','I','B',' ');
    streaminfo.dwQuality = (DWORD)ICQUALITY_DEFAULT;
  }

  /* creates a new stream in the new file */
  HRESULT hr = AVIFileCreateStream(file, &stream, &streaminfo);         
  if (hr != 0)
    return IM_ERR_ACCESS;

  /* set stream format */
  if (use_compressor)
  {
    if (!ICSeqCompressFrameStart(&compvars, dib->bmi))
      return IM_ERR_COMPRESS;

    hr = AVIStreamSetFormat(stream, 0, compvars.lpbiOut, dib->size - dib->bits_size); 
  }
  else
    hr = AVIStreamSetFormat(stream, 0, dib->dib, dib->size - dib->bits_size); 

  if (hr != 0)
    return IM_ERR_ACCESS;

  return IM_ERR_NONE;
}
Esempio n. 8
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;
}
Esempio n. 9
0
void PrintImageInfo(const char* file_name)
{
  printf("IM Info\n");
  printf("  File Name:\n    %s\n", file_name);

  int error;
  imFile* ifile = imFileOpen(file_name, &error);
  if (!ifile) 
  {
    PrintError(error);
    return;
  }

  double file_size = FileSize(file_name);
  printf("  File Size: %.2f %s\n", file_size, GetSizeDesc(&file_size));

  char format[10];
  char compression[20];
  int image_count;
  imFileGetInfo(ifile, format, compression, &image_count);

  char format_desc[50];
  imFormatInfo(format, format_desc, NULL, NULL);
  printf("  Format: %s - %s\n", format, format_desc);
  printf("  Compression: %s\n", compression);
  printf("  Image Count: %d\n", image_count);
  
  for (int i = 0; i < image_count; i++)
  {
    int width, height, color_mode, data_type;

    error = imFileReadImageInfo(ifile, i, &width, &height, &color_mode, &data_type);
    if (error != IM_ERR_NONE)
    {
      PrintError(error);
      imFileClose(ifile);  
      return;
    }

    printf("  Image #%d\n", i);
    printf("    Width: %d\n", width);
    printf("    Height: %d\n", height);
    printf("    Color Space: %s\n", imColorModeSpaceName(color_mode));
    printf("      Has Alpha: %s\n", imColorModeHasAlpha(color_mode)? "Yes": "No");
    printf("      Is Packed: %s\n", imColorModeIsPacked(color_mode)? "Yes": "No");
    printf("      Is Top Down: %s\n", imColorModeIsTopDown(color_mode)? "Yes": "No");
    printf("    Data Type: %s\n", imDataTypeName(data_type));

    double image_size = imImageDataSize(width, height, color_mode, data_type);
    printf("    Data Size: %.2f %s\n", image_size, GetSizeDesc(&image_size));

    char* attrib_list[50];  // should be dynamic allocated
    int attrib_list_count;
    imFileGetAttributeList(ifile, attrib_list, &attrib_list_count);

    for (int a = 0; a < attrib_list_count; a++)
    {
      if (a == 0)
        printf("    Attributes:\n");

      int attrib_data_type, attrib_count;
      const void* attrib_data = imFileGetAttribute(ifile, attrib_list[a], &attrib_data_type, &attrib_count);

      if (attrib_count == 1)
        printf("      %s: %s\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type));
      else if (attrib_data_type == IM_BYTE && FindZero((imbyte*)attrib_data, attrib_count))
        printf("      %s: %s\n", attrib_list[a], attrib_data);
      else
        printf("      %s: %s %s ...\n", attrib_list[a], AttribData2Str(attrib_data, attrib_data_type), AttribData2Str((imbyte*)attrib_data + imDataTypeSize(attrib_data_type), attrib_data_type));
    }
  }
    
  imFileClose(ifile);  
}