Click to See Complete Forum and Search --> : Jpeg


Fatboy
April 19th, 2005, 12:48 PM
How do you manipulate JPEG files in a program, for example to draw it on to a client window, without converting into BITMAP? Just so that my resources don't take up too much space.

Don Reba
April 20th, 2005, 11:54 AM
You have to decode the image to draw it. This is especially true for JPEG. JPEG2000, on the other hand, can be decoded just partially before being drawn. With JPEG GDI+ can handle almost all of the work for you.

Fatboy
April 20th, 2005, 03:13 PM
How do I decode it manually? Would it be a stupid thing to do? If it is, could you tell me a library that has built in functions to decode the required Jpeg?

I'd still appreciate a brief description of the bit-operations required to decode a Jpeg though.

Deception666
April 21st, 2005, 01:55 PM
Here is some code that allows you to read in a JPEG file. It is not pretty and it may not be the fastest code you have seen, but it will do what you want. pImage is just a buffer of RGBA values. The library that was used is jpeglib. Go here for more info. http://www.ijg.org/

bool JPGReader::Read( std::string &rFile )
{
// local(s)
bool bRead = false;
FILE *pFile = NULL;

// save the file name
m_sFileName = rFile;

// open a file to be read
if (pFile = fopen(rFile.c_str(), "rb"))
{
// local(s)
jpeg_decompress_struct jdsInfo;
jpeg_error_mgr jemErr;
unsigned int nImageSize;

// have the decompression object point to the error object
jdsInfo.err = jpeg_std_error(&jemErr);
// initialize the decompression object
jpeg_create_decompress(&jdsInfo);
// set the data source
jpeg_stdio_src(&jdsInfo, pFile);
// read the header
jpeg_read_header(&jdsInfo, TRUE);

// make sure that the output color format is RGB
if (jdsInfo.out_color_space == JCS_RGB)
{
// local(s)
unsigned char *pScanLine;
Image *pImage;

// start decompressing the data
jpeg_start_decompress(&jdsInfo);
// determine file attributes
m_oSize.m_nWidth = jdsInfo.image_width;
m_oSize.m_nHeight = jdsInfo.image_height;
// determine the file image size
nImageSize = m_oSize.m_nWidth * m_oSize.m_nHeight;
// create a new image
m_pImage = new Image[nImageSize];
// create the scan line buffer
pScanLine = new unsigned char[jdsInfo.image_width * jdsInfo.num_components];
// read the image
while (jdsInfo.output_scanline < jdsInfo.output_height)
{
// local(s)
unsigned char *pImageBuf = pScanLine;

// read line by line
jpeg_read_scanlines(&jdsInfo, &pScanLine, 1);
// image is read top to bottom
// this needs to be converted to a bottom to top
pImage = m_pImage + (m_oSize.m_nWidth *
(jdsInfo.output_height - jdsInfo.output_scanline));
// push the data into internal data
for (unsigned int i = 0;
i < jdsInfo.image_width;
i++)
{
// set the alpha value to 0x00
pImage->m_nAlpha = 0xFF;
// set the RBG values
memcpy(pImage, pImageBuf, 3);
// increase the pointers
pImageBuf += 3;
pImage++;
}
}

// finish decompressing the data
jpeg_finish_decompress(&jdsInfo);
// delete the scan line buffer
delete [] pScanLine;
// release the internal memory stored by the jpeg object
jpeg_destroy_decompress(&jdsInfo);
// set the flag
bRead = true;
}

// close the file
fclose(pFile);
}

return bRead;
}

alxs
July 21st, 2005, 06:38 PM
Maybe an easy-to-use JPEG2000 decoding component is what you need? ;)

Take a look: http://j2k-codec.com

--
Alex Saveliev

golanshahar
July 21st, 2005, 06:57 PM
for displaying Jpeg file you can use the IPicture (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/42e3cd0e-2413-494a-8be8-2952089e02d2.asp) com object and use the Render (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/42e3cd0e-2413-494a-8be8-2952089e02d2.asp) function to display the jpeg to the window dc. ( its part of the SDK )

now if you want you can insert the Jpeg Images to the resources of your app (under custom resource)
and to get it in runtime you need to

::FindResource(..)
::LoadResource(..)
::CreateStreamOnHGlobal(..)
::SizeofResource(..)
::OleLoadPicture(..)
use the IPicture interferface with Render(..)


Cheers