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

DLLEXPORT int DLLCALL tjDecompressToYUV ( tjhandle  handle,
unsigned char *  jpegBuf,
unsigned long  jpegSize,
unsigned char *  dstBuf,
int  flags 
)

Decompress a JPEG image to a YUV planar image. This function performs JPEG decompression but leaves out the color conversion step, so a planar YUV image is generated instead of an RGB image. The padding of the planes in this image is the same as the images generated by tjEncodeYUV2(). Note that, if the width or height of the image is not an even multiple of the MCU block size (see tjMCUWidth and tjMCUHeight), then an intermediate buffer copy will be performed within TurboJPEG.

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 YUV image. Use tjBufSizeYUV to determine the appropriate size for this buffer based on the image width, height, and level of subsampling.
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 803 of file turbojpeg.c.

References TJFLAG_FASTUPSAMPLE, TJFLAG_FORCEMMX, TJFLAG_FORCESSE, and TJFLAG_FORCESSE2.

{
      int i, row, retval=0;  JSAMPROW *outbuf[MAX_COMPONENTS];
      int cw[MAX_COMPONENTS], ch[MAX_COMPONENTS], iw[MAX_COMPONENTS],
            tmpbufsize=0, usetmpbuf=0, th[MAX_COMPONENTS];
      JSAMPLE *_tmpbuf=NULL, *ptr=dstBuf;  JSAMPROW *tmpbuf[MAX_COMPONENTS];

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

      for(i=0; i<MAX_COMPONENTS; i++)
      {
            tmpbuf[i]=NULL;  outbuf[i]=NULL;
      }

      if(jpegBuf==NULL || jpegSize<=0 || dstBuf==NULL)
            _throw("tjDecompressToYUV(): 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);

      for(i=0; i<dinfo->num_components; i++)
      {
            jpeg_component_info *compptr=&dinfo->comp_info[i];
            int ih;
            iw[i]=compptr->width_in_blocks*DCTSIZE;
            ih=compptr->height_in_blocks*DCTSIZE;
            cw[i]=PAD(dinfo->image_width, dinfo->max_h_samp_factor)
                  *compptr->h_samp_factor/dinfo->max_h_samp_factor;
            ch[i]=PAD(dinfo->image_height, dinfo->max_v_samp_factor)
                  *compptr->v_samp_factor/dinfo->max_v_samp_factor;
            if(iw[i]!=cw[i] || ih!=ch[i]) usetmpbuf=1;
            th[i]=compptr->v_samp_factor*DCTSIZE;
            tmpbufsize+=iw[i]*th[i];
            if((outbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*ch[i]))==NULL)
                  _throw("tjDecompressToYUV(): Memory allocation failure");
            for(row=0; row<ch[i]; row++)
            {
                  outbuf[i][row]=ptr;
                  ptr+=PAD(cw[i], 4);
            }
      }
      if(usetmpbuf)
      {
            if((_tmpbuf=(JSAMPLE *)malloc(sizeof(JSAMPLE)*tmpbufsize))==NULL)
                  _throw("tjDecompressToYUV(): Memory allocation failure");
            ptr=_tmpbuf;
            for(i=0; i<dinfo->num_components; i++)
            {
                  if((tmpbuf[i]=(JSAMPROW *)malloc(sizeof(JSAMPROW)*th[i]))==NULL)
                        _throw("tjDecompressToYUV(): Memory allocation failure");
                  for(row=0; row<th[i]; row++)
                  {
                        tmpbuf[i][row]=ptr;
                        ptr+=iw[i];
                  }
            }
      }

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

      jpeg_start_decompress(dinfo);
      for(row=0; row<(int)dinfo->output_height;
            row+=dinfo->max_v_samp_factor*DCTSIZE)
      {
            JSAMPARRAY yuvptr[MAX_COMPONENTS];
            int crow[MAX_COMPONENTS];
            for(i=0; i<dinfo->num_components; i++)
            {
                  jpeg_component_info *compptr=&dinfo->comp_info[i];
                  crow[i]=row*compptr->v_samp_factor/dinfo->max_v_samp_factor;
                  if(usetmpbuf) yuvptr[i]=tmpbuf[i];
                  else yuvptr[i]=&outbuf[i][crow[i]];
            }
            jpeg_read_raw_data(dinfo, yuvptr, dinfo->max_v_samp_factor*DCTSIZE);
            if(usetmpbuf)
            {
                  int j;
                  for(i=0; i<dinfo->num_components; i++)
                  {
                        for(j=0; j<min(th[i], ch[i]-crow[i]); j++)
                        {
                              memcpy(outbuf[i][crow[i]+j], tmpbuf[i][j], cw[i]);
                        }
                  }
            }
      }
      jpeg_finish_decompress(dinfo);

      bailout:
      if(dinfo->global_state>DSTATE_START) jpeg_abort_decompress(dinfo);
      for(i=0; i<MAX_COMPONENTS; i++)
      {
            if(tmpbuf[i]) free(tmpbuf[i]);
            if(outbuf[i]) free(outbuf[i]);
      }
      if(_tmpbuf) free(_tmpbuf);
      return retval;
}

Generated by  Doxygen 1.6.0   Back to index