//------------------------------------------------------------------------ // JpegEncoder::InitCompressor //------------------------------------------------------------------------ void JpegEncoder::InitCompressor(MemBufPtr ptrmbSrc, struct jpeg_compress_struct *pcinfo) { int iWidth, iHeight; try { ptrmbSrc->GetIntegerMetadata("width", &iWidth); ptrmbSrc->GetIntegerMetadata("height", &iHeight); } catch( NoDataException e ) { // Transform exception into a jpeg exception throw JpegEncoder::Exception(PSZ(e.Error()), PSZ(e.Reason()), "JpegEncoder::InitCompressor"); } if( iWidth < 1 ||iWidth > JPEG_MAX_DIMENSION ) throw JpegEncoder::Exception("BAD_PARAMETER", "Wrongs image dimensions. Must be in [1, 65000]", "JpegEncoder::Encode"); // create context jpeg_create_compress(pcinfo); // Put cinfo values pcinfo->image_width = iWidth; pcinfo->image_height = iHeight; pcinfo->input_components = 1; // grayscale image pcinfo->in_color_space = JCS_GRAYSCALE; // Set compression parameters to default values jpeg_set_defaults(pcinfo); // Set quality to max jpeg_set_quality(pcinfo, 100, TRUE); }
//------------------------------------------------------------------------ // BmpEncoder::EncodeGrayScaleToMemBuf //------------------------------------------------------------------------ void BmpEncoder::EncodeGrayScaleToMemBuf(MemBufPtr ptrmbSrc, MemBufPtr ptrmbDst) { int iWidth, iHeight; try { ptrmbSrc->GetIntegerMetadata("width", &iWidth); ptrmbSrc->GetIntegerMetadata("height", &iHeight); } catch( NoDataException e ) { // Transform exception into a jpeg exception throw BmpEncoder::Exception(PSZ(e.Error()), PSZ(e.Reason()), "BmpEncoder::EncodeGrayScaleToMemBuf"); } if( iWidth < 1 || iWidth > BMP_MAX_DIMENSION ) throw Exception("BAD_PARAMETER", "Wrongs image dimensions. Must be in [1, 65000]", "BmpEncoder::EncodeGrayScaleToMemBuf"); int iBytePaddingPerRow = 0; EncodeHeader(ptrmbDst, 8, iWidth, iHeight, &iBytePaddingPerRow); // Image data for( int y = 0; y < iHeight; y++ ) { ptrmbDst->PutBloc((uint8 *)(ptrmbSrc->Buf()) + (iHeight - y - 1) * iWidth, iWidth); for( int i = 0; i < iBytePaddingPerRow; i++ ) (*ptrmbDst) << (uint8)0; } }
//------------------------------------------------------------------------ // JpegEncoder::Compress //------------------------------------------------------------------------ void JpegEncoder::Compress(MemBufPtr ptrmbSrc, struct jpeg_compress_struct *pcinfo) { // Start compressor jpeg_start_compress(pcinfo, TRUE); JSAMPROW *row_pointer = new JSAMPROW[pcinfo->image_height]; for( uint ui = 0; ui < pcinfo->image_height; ui++ ) row_pointer[ui] = (unsigned char *)(ptrmbSrc->Buf() + ui * pcinfo->image_width); // Write all rows in one single pass jpeg_write_scanlines(pcinfo, row_pointer, pcinfo->image_height); // Terminate encoding process jpeg_finish_compress(pcinfo); jpeg_destroy_compress(pcinfo); }