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

DLLEXPORT int DLLCALL tjCompress2 ( tjhandle  handle,
unsigned char *  srcBuf,
int  width,
int  pitch,
int  height,
int  pixelFormat,
unsigned char **  jpegBuf,
unsigned long *  jpegSize,
int  jpegSubsamp,
int  jpegQual,
int  flags 
)

Compress an RGB or grayscale image into a JPEG image.

Parameters:
handlea handle to a TurboJPEG compressor or transformer instance
srcBufpointer to an image buffer containing RGB or grayscale pixels to be compressed
widthwidth (in pixels) of the source image
pitchbytes per line of the source image. Normally, this should be width * tjPixelSize[pixelFormat] if the image is unpadded, or TJPAD(width * tjPixelSize[pixelFormat]) if each line of the image is padded to the nearest 32-bit boundary, as is the case for Windows bitmaps. You can also be clever and use this parameter to skip lines, etc. Setting this parameter to 0 is the equivalent of setting it to width * tjPixelSize[pixelFormat].
heightheight (in pixels) of the source image
pixelFormatpixel format of the source image (see Pixel formats.)
jpegBufaddress of a pointer to an image buffer that will receive the JPEG image. TurboJPEG has the ability to reallocate the JPEG buffer to accommodate the size of the JPEG image. Thus, you can choose to:

  1. pre-allocate the JPEG buffer with an arbitrary size using tjAlloc() and let TurboJPEG grow the buffer as needed,
  2. set *jpegBuf to NULL to tell TurboJPEG to allocate the buffer for you, or
  3. pre-allocate the buffer to a "worst case" size determined by calling tjBufSize(). This should ensure that the buffer never has to be re-allocated (setting TJFLAG_NOREALLOC guarantees this.)

If you choose option 1, *jpegSize should be set to the size of your pre-allocated buffer. In any case, unless you have set TJFLAG_NOREALLOC, you should always check *jpegBuf upon return from this function, as it may have changed.

jpegSizepointer to an unsigned long variable which holds the size of the JPEG image buffer. If *jpegBuf points to a pre-allocated buffer, then *jpegSize should be set to the size of the buffer. Upon return, *jpegSize will contain the size of the JPEG image (in bytes.)
jpegSubsampthe level of chrominance subsampling to be used when generating the JPEG image (see Chrominance subsampling options.)
jpegQualthe image quality of the generated JPEG image (1 = worst, 100 = best)
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 397 of file turbojpeg.c.

References TJ_NUMPF, tjBufSize(), TJFLAG_BOTTOMUP, TJFLAG_FORCEMMX, TJFLAG_FORCESSE, TJFLAG_FORCESSE2, TJFLAG_NOREALLOC, and tjPixelSize.

{
      int i, retval=0, alloc=1;  JSAMPROW *row_pointer=NULL;

      getinstance(handle)
      if((this->init&COMPRESS)==0)
            _throw("tjCompress2(): Instance has not been initialized for compression");

      if(srcBuf==NULL || width<=0 || pitch<0 || height<=0 || pixelFormat<0
            || pixelFormat>=TJ_NUMPF || jpegBuf==NULL || jpegSize==NULL
            || jpegSubsamp<0 || jpegSubsamp>=NUMSUBOPT || jpegQual<0 || jpegQual>100)
            _throw("tjCompress2(): Invalid argument");

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

      if(pitch==0) pitch=width*tjPixelSize[pixelFormat];

      cinfo->image_width=width;
      cinfo->image_height=height;

      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(flags&TJFLAG_NOREALLOC)
      {
            alloc=0;  *jpegSize=tjBufSize(width, height, jpegSubsamp);
      }
      jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
      if(setCompDefaults(cinfo, pixelFormat, jpegSubsamp, jpegQual)==-1)
            return -1;

      jpeg_start_compress(cinfo, TRUE);
      if((row_pointer=(JSAMPROW *)malloc(sizeof(JSAMPROW)*height))==NULL)
            _throw("tjCompress2(): Memory allocation failure");
      for(i=0; i<height; i++)
      {
            if(flags&TJFLAG_BOTTOMUP) row_pointer[i]=&srcBuf[(height-i-1)*pitch];
            else row_pointer[i]=&srcBuf[i*pitch];
      }
      while(cinfo->next_scanline<cinfo->image_height)
      {
            jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
                  cinfo->image_height-cinfo->next_scanline);
      }
      jpeg_finish_compress(cinfo);

      bailout:
      if(cinfo->global_state>CSTATE_START) jpeg_abort_compress(cinfo);
      if(row_pointer) free(row_pointer);
      return retval;
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index