Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions
nsJPEGEncoder Class Reference

#include <nsJPEGEncoder.h>

Inheritance diagram for nsJPEGEncoder:
Inheritance graph
[legend]
Collaboration diagram for nsJPEGEncoder:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_IMGIENCODER
NS_DECL_NSIINPUTSTREAM 
nsJPEGEncoder ()
void encodeClipboardImage (in nsIClipboardImage aClipboardImage, out nsIFile aImageFile)

Protected Member Functions

void ConvertHostARGBRow (const PRUint8 *aSrc, PRUint8 *aDest, PRUint32 aPixelWidth)
void StripAlpha (const PRUint8 *aSrc, PRUint8 *aDest, PRUint32 aPixelWidth)

Static Protected Member Functions

static void initDestination (jpeg_compress_struct *cinfo)
static boolean emptyOutputBuffer (jpeg_compress_struct *cinfo)
static void termDestination (jpeg_compress_struct *cinfo)
static void errorExit (jpeg_common_struct *cinfo)

Protected Attributes

PRUint8mImageBuffer
PRUint32 mImageBufferSize
PRUint32 mImageBufferUsed
PRUint32 mImageBufferReadPoint

Private Member Functions

 ~nsJPEGEncoder ()

Detailed Description

Definition at line 63 of file nsJPEGEncoder.h.


Constructor & Destructor Documentation

Definition at line 64 of file nsJPEGEncoder.cpp.


Member Function Documentation

void nsJPEGEncoder::ConvertHostARGBRow ( const PRUint8 aSrc,
PRUint8 aDest,
PRUint32  aPixelWidth 
) [protected]

Definition at line 267 of file nsJPEGEncoder.cpp.

{
  for (PRUint32 x = 0; x < aPixelWidth; x ++) {
    const PRUint32& pixelIn = ((const PRUint32*)(aSrc))[x];
    PRUint8 *pixelOut = &aDest[x * 3];

    PRUint8 alpha = (pixelIn & 0xff000000) >> 24;
    if (alpha == 0) {
      pixelOut[0] = pixelOut[1] = pixelOut[2] = 0;
    } else {
      pixelOut[0] = (((pixelIn & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
      pixelOut[1] = (((pixelIn & 0x00ff00) >>  8) * 255 + alpha / 2) / alpha;
      pixelOut[2] = (((pixelIn & 0x0000ff) >>  0) * 255 + alpha / 2) / alpha;
    }
  }
}

Definition at line 337 of file nsJPEGEncoder.cpp.

{
  nsJPEGEncoder* that = NS_STATIC_CAST(nsJPEGEncoder*, cinfo->client_data);
  NS_ASSERTION(that->mImageBuffer, "No buffer to empty!");

  that->mImageBufferUsed = that->mImageBufferSize;

  // expand buffer, just double size each time
  that->mImageBufferSize *= 2;

  PRUint8* newBuf = (PRUint8*)PR_Realloc(that->mImageBuffer,
                                         that->mImageBufferSize);
  if (! newBuf) {
    // can't resize, just zero (this will keep us from writing more)
    PR_Free(that->mImageBuffer);
    that->mImageBuffer = nsnull;
    that->mImageBufferSize = 0;
    that->mImageBufferUsed = 0;

    // this seems to be the only way to do errors through the JPEG library
    longjmp(((encoder_error_mgr*)(cinfo->err))->setjmp_buffer,
            NS_ERROR_OUT_OF_MEMORY);
  }
  that->mImageBuffer = newBuf;

  cinfo->dest->next_output_byte = &that->mImageBuffer[that->mImageBufferUsed];
  cinfo->dest->free_in_buffer = that->mImageBufferSize - that->mImageBufferUsed;
  return 1;
}

Here is the call graph for this function:

void imgIEncoder::encodeClipboardImage ( in nsIClipboardImage  aClipboardImage,
out nsIFile  aImageFile 
) [inherited]
void nsJPEGEncoder::errorExit ( jpeg_common_struct cinfo) [static, protected]

Definition at line 393 of file nsJPEGEncoder.cpp.

{
  nsresult error_code;
  encoder_error_mgr *err = (encoder_error_mgr *) cinfo->err;

  // Convert error to a browser error code
  switch (cinfo->err->msg_code) {
    case JERR_OUT_OF_MEMORY:
      error_code = NS_ERROR_OUT_OF_MEMORY;
    default:
      error_code = NS_ERROR_FAILURE;
  }

  // Return control to the setjmp point.
  longjmp(err->setjmp_buffer, error_code);
}
void nsJPEGEncoder::initDestination ( jpeg_compress_struct cinfo) [static, protected]

Definition at line 311 of file nsJPEGEncoder.cpp.

{
  nsJPEGEncoder* that = NS_STATIC_CAST(nsJPEGEncoder*, cinfo->client_data);
  NS_ASSERTION(! that->mImageBuffer, "Image buffer already initialized");

  that->mImageBufferSize = 8192;
  that->mImageBuffer = (PRUint8*)PR_Malloc(that->mImageBufferSize);
  that->mImageBufferUsed = 0;

  cinfo->dest->next_output_byte = that->mImageBuffer;
  cinfo->dest->free_in_buffer = that->mImageBufferSize;
}
void nsJPEGEncoder::StripAlpha ( const PRUint8 aSrc,
PRUint8 aDest,
PRUint32  aPixelWidth 
) [protected]

Definition at line 291 of file nsJPEGEncoder.cpp.

{
  for (PRUint32 x = 0; x < aPixelWidth; x ++) {
    const PRUint8* pixelIn = &aSrc[x * 4];
    PRUint8* pixelOut = &aDest[x * 3];
    pixelOut[0] = pixelIn[0];
    pixelOut[1] = pixelIn[1];
    pixelOut[2] = pixelIn[2];
  }
}
void nsJPEGEncoder::termDestination ( jpeg_compress_struct cinfo) [static, protected]

Definition at line 376 of file nsJPEGEncoder.cpp.

{
  nsJPEGEncoder* that = NS_STATIC_CAST(nsJPEGEncoder*, cinfo->client_data);
  if (! that->mImageBuffer)
    return;
  that->mImageBufferUsed = cinfo->dest->next_output_byte - that->mImageBuffer;
  NS_ASSERTION(that->mImageBufferUsed < that->mImageBufferSize,
               "JPEG library busted, got a bad image buffer size");
}

Member Data Documentation

Definition at line 88 of file nsJPEGEncoder.h.

Definition at line 92 of file nsJPEGEncoder.h.

Definition at line 89 of file nsJPEGEncoder.h.

Definition at line 90 of file nsJPEGEncoder.h.


The documentation for this class was generated from the following files: