示例#1
0
void 
GBitmap::init(ByteStream &ref, int aborder)
{
  GMonitorLock lock(monitor());
  // Get magic number
  char magic[2];
  magic[0] = magic[1] = 0;
  ref.readall((void*)magic, sizeof(magic));
  char lookahead = '\n';
  int acolumns = read_integer(lookahead, ref);
  int arows = read_integer(lookahead, ref);
  init(arows, acolumns, aborder);
  // go reading file
  if (magic[0]=='P')
    {
      switch(magic[1])
        {
        case '1':
          grays = 2;
          read_pbm_text(ref); 
          return;
        case '2':
          grays = 1 + read_integer(lookahead, ref);
          if (grays > 256)
            G_THROW("Cannot read PGM with depth greater than 8 bits.");
          read_pgm_text(ref); 
          return;
        case '4':
          grays = 2;
          read_pbm_raw(ref); 
          return;
        case '5':
          grays = 1 + read_integer(lookahead, ref);
          if (grays > 256)
            grays = 256;
          read_pgm_raw(ref); 
          return;
        }
    }
  else if (magic[0]=='R')
    {
      switch(magic[1])
        {
        case '4':
          grays = 2;
          read_rle_raw(ref); 
          return;
        }
    }
  G_THROW( ERR_MSG("GBitmap.bad_format") );
}
示例#2
0
void 
GBitmap::init(ByteStream &ref, int aborder)
{
  GMonitorLock lock(monitor());
#if HAVE_FREEIMAGE
	FIBITMAP *dib = ref.fiLoadImage(0);
	if (dib == NULL)
		G_THROW("File format unrecognized by FreeImage.");

	{
		FIBITMAP *dib2 = FreeImage_ConvertToGreyscale(dib);
		FreeImage_Unload(dib);
		if (dib2 == NULL) {
			G_THROW("Failed to convert the bitmap to grayscale bitmap.");
		}
		dib = dib2;
	}

	// Read image size
	int acolumns = FreeImage_GetWidth(dib);
	int arows = FreeImage_GetHeight(dib);
	init(arows, acolumns, aborder);

	// Returns the width of the bitmap in bytes, rounded to the next 32-bit boundary,
	// also known as "pitch" or "stride" or "scan width".
	unsigned pitch = FreeImage_GetPitch(dib);

	const unsigned char* bits = (const unsigned char*)FreeImage_GetBits(dib); // the pointer to the 1 pixel

	// Read image data
	grays = 256;
	unsigned char *row = bytes_data + border;
	for (int y = 0; y < nrows; y++) {
		const unsigned char *rgb = bits + y*pitch;

		for (int x = 0; x < ncolumns; x++) {
			row[x] = 255 - rgb[x];
		}

		row += bytes_per_row;
	}

	FreeImage_Unload(dib);
#else
  // Get magic number
  char magic[2];
  magic[0] = magic[1] = 0;
  ref.readall((void*)magic, sizeof(magic));
  char lookahead = '\n';
  int acolumns = read_integer(lookahead, ref);
  int arows = read_integer(lookahead, ref);
  int maxval = 1;
  init(arows, acolumns, aborder);
  // go reading file
  if (magic[0]=='P')
    {
      switch(magic[1])
        {
        case '1':
          grays = 2;
          read_pbm_text(ref); 
          return;
        case '2':
          maxval = read_integer(lookahead, ref);
          if (maxval > 65535)
            G_THROW("Cannot read PGM with depth greater than 16 bits.");
          grays = (maxval>255 ? 256 : maxval+1);
          read_pgm_text(ref, maxval); 
          return;
        case '4':
          grays = 2;
          read_pbm_raw(ref); 
          return;
        case '5':
          maxval = read_integer(lookahead, ref);
          if (maxval > 65535)
            G_THROW("Cannot read PGM with depth greater than 16 bits.");
          grays = (maxval>255 ? 256 : maxval+1);
          read_pgm_raw(ref, maxval); 
          return;
        }
    }
  else if (magic[0]=='R')
    {
      switch(magic[1])
        {
        case '4':
          grays = 2;
          read_rle_raw(ref); 
          return;
        }
    }
  G_THROW( ERR_MSG("GBitmap.bad_format") );
#endif
}