Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/FreeImage/PluginSGI.cpp  view on Meta::CPAN


typedef struct tagSGIHeader {
	/** IRIS image file magic number. This should be decimal 474. */
	WORD magic;
	/** Storage format: 0 for uncompressed, 1 for RLE compression. */
	BYTE storage;
	/** Number of bytes per pixel channel. Legally 1 or 2. */
	BYTE bpc;
	/**
	Number of dimensions. Legally 1, 2, or 3. 
	1 means a single row, XSIZE long
	2 means a single 2D image
	3 means multiple 2D images
	*/
	WORD dimension;	
	/** X size in pixels */
	WORD xsize;
	/** Y size in pixels */
	WORD ysize;
	/**
	Number of channels. 
	1 indicates greyscale
	3 indicates RGB
	4 indicates RGB and Alpha
	*/
	WORD zsize;
	/** Minimum pixel value. This is the lowest pixel value in the image.*/
	LONG pixmin;
	/** Maximum pixel value. This is the highest pixel value in the image.*/
	LONG pixmax;
	/** Ignored. Normally set to 0. */
	char dummy[4];
	/** Image name. Must be null terminated, therefore at most 79 bytes. */
	char imagename[80];
	/** 
	Colormap ID. 
	0 - normal mode
	1 - dithered, 3 mits for red and green, 2 for blue, obsolete
	2 - index colour, obsolete
	3 - not an image but a colourmap
	*/
	LONG colormap;
	/** Ignored. Should be set to 0, makes the header 512 bytes. */
	char reserved[404];
} SGIHeader;

typedef struct tagRLEStatus {
  int cnt;
  int val;
} RLEStatus;

#ifdef _WIN32
#pragma pack(pop)
#else
#pragma pack()
#endif

static const char *SGI_LESS_THAN_HEADER_LENGTH = "Incorrect header size";
static const char *SGI_16_BIT_COMPONENTS_NOT_SUPPORTED = "No 16 bit support";
static const char *SGI_COLORMAPS_NOT_SUPPORTED = "No colormap support";
static const char *SGI_EOF_IN_RLE_INDEX = "EOF in run length encoding";
static const char *SGI_EOF_IN_IMAGE_DATA = "EOF in image data";
static const char *SGI_INVALID_CHANNEL_COUNT = "Invalid channel count";

// ==========================================================
// Plugin Interface
// ==========================================================

static int s_format_id;

// ==========================================================
// Plugin Implementation
// ==========================================================

#ifndef FREEIMAGE_BIGENDIAN
static void 
SwapHeader(SGIHeader *header) {
	SwapShort(&header->magic);
	SwapShort(&header->dimension);
	SwapShort(&header->xsize);
	SwapShort(&header->ysize);
	SwapShort(&header->zsize);
	SwapLong((DWORD*)&header->pixmin);
	SwapLong((DWORD*)&header->pixmax);
	SwapLong((DWORD*)&header->colormap);
}
#endif

static int 
get_rlechar(FreeImageIO *io, fi_handle handle, RLEStatus *pstatus) {
	if (!pstatus->cnt) {
		int cnt = 0;
		while (0 == cnt) {
			BYTE packed = 0;
			if(io->read_proc(&packed, sizeof(BYTE), 1, handle) < 1) {
				return EOF;
			}
			cnt = packed;
		}
		if (cnt == EOF) {
			return EOF;
		}
		pstatus->cnt = cnt & 0x7F;
		if (cnt & 0x80) {
			pstatus->val = -1;
		} else {
			BYTE packed = 0;
			if(io->read_proc(&packed, sizeof(BYTE), 1, handle) < 1) {
				return EOF;
			}
			pstatus->val = packed;
		}
	}
	pstatus->cnt--;
	if (pstatus->val == -1) {
		BYTE packed = 0;
		if(io->read_proc(&packed, sizeof(BYTE), 1, handle) < 1) {
			return EOF;
		}
		return packed;
	}



( run in 0.531 second using v1.01-cache-2.11-cpan-119454b85a5 )