1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsIconDecoder.h"
8 #include "nsIInputStream.h"
12 #include "RasterImage.h"
18 nsIconDecoder::nsIconDecoder(RasterImage
* aImage
)
23 mState(iconStateStart
)
28 nsIconDecoder::~nsIconDecoder()
32 nsIconDecoder::WriteInternal(const char* aBuffer
, uint32_t aCount
)
34 NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
36 // We put this here to avoid errors about crossing initialization with case
38 uint32_t bytesToRead
= 0;
40 // Loop until the input data is gone
46 mWidth
= (uint8_t)*aBuffer
;
51 mState
= iconStateHaveHeight
;
54 case iconStateHaveHeight
:
57 mHeight
= (uint8_t)*aBuffer
;
59 // Post our size to the superclass
60 PostSize(mWidth
, mHeight
);
62 PostHasTransparency();
65 // Setting the size led to an error.
66 mState
= iconStateFinished
;
70 // If We're doing a size decode, we're done
72 mState
= iconStateFinished
;
77 PostDecoderError(NS_ERROR_OUT_OF_MEMORY
);
84 mState
= iconStateReadPixels
;
87 case iconStateReadPixels
: {
89 // How many bytes are we reading?
90 bytesToRead
= std::min(aCount
, mImageDataLength
- mPixBytesRead
);
93 memcpy(mImageData
+ mPixBytesRead
, aBuffer
, bytesToRead
);
95 // Performance isn't critical here, so our update rectangle is
96 // always the full icon
97 nsIntRect
r(0, 0, mWidth
, mHeight
);
103 aBuffer
+= bytesToRead
;
104 aCount
-= bytesToRead
;
105 mPixBytesRead
+= bytesToRead
;
107 // If we've got all the pixel bytes, we're finished
108 if (mPixBytesRead
== mImageDataLength
) {
111 mState
= iconStateFinished
;
116 case iconStateFinished
:
118 // Consume all excess data silently
127 } // namespace mozilla