Example #1
0
ALERROR JPEGLoadFromMemory (char *pImage, int iSize, DWORD dwFlags, HPALETTE hPalette, HBITMAP *rethBitmap)

//	JPEGLoadFromMemory
//
//	Load from a memory stream

	{
	ALERROR error;
	IJLERR jerr;

	//	Initialize library

	JPEG_CORE_PROPERTIES jcprops;
	jerr = ijlInit(&jcprops);
	if (jerr != IJL_OK)
		return ERR_FAIL;

	//	Point to the buffer

	jcprops.JPGFile = NULL;
	jcprops.JPGBytes = (BYTE *)pImage;
	jcprops.JPGSizeBytes = iSize;

	//	Read parameters

	jerr = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
	if (jerr != IJL_OK)
		{
		ijlFree(&jcprops);
		return ERR_FAIL;
		}

	DWORD width = jcprops.JPGWidth;
	DWORD height = jcprops.JPGHeight;
	DWORD nchannels = 3;	//	24-bits
	DWORD dib_line_width = width * nchannels;
	DWORD dib_pad_bytes = IJL_DIB_PAD_BYTES(width,nchannels);

	//	Allocate a dib

	HBITMAP hBitmap;
	BYTE *p24BitPixel;
	if (error = dibCreate24bitDIB(width,
			height,
			0,
			&hBitmap,
			&p24BitPixel))
		{
		ijlFree(&jcprops);
		return error;
		}

	//	Setup the library to load into the dib format

	jcprops.DIBWidth = width;
	jcprops.DIBHeight = -(int)height;
	jcprops.DIBChannels = nchannels;
	jcprops.DIBColor = IJL_BGR;
	jcprops.DIBPadBytes = dib_pad_bytes;
	jcprops.DIBBytes = p24BitPixel;

	//	Setup the color space

	switch (jcprops.JPGChannels)
		{
		case 1:
			jcprops.JPGColor = IJL_G;
			break;

		case 3:
			jcprops.JPGColor = IJL_YCBCR;
			break;

		default:
			{
			jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
			jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;
			}
		}

	//	Load the data in the buffer

	jerr = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
	if (jerr != IJL_OK)
		{
		::DeleteObject(hBitmap);
		ijlFree(&jcprops);
		return ERR_FAIL;
		}

	//	Done

	ijlFree(&jcprops);

	//	If we want a DIB, just return that; otherwise,
	//	convert to DDB and return that

	if (dwFlags & JPEG_LFR_DIB)
		*rethBitmap = hBitmap;
	else
		{
		HBITMAP hDDB;
		error = dibConvertToDDB(hBitmap, hPalette, &hDDB);
		::DeleteObject(hBitmap);
		if (error)
			return error;

		*rethBitmap = hDDB;
		}

	return NOERROR;
	}
	/////////////////////////////////////////
	// Texture::ImageJPG::Load
	void Texture::ImageJPG::Load(const char *filename)
	{
	throw Daher::Exception("Cannot load image: IJL initialization failed");
#if 0
		JPEG_CORE_PROPERTIES		image;
		unsigned char				*imageBits = NULL;		// the bits, before swapping
		int							imageSize = 0;
		
		DGL::LogPrint("Loading JPEG Image '%s' ...",filename);
		ZeroMemory( &image, sizeof( JPEG_CORE_PROPERTIES ) );
		// try to init the image 'container'
		if( ijlInit( &image ) != IJL_OK )
			throw Daher::Exception("Cannot load image: IJL initialization failed");

		image.JPGFile = const_cast<char*>(filename);
		// try to read the params from the file
		if( ijlRead( &image, IJL_JFILE_READPARAMS ) != IJL_OK )
			throw Daher::Exception("File Not Found");

		// check info about the channels
		switch(image.JPGChannels) {
			case 1:
				image.JPGColor = IJL_G;
				break;
			case 3:
				image.JPGColor = IJL_YCBCR;
				break;
			default:
				// This catches everything else, but no
				// color twist will be performed by the IJL.
				image.DIBColor = (IJL_COLOR)IJL_OTHER;
				image.JPGColor = (IJL_COLOR)IJL_OTHER;
				break;
		}
		image.DIBWidth    = image.JPGWidth;
		image.DIBHeight   = image.JPGHeight;
		image.DIBChannels = 3;
		image.DIBPadBytes = IJL_DIB_PAD_BYTES(image.DIBWidth,image.DIBChannels);
		imageSize = (image.DIBWidth * image.DIBChannels + image.DIBPadBytes) * image.DIBHeight;
		// allocate memory to store the image unsigned chars (the info OGL wants)
		imageBits = new unsigned char[ imageSize ];
		this->data = new unsigned char[ imageSize ];

		image.DIBBytes = imageBits;
		// Read the image unsigned chars from the image
		if( ijlRead( &image, IJL_JFILE_READWHOLEIMAGE ) != IJL_OK ) {
			delete [] imageBits;
			delete [] this->data;
			this->data = NULL;
			return;
		}
		// free the image container
		if( ijlFree( &image ) != IJL_OK ) 
			throw Daher::Exception("Cannot Free IJL Container");

		this->width	= image.DIBWidth;
		this->height = image.DIBHeight;
		// the JPG stores its image unsigned chars in a different order, instead of
		// going from lower to higher lines it goes higher to lower.
		// Use imageBits2 after this loop.
		for (int j = 0;j < this->height; j++) {
			for (int i = 0;i < this->width; i++) {
				this->data[(j * this->width+i)* 3+ this->Red] = imageBits[((this->height-1-j) * this->width+i)* 3+ this->Blue];
				this->data[(j * this->width+i)* 3+ this->Green] = imageBits[((this->height-1-j) * this->width+i)* 3+ this->Green];
				this->data[(j * this->width+i)* 3+ this->Blue] = imageBits[((this->height-1-j) * this->width+i)* 3+ this->Red];
			}
		}
		// free memory
		delete [] imageBits;
#endif
	}