Bug 688238. imglib: Move Decoder::Init() arguments into the constructor. r=joedrew
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Tue, 27 Sep 2011 12:24:03 -0400
changeset 77768 cd9a1438836730e7819a82f9a0c7b2e3c49ba498
parent 77767 88960733383e3b79ec604761e4067a17b4f79ea3
child 77769 09d60465831c71dcf8dbc695d62c79b23b23f4e5
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersjoedrew
bugs688238
milestone10.0a1
Bug 688238. imglib: Move Decoder::Init() arguments into the constructor. r=joedrew This makes the lifetime of the arguments more clear. i.e. that they stay the same for the lifetime of the decoder.
modules/libpr0n/decoders/nsBMPDecoder.cpp
modules/libpr0n/decoders/nsBMPDecoder.h
modules/libpr0n/decoders/nsGIFDecoder2.cpp
modules/libpr0n/decoders/nsGIFDecoder2.h
modules/libpr0n/decoders/nsICODecoder.cpp
modules/libpr0n/decoders/nsICODecoder.h
modules/libpr0n/decoders/nsIconDecoder.cpp
modules/libpr0n/decoders/nsIconDecoder.h
modules/libpr0n/decoders/nsJPEGDecoder.cpp
modules/libpr0n/decoders/nsJPEGDecoder.h
modules/libpr0n/decoders/nsPNGDecoder.cpp
modules/libpr0n/decoders/nsPNGDecoder.h
modules/libpr0n/src/Decoder.cpp
modules/libpr0n/src/Decoder.h
modules/libpr0n/src/RasterImage.cpp
--- a/modules/libpr0n/decoders/nsBMPDecoder.cpp
+++ b/modules/libpr0n/decoders/nsBMPDecoder.cpp
@@ -58,27 +58,28 @@ namespace imagelib {
 #ifdef PR_LOGGING
 PRLogModuleInfo *gBMPLog = PR_NewLogModule("BMPDecoder");
 #endif
 
 // Convert from row (1..height) to absolute line (0..height-1)
 #define LINE(row) ((mBIH.height < 0) ? (-mBIH.height - (row)) : ((row) - 1))
 #define PIXEL_OFFSET(row, col) (LINE(row) * mBIH.width + col)
 
-nsBMPDecoder::nsBMPDecoder()
+nsBMPDecoder::nsBMPDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver)
+ : Decoder(aImage, aObserver)
 {
-    mColors = nsnull;
-    mRow = nsnull;
-    mImageData = nsnull;
-    mCurPos = mPos = mNumColors = mRowBytes = 0;
-    mOldLine = mCurLine = 1; // Otherwise decoder will never start
-    mState = eRLEStateInitial;
-    mStateData = 0;
-    mLOH = WIN_HEADER_LENGTH;
-    mUseAlphaData = mHaveAlphaData = PR_FALSE;
+  mColors = nsnull;
+  mRow = nsnull;
+  mImageData = nsnull;
+  mCurPos = mPos = mNumColors = mRowBytes = 0;
+  mOldLine = mCurLine = 1; // Otherwise decoder will never start
+  mState = eRLEStateInitial;
+  mStateData = 0;
+  mLOH = WIN_HEADER_LENGTH;
+  mUseAlphaData = mHaveAlphaData = PR_FALSE;
 }
 
 nsBMPDecoder::~nsBMPDecoder()
 {
   delete[] mColors;
   if (mRow) {
       moz_free(mRow);
   }
--- a/modules/libpr0n/decoders/nsBMPDecoder.h
+++ b/modules/libpr0n/decoders/nsBMPDecoder.h
@@ -54,17 +54,17 @@ class RasterImage;
 
 /**
  * Decoder for BMP-Files, as used by Windows and OS/2
  */
 class nsBMPDecoder : public Decoder
 {
 public:
 
-    nsBMPDecoder();
+    nsBMPDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver);
     ~nsBMPDecoder();
 
     // Specifies whether or not the BMP file will contain alpha data
     // If set to true and the BMP is 32BPP, the alpha data will be
     // retrieved from the 4th byte of image data per pixel 
     void SetUseAlphaData(PRBool useAlphaData);
     // Obtains the bits per pixel from the internal BIH header
     PRInt32 GetBitsPerPixel() const;
--- a/modules/libpr0n/decoders/nsGIFDecoder2.cpp
+++ b/modules/libpr0n/decoders/nsGIFDecoder2.cpp
@@ -102,18 +102,19 @@ namespace imagelib {
     mGIFStruct.state = (s);            \
   PR_END_MACRO
 
 /* Get a 16-bit value stored in little-endian format */
 #define GETINT16(p)   ((p)[1]<<8|(p)[0])
 //////////////////////////////////////////////////////////////////////
 // GIF Decoder Implementation
 
-nsGIFDecoder2::nsGIFDecoder2()
-  : mCurrentRow(-1)
+nsGIFDecoder2::nsGIFDecoder2(RasterImage *aImage, imgIDecoderObserver* aObserver)
+  : Decoder(aImage, aObserver)
+  , mCurrentRow(-1)
   , mLastFlushedRow(-1)
   , mImageData(nsnull)
   , mOldColor(0)
   , mCurrentFrame(-1)
   , mCurrentPass(0)
   , mLastFlushedPass(0)
   , mGIFOpen(PR_FALSE)
   , mSawTransparency(PR_FALSE)
--- a/modules/libpr0n/decoders/nsGIFDecoder2.h
+++ b/modules/libpr0n/decoders/nsGIFDecoder2.h
@@ -53,17 +53,17 @@ class RasterImage;
 
 //////////////////////////////////////////////////////////////////////
 // nsGIFDecoder2 Definition
 
 class nsGIFDecoder2 : public Decoder
 {
 public:
 
-  nsGIFDecoder2();
+  nsGIFDecoder2(RasterImage *aImage, imgIDecoderObserver* aObserver);
   ~nsGIFDecoder2();
 
   virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
   virtual void FinishInternal();
   virtual Telemetry::ID SpeedHistogram();
 
 private:
   /* These functions will be called when the decoder has a decoded row,
--- a/modules/libpr0n/decoders/nsICODecoder.cpp
+++ b/modules/libpr0n/decoders/nsICODecoder.cpp
@@ -94,17 +94,18 @@ nsICODecoder::GetNumColors()
     default:
       numColors = (PRUint16)-1;
     }
   }
   return numColors;
 }
 
 
-nsICODecoder::nsICODecoder()
+nsICODecoder::nsICODecoder(RasterImage *aImage, imgIDecoderObserver* aObserver)
+ : Decoder(aImage, aObserver)
 {
   mPos = mImageOffset = mCurrIcon = mNumIcons = mBPP = mRowBytes = 0;
   mIsPNG = PR_FALSE;
   mRow = nsnull;
   mOldLine = mCurLine = 1; // Otherwise decoder will never start
 }
 
 nsICODecoder::~nsICODecoder()
@@ -312,18 +313,18 @@ nsICODecoder::WriteInternal(const char* 
     memcpy(mSignature + (mPos - mImageOffset), aBuffer, toCopy);
     mPos += toCopy;
     aCount -= toCopy;
     aBuffer += toCopy;
 
     mIsPNG = !memcmp(mSignature, nsPNGDecoder::pngSignatureBytes, 
                      PNGSIGNATURESIZE);
     if (mIsPNG) {
-      mContainedDecoder = new nsPNGDecoder();
-      mContainedDecoder->InitSharedDecoder(mImage, mObserver);
+      mContainedDecoder = new nsPNGDecoder(mImage, mObserver);
+      mContainedDecoder->InitSharedDecoder();
       mContainedDecoder->Write(mSignature, PNGSIGNATURESIZE);
       mDataError = mContainedDecoder->HasDataError();
       if (mContainedDecoder->HasDataError()) {
         return;
       }
     }
   }
 
@@ -381,21 +382,21 @@ nsICODecoder::WriteInternal(const char* 
     }
     // We are extracting the BPP from the BIH header as it should be trusted 
     // over the one we have from the icon header
     mBPP = ExtractBPPFromBitmap(reinterpret_cast<PRInt8*>(mBIHraw));
     
     // Init the bitmap decoder which will do most of the work for us
     // It will do everything except the AND mask which isn't present in bitmaps
     // bmpDecoder is for local scope ease, it will be freed by mContainedDecoder
-    nsBMPDecoder *bmpDecoder = new nsBMPDecoder(); 
+    nsBMPDecoder *bmpDecoder = new nsBMPDecoder(mImage, mObserver); 
     mContainedDecoder = bmpDecoder;
     bmpDecoder->SetUseAlphaData(PR_TRUE);
     mContainedDecoder->SetSizeDecode(IsSizeDecode());
-    mContainedDecoder->InitSharedDecoder(mImage, mObserver);
+    mContainedDecoder->InitSharedDecoder();
 
     // The ICO format when containing a BMP does not include the 14 byte
     // bitmap file header. To use the code of the BMP decoder we need to 
     // generate this header ourselves and feed it to the BMP decoder.
     PRInt8 bfhBuffer[BMPFILEHEADERSIZE];
     if (!FillBitmapFileHeaderBuffer(bfhBuffer)) {
       PostDataError();
       return;
--- a/modules/libpr0n/decoders/nsICODecoder.h
+++ b/modules/libpr0n/decoders/nsICODecoder.h
@@ -53,17 +53,17 @@ namespace mozilla {
 namespace imagelib {
 
 class RasterImage;
 
 class nsICODecoder : public Decoder
 {
 public:
 
-  nsICODecoder();
+  nsICODecoder(RasterImage *aImage, imgIDecoderObserver* aObserver);
   virtual ~nsICODecoder();
 
   // Obtains the width of the icon directory entry
   PRUint32 GetRealWidth() const
   {
     return mDirEntry.mWidth == 0 ? 256 : mDirEntry.mWidth; 
   }
 
--- a/modules/libpr0n/decoders/nsIconDecoder.cpp
+++ b/modules/libpr0n/decoders/nsIconDecoder.cpp
@@ -45,23 +45,24 @@
 #include "nspr.h"
 #include "nsRect.h"
 
 #include "ImageErrors.h"
 
 namespace mozilla {
 namespace imagelib {
 
-nsIconDecoder::nsIconDecoder() :
-  mWidth(-1),
-  mHeight(-1),
-  mPixBytesRead(0),
-  mPixBytesTotal(0),
-  mImageData(nsnull),
-  mState(iconStateStart)
+nsIconDecoder::nsIconDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver)
+ : Decoder(aImage, aObserver),
+   mWidth(-1),
+   mHeight(-1),
+   mPixBytesRead(0),
+   mPixBytesTotal(0),
+   mImageData(nsnull),
+   mState(iconStateStart)
 {
   // Nothing to do
 }
 
 nsIconDecoder::~nsIconDecoder()
 { }
 
 void
--- a/modules/libpr0n/decoders/nsIconDecoder.h
+++ b/modules/libpr0n/decoders/nsIconDecoder.h
@@ -69,17 +69,17 @@ class RasterImage;
 //
 //
 //////////////////////////////////////////////////////////////////////////////////////////////
 
 class nsIconDecoder : public Decoder
 {
 public:
 
-  nsIconDecoder();
+  nsIconDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver);
   virtual ~nsIconDecoder();
 
   virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
 
   PRUint8 mWidth;
   PRUint8 mHeight;
   PRUint32 mPixBytesRead;
   PRUint32 mPixBytesTotal;
--- a/modules/libpr0n/decoders/nsJPEGDecoder.cpp
+++ b/modules/libpr0n/decoders/nsJPEGDecoder.cpp
@@ -104,17 +104,18 @@ METHODDEF(boolean) fill_input_buffer (j_
 METHODDEF(void) skip_input_data (j_decompress_ptr jd, long num_bytes);
 METHODDEF(void) term_source (j_decompress_ptr jd);
 METHODDEF(void) my_error_exit (j_common_ptr cinfo);
 
 /* Normal JFIF markers can't have more bytes than this. */
 #define MAX_JPEG_MARKER_LENGTH  (((PRUint32)1 << 16) - 1)
 
 
-nsJPEGDecoder::nsJPEGDecoder()
+nsJPEGDecoder::nsJPEGDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver)
+ : Decoder(aImage, aObserver)
 {
   mState = JPEG_HEADER;
   mReading = PR_TRUE;
   mImageData = nsnull;
 
   mBytesToSkip = 0;
   memset(&mInfo, 0, sizeof(jpeg_decompress_struct));
   memset(&mSourceMgr, 0, sizeof(mSourceMgr));
--- a/modules/libpr0n/decoders/nsJPEGDecoder.h
+++ b/modules/libpr0n/decoders/nsJPEGDecoder.h
@@ -81,17 +81,17 @@ typedef enum {
     JPEG_ERROR    
 } jstate;
 
 class RasterImage;
 
 class nsJPEGDecoder : public Decoder
 {
 public:
-  nsJPEGDecoder();
+  nsJPEGDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver);
   virtual ~nsJPEGDecoder();
 
   virtual void InitInternal();
   virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
   virtual void FinishInternal();
 
   virtual Telemetry::ID SpeedHistogram();
   void NotifyDone();
--- a/modules/libpr0n/decoders/nsPNGDecoder.cpp
+++ b/modules/libpr0n/decoders/nsPNGDecoder.cpp
@@ -76,23 +76,24 @@ static PRLogModuleInfo *gPNGDecoderAccou
 #define WIDTH_OFFSET 16
 #define HEIGHT_OFFSET (WIDTH_OFFSET + 4)
 #define BYTES_NEEDED_FOR_DIMENSIONS (HEIGHT_OFFSET + 4)
 
 // First 8 bytes of a PNG file
 const PRUint8 
 nsPNGDecoder::pngSignatureBytes[] = { 137, 80, 78, 71, 13, 10, 26, 10 };
 
-nsPNGDecoder::nsPNGDecoder() :
-  mPNG(nsnull), mInfo(nsnull),
-  mCMSLine(nsnull), interlacebuf(nsnull),
-  mInProfile(nsnull), mTransform(nsnull),
-  mHeaderBuf(nsnull), mHeaderBytesRead(0),
-  mChannels(0), mFrameIsHidden(PR_FALSE),
-  mCMSMode(0), mDisablePremultipliedAlpha(PR_FALSE)
+nsPNGDecoder::nsPNGDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver)
+ : Decoder(aImage, aObserver),
+   mPNG(nsnull), mInfo(nsnull),
+   mCMSLine(nsnull), interlacebuf(nsnull),
+   mInProfile(nsnull), mTransform(nsnull),
+   mHeaderBuf(nsnull), mHeaderBytesRead(0),
+   mChannels(0), mFrameIsHidden(PR_FALSE),
+   mCMSMode(0), mDisablePremultipliedAlpha(PR_FALSE)
 {
 }
 
 nsPNGDecoder::~nsPNGDecoder()
 {
   if (mPNG)
     png_destroy_read_struct(&mPNG, mInfo ? &mInfo : NULL, NULL);
   if (mCMSLine)
--- a/modules/libpr0n/decoders/nsPNGDecoder.h
+++ b/modules/libpr0n/decoders/nsPNGDecoder.h
@@ -54,17 +54,17 @@
 
 namespace mozilla {
 namespace imagelib {
 class RasterImage;
 
 class nsPNGDecoder : public Decoder
 {
 public:
-  nsPNGDecoder();
+  nsPNGDecoder(RasterImage *aImage, imgIDecoderObserver* aObserver);
   virtual ~nsPNGDecoder();
 
   virtual void InitInternal();
   virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
   virtual Telemetry::ID SpeedHistogram();
 
   void CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
                    PRInt32 width, PRInt32 height,
--- a/modules/libpr0n/src/Decoder.cpp
+++ b/modules/libpr0n/src/Decoder.cpp
@@ -39,74 +39,66 @@
 #include "Decoder.h"
 #include "nsIServiceManager.h"
 #include "nsIConsoleService.h"
 #include "nsIScriptError.h"
 
 namespace mozilla {
 namespace imagelib {
 
-Decoder::Decoder()
+Decoder::Decoder(RasterImage *aImage, imgIDecoderObserver* aObserver)
   : mDecodeFlags(0)
   , mFrameCount(0)
   , mFailCode(NS_OK)
   , mInitialized(false)
   , mSizeDecode(false)
   , mInFrame(false)
   , mDecodeDone(false)
   , mDataError(false)
 {
+  // We should always have an image
+  NS_ABORT_IF_FALSE(aImage, "Can't initialize decoder without an image!");
+
+  // Save our paremeters
+  mImage = aImage;
+  mObserver = aObserver;
 }
 
 Decoder::~Decoder()
 {
   NS_WARN_IF_FALSE(!mInFrame, "Shutting down decoder mid-frame!");
   mInitialized = false;
 }
 
 /*
  * Common implementation of the decoder interface.
  */
 
 void
-Decoder::Init(RasterImage* aImage, imgIDecoderObserver* aObserver)
+Decoder::Init()
 {
-  // We should always have an image
-  NS_ABORT_IF_FALSE(aImage, "Can't initialize decoder without an image!");
-
   // No re-initializing
-  NS_ABORT_IF_FALSE(mImage == nsnull, "Can't re-initialize a decoder!");
-
-  // Save our paremeters
-  mImage = aImage;
-  mObserver = aObserver;
+  NS_ABORT_IF_FALSE(!mInitialized, "Can't re-initialize a decoder!");
 
   // Fire OnStartDecode at init time to support bug 512435
   if (!IsSizeDecode() && mObserver)
       mObserver->OnStartDecode(nsnull);
 
   // Implementation-specific initialization
   InitInternal();
   mInitialized = true;
 }
 
 // Initializes a decoder whose aImage and aObserver is already being used by a
 // parent decoder
-void 
-Decoder::InitSharedDecoder(RasterImage* aImage, imgIDecoderObserver* aObserver) 
+void
+Decoder::InitSharedDecoder()
 {
-  // We should always have an image
-  NS_ABORT_IF_FALSE(aImage, "Can't initialize decoder without an image!");
-
   // No re-initializing
-  NS_ABORT_IF_FALSE(mImage == nsnull, "Can't re-initialize a decoder!");
-
-  // Save our parameters
-  mImage = aImage;
-  mObserver = aObserver;
+  NS_ABORT_IF_FALSE(!mInitialized, "Can't re-initialize a decoder!");
 
   // Implementation-specific initialization
   InitInternal();
   mInitialized = true;
 }
 
 void
 Decoder::Write(const char* aBuffer, PRUint32 aCount)
--- a/modules/libpr0n/src/Decoder.h
+++ b/modules/libpr0n/src/Decoder.h
@@ -45,40 +45,40 @@
 
 namespace mozilla {
 namespace imagelib {
 
 class Decoder
 {
 public:
 
-  Decoder();
+  Decoder(RasterImage* aImage, imgIDecoderObserver* aObserver);
   virtual ~Decoder();
 
   /**
    * Initialize an image decoder. Decoders may not be re-initialized.
    *
    * @param aContainer The image container to decode to.
    * @param aObserver The observer for decode notification events.
    *
    * Notifications Sent: TODO
    */
-  void Init(RasterImage* aImage, imgIDecoderObserver* aObserver);
+  void Init();
 
 
   /**
    * Initializes a decoder whose aImage and aObserver is already being used by a
    * parent decoder. Decoders may not be re-initialized.
    *
    * @param aContainer The image container to decode to.
    * @param aObserver The observer for decode notification events.
    *
    * Notifications Sent: TODO
    */
-  void InitSharedDecoder(RasterImage* aImage, imgIDecoderObserver* aObserver);
+  void InitSharedDecoder();
 
   /**
    * Writes data to the decoder.
    *
    * @param aBuffer buffer containing the data to be written
    * @param aCount the number of bytes to write
    *
    * Any errors are reported by setting the appropriate state on the decoder.
--- a/modules/libpr0n/src/RasterImage.cpp
+++ b/modules/libpr0n/src/RasterImage.cpp
@@ -2162,45 +2162,45 @@ RasterImage::InitDecoder(bool aDoSizeDec
 
   // Since we're not decoded, we should not have a discard timer active
   NS_ABORT_IF_FALSE(!DiscardingActive(), "Discard Timer active in InitDecoder()!");
 
   // Figure out which decoder we want
   eDecoderType type = GetDecoderType(mSourceDataMimeType.get());
   CONTAINER_ENSURE_TRUE(type != eDecoderType_unknown, NS_IMAGELIB_ERROR_NO_DECODER);
 
+  nsCOMPtr<imgIDecoderObserver> observer(do_QueryReferent(mObserver));
   // Instantiate the appropriate decoder
   switch (type) {
     case eDecoderType_png:
-      mDecoder = new nsPNGDecoder();
+      mDecoder = new nsPNGDecoder(this, observer);
       break;
     case eDecoderType_gif:
-      mDecoder = new nsGIFDecoder2();
+      mDecoder = new nsGIFDecoder2(this, observer);
       break;
     case eDecoderType_jpeg:
-      mDecoder = new nsJPEGDecoder();
+      mDecoder = new nsJPEGDecoder(this, observer);
       break;
     case eDecoderType_bmp:
-      mDecoder = new nsBMPDecoder();
+      mDecoder = new nsBMPDecoder(this, observer);
       break;
     case eDecoderType_ico:
-      mDecoder = new nsICODecoder();
+      mDecoder = new nsICODecoder(this, observer);
       break;
     case eDecoderType_icon:
-      mDecoder = new nsIconDecoder();
+      mDecoder = new nsIconDecoder(this, observer);
       break;
     default:
       NS_ABORT_IF_FALSE(0, "Shouldn't get here!");
   }
 
   // Initialize the decoder
-  nsCOMPtr<imgIDecoderObserver> observer(do_QueryReferent(mObserver));
   mDecoder->SetSizeDecode(aDoSizeDecode);
   mDecoder->SetDecodeFlags(mFrameDecodeFlags);
-  mDecoder->Init(this, observer);
+  mDecoder->Init();
   CONTAINER_ENSURE_SUCCESS(mDecoder->GetDecoderError());
 
   // Create a decode worker
   mWorker = new imgDecodeWorker(this);
 
   if (!aDoSizeDecode) {
     Telemetry::GetHistogramById(Telemetry::IMAGE_DECODE_COUNT)->Subtract(mDecodeCount);
     mDecodeCount++;