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"
9 #include "RasterImage.h"
19 nsIconDecoder::nsIconDecoder(RasterImage
&aImage
)
24 mState(iconStateStart
)
29 nsIconDecoder::~nsIconDecoder()
33 nsIconDecoder::WriteInternal(const char *aBuffer
, uint32_t aCount
, DecodeStrategy
)
35 NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
37 // We put this here to avoid errors about crossing initialization with case
39 uint32_t bytesToRead
= 0;
41 // Loop until the input data is gone
47 mWidth
= (uint8_t)*aBuffer
;
52 mState
= iconStateHaveHeight
;
55 case iconStateHaveHeight
:
58 mHeight
= (uint8_t)*aBuffer
;
60 // Post our size to the superclass
61 PostSize(mWidth
, mHeight
);
63 // Setting the size led to an error.
64 mState
= iconStateFinished
;
68 // If We're doing a size decode, we're done
70 mState
= iconStateFinished
;
75 PostDecoderError(NS_ERROR_OUT_OF_MEMORY
);
82 mState
= iconStateReadPixels
;
85 case iconStateReadPixels
:
88 // How many bytes are we reading?
89 bytesToRead
= std::min(aCount
, mImageDataLength
- mPixBytesRead
);
92 memcpy(mImageData
+ mPixBytesRead
, aBuffer
, bytesToRead
);
94 // Performance isn't critical here, so our update rectangle is
95 // always the full icon
96 nsIntRect
r(0, 0, mWidth
, mHeight
);
102 aBuffer
+= bytesToRead
;
103 aCount
-= bytesToRead
;
104 mPixBytesRead
+= bytesToRead
;
106 // If we've got all the pixel bytes, we're finished
107 if (mPixBytesRead
== mImageDataLength
) {
110 mState
= iconStateFinished
;
115 case iconStateFinished
:
117 // Consume all excess data silently
126 } // namespace mozilla