CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2011

    Question questions about libjpeg from IJG


    I'm using the jpeg library from IJG in order to compress the raw data from usb camera and decompress and display it at the end of my procedure.
    I hope that there are some of you, who has used the libjpeg and knows more about it...

    Question: Do I have to create the compressor (jpeg_create_compress(&cinfo) ) for each incoming frame and destroy the jpeg compressor afterwards or is there a possibility to store the compressor object? I guess it is not efficient to do that initialization and destroying steps for every frame, which appears with a frequency of more than 25 fps.
    Actually, I have tried already to implement it in that way that the compressor, decompressor and their error manager are defined as member variables of my class. These are then initialized in the initialization function of my class, that is
               unsigned long  size = (m_nSizeX * m_nSizeY * m_nBitsPerPixel)/8;
    	//============== compressor ========================
    	// global allocation of memory (in bytes) for the array with compressed data
    	 hglobMemforCompressedJpegData = GlobalAlloc(GMEM_MOVEABLE, size);
    	 m_compressedDataArray = (unsigned char*) GlobalLock(hglobMemforCompressedJpegData); 
            m_compressInfo.err = jpeg_std_error(&m_jpegErrMgrForCompression);
    	//// initialize the rest of the JPEG object ( e.g. allocates a small amount of memory )
    	//// Specify the destination for the compressed data, here it is stored in memory buffer
    	jpeg_mem_dest(&m_compressInfo, &m_compressedDataArray, &(size));
    	//============== decompressor ========================
    	// global allocation of memory (in bytes) for the array with decompressed data
    	hglobMemforDecompressedJpegData = GlobalAlloc(GMEM_MOVEABLE, size);
    	m_decompressedDataArray = (unsigned char*)      GlobalLock(hglobMemforDecompressedJpegData); 
    	//// define an error handler for the decompression struct
    	m_decompressInfo.err = jpeg_std_error(&m_jpegErrMgrForDecompression);
    	//// Specify the source for the decompressed data, here it is stored in memory buffer
    	jpeg_mem_src(&m_decompressInfo, m_compressedDataArray,size);
    every time if a new frame is capture by the camera, the function for compression is called, where I start the compression:

    // set parameters for compression, including image size & colorspace
    	m_compressInfo.image_width      = m_nSizeX;
    	m_compressInfo.image_height     = m_nSizeY;
    	m_compressInfo.input_components = 3;
    	m_compressInfo.in_color_space   =  JCS_YCbCr;
    	/* Make optional parameter settings here, e.g. specify the quality */
    	/*set the quality [0..100]  */
    	jpeg_set_quality (&m_compressInfo, quality, true);
    	// begin a compression cycle. This will initialize internal state, allocate working storage,
    	// and emit the first few bytes of the JPEG datastream header.
    	jpeg_start_compress(&m_compressInfo, true);
    	JSAMPROW row_pointer[1];        /* pointer to a single row */
    	int row_stride;                 /* physical row width in buffer */
    	row_stride = m_compressInfo.image_width * 3;   /* JSAMPLEs per row in image_buffer */
    	unsigned char* copyPointer =(unsigned char*) m_pcImageMemory;
    	// compress the raw data row by row
    	while (m_compressInfo.next_scanline < m_compressInfo.image_height)
    		// define the next row of raw data
    		row_pointer[0]  =  copyPointer;
    		jpeg_write_scanlines(&m_compressInfo, row_pointer, 1);
    		copyPointer += row_stride;
    	/*complete the compression cycle. */
    But I don't call the destruction of the compressor ( jpeg_destroy_compress).
    It looks similarly with the decompression function.
    The problem is, if I do so -> the first frame is compressed and decompressed correctly, but the second frame causes an error in the decompressor at the step jpeg_read_header().

    Is my idea wrong? Is it possible to use libjpeg without creating compressor for each frame?

    P.S. If I create the compressor and decompressor for each frame, then there is no exception/error anymore and the frames are displayed after compression/decompression method correctly, BUT the application seems to be freezed, because no control buttons on my MFC application can be used anymore and I cannot exit the application, too. May be , you know the solution for this behaviour?

    best regards,

    Last edited by vanselm; May 5th, 2011 at 09:54 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)