Logo Search packages:      
Sourcecode: libjpeg-turbo version File versions  Download package

DLLEXPORT int DLLCALL tjDecompress2 ( tjhandle  handle,
unsigned char *  jpegBuf,
unsigned long  jpegSize,
unsigned char *  dstBuf,
int  width,
int  pitch,
int  height,
int  pixelFormat,
int  flags 
)

Decompress a JPEG image to an RGB or grayscale image.

Parameters:
handlea handle to a TurboJPEG decompressor or transformer instance
jpegBufpointer to a buffer containing the JPEG image to decompress
jpegSizesize of the JPEG image (in bytes)
dstBufpointer to an image buffer which will receive the decompressed image. This buffer should normally be pitch * scaledHeight bytes in size, where scaledHeight can be determined by calling TJSCALED() with the JPEG image height and one of the scaling factors returned by tjGetScalingFactors(). The dstBuf pointer may also be used to decompress into a specific region of a larger buffer.
widthdesired width (in pixels) of the destination image. If this is smaller than the width of the JPEG image being decompressed, then TurboJPEG will use scaling in the JPEG decompressor to generate the largest possible image that will fit within the desired width. If width is set to 0, then only the height will be considered when determining the scaled image size.
pitchbytes per line of the destination image. Normally, this is scaledWidth * tjPixelSize[pixelFormat] if the decompressed image is unpadded, else TJPAD(scaledWidth * tjPixelSize[pixelFormat]) if each line of the decompressed image is padded to the nearest 32-bit boundary, as is the case for Windows bitmaps. (NOTE: scaledWidth can be determined by calling TJSCALED() with the JPEG image width and one of the scaling factors returned by tjGetScalingFactors().) You can also be clever and use the pitch parameter to skip lines, etc. Setting this parameter to 0 is the equivalent of setting it to scaledWidth * tjPixelSize[pixelFormat].
heightdesired height (in pixels) of the destination image. If this is smaller than the height of the JPEG image being decompressed, then TurboJPEG will use scaling in the JPEG decompressor to generate the largest possible image that will fit within the desired height. If height is set to 0, then only the width will be considered when determining the scaled image size.
pixelFormatpixel format of the destination image (see Pixel formats.)
flagsthe bitwise OR of one or more of the flags.
Returns:
0 if successful, or -1 if an error occurred (see tjGetErrorStr().)

Definition at line 719 of file turbojpeg.c.

References tjscalingfactor::denom, tjscalingfactor::num, TJ_NUMPF, TJFLAG_BOTTOMUP, TJFLAG_FASTUPSAMPLE, TJFLAG_FORCEMMX, TJFLAG_FORCESSE, TJFLAG_FORCESSE2, tjPixelSize, and TJSCALED.

{
      int i, retval=0;  JSAMPROW *row_pointer=NULL;
      int jpegwidth, jpegheight, scaledw, scaledh;

      getinstance(handle);
      if((this->init&DECOMPRESS)==0)
            _throw("tjDecompress2(): Instance has not been initialized for decompression");

      if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL || width<0 || pitch<0
            || height<0 || pixelFormat<0 || pixelFormat>=TJ_NUMPF)
            _throw("tjDecompress2(): Invalid argument");

      if(flags&TJFLAG_FORCEMMX) putenv("JSIMD_FORCEMMX=1");
      else if(flags&TJFLAG_FORCESSE) putenv("JSIMD_FORCESSE=1");
      else if(flags&TJFLAG_FORCESSE2) putenv("JSIMD_FORCESSE2=1");

      if(setjmp(this->jerr.setjmp_buffer))
      {
            /* If we get here, the JPEG code has signaled an error. */
            retval=-1;
            goto bailout;
      }

      jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
      jpeg_read_header(dinfo, TRUE);
      if(setDecompDefaults(dinfo, pixelFormat)==-1) return -1;

      if(flags&TJFLAG_FASTUPSAMPLE) dinfo->do_fancy_upsampling=FALSE;

      jpegwidth=dinfo->image_width;  jpegheight=dinfo->image_height;
      if(width==0) width=jpegwidth;
      if(height==0) height=jpegheight;
      for(i=0; i<NUMSF; i++)
      {
            scaledw=TJSCALED(jpegwidth, sf[i]);
            scaledh=TJSCALED(jpegheight, sf[i]);
            if(scaledw<=width && scaledh<=height)
                        break;
      }
      if(scaledw>width || scaledh>height)
            _throw("tjDecompress2(): Could not scale down to desired image dimensions");
      width=scaledw;  height=scaledh;
      dinfo->scale_num=sf[i].num;
      dinfo->scale_denom=sf[i].denom;

      jpeg_start_decompress(dinfo);
      if(pitch==0) pitch=dinfo->output_width*tjPixelSize[pixelFormat];
      if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)
            *dinfo->output_height))==NULL)
            _throw("tjDecompress2(): Memory allocation failure");
      for(i=0; i<(int)dinfo->output_height; i++)
      {
            if(flags&TJFLAG_BOTTOMUP)
                  row_pointer[i]=&dstBuf[(dinfo->output_height-i-1)*pitch];
            else row_pointer[i]=&dstBuf[i*pitch];
      }
      while(dinfo->output_scanline<dinfo->output_height)
      {
            jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
                  dinfo->output_height-dinfo->output_scanline);
      }
      jpeg_finish_decompress(dinfo);

      bailout:
      if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
      if(row_pointer) free(row_pointer);
      return retval;
}

Generated by  Doxygen 1.6.0   Back to index