1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2001
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Scott MacGregor <mscott@netscape.com>
25 : * Bobby Holley <bobbyholley@gmail.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsIconDecoder.h"
42 : #include "nsIInputStream.h"
43 : #include "RasterImage.h"
44 : #include "imgIContainerObserver.h"
45 : #include "nspr.h"
46 : #include "nsRect.h"
47 :
48 : #include "ImageErrors.h"
49 :
50 : namespace mozilla {
51 : namespace image {
52 :
53 0 : nsIconDecoder::nsIconDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver)
54 : : Decoder(aImage, aObserver),
55 : mWidth(-1),
56 : mHeight(-1),
57 : mPixBytesRead(0),
58 : mPixBytesTotal(0),
59 : mImageData(nsnull),
60 0 : mState(iconStateStart)
61 : {
62 : // Nothing to do
63 0 : }
64 :
65 0 : nsIconDecoder::~nsIconDecoder()
66 0 : { }
67 :
68 : void
69 0 : nsIconDecoder::WriteInternal(const char *aBuffer, PRUint32 aCount)
70 : {
71 0 : NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
72 :
73 : // We put this here to avoid errors about crossing initialization with case
74 : // jumps on linux.
75 0 : PRUint32 bytesToRead = 0;
76 : nsresult rv;
77 :
78 : // Performance isn't critical here, so our update rectangle is
79 : // always the full icon
80 0 : nsIntRect r(0, 0, mWidth, mHeight);
81 :
82 : // Loop until the input data is gone
83 0 : while (aCount > 0) {
84 0 : switch (mState) {
85 : case iconStateStart:
86 :
87 : // Grab the width
88 0 : mWidth = (PRUint8)*aBuffer;
89 :
90 : // Book Keeping
91 0 : aBuffer++;
92 0 : aCount--;
93 0 : mState = iconStateHaveHeight;
94 0 : break;
95 :
96 : case iconStateHaveHeight:
97 :
98 : // Grab the Height
99 0 : mHeight = (PRUint8)*aBuffer;
100 :
101 : // Post our size to the superclass
102 0 : PostSize(mWidth, mHeight);
103 0 : if (HasError()) {
104 : // Setting the size lead to an error; this can happen when for example
105 : // a multipart channel sends an image of a different size.
106 0 : mState = iconStateFinished;
107 0 : return;
108 : }
109 :
110 : // If We're doing a size decode, we're done
111 0 : if (IsSizeDecode()) {
112 0 : mState = iconStateFinished;
113 0 : break;
114 : }
115 :
116 : // Add the frame and signal
117 : rv = mImage.EnsureFrame(0, 0, 0, mWidth, mHeight,
118 : gfxASurface::ImageFormatARGB32,
119 0 : &mImageData, &mPixBytesTotal);
120 0 : if (NS_FAILED(rv)) {
121 0 : PostDecoderError(rv);
122 0 : return;
123 : }
124 :
125 : // Tell the superclass we're starting a frame
126 0 : PostFrameStart();
127 :
128 : // Book Keeping
129 0 : aBuffer++;
130 0 : aCount--;
131 0 : mState = iconStateReadPixels;
132 0 : break;
133 :
134 : case iconStateReadPixels:
135 :
136 : // How many bytes are we reading?
137 0 : bytesToRead = NS_MIN(aCount, mPixBytesTotal - mPixBytesRead);
138 :
139 : // Copy the bytes
140 0 : memcpy(mImageData + mPixBytesRead, aBuffer, bytesToRead);
141 :
142 : // Invalidate
143 0 : PostInvalidation(r);
144 :
145 : // Book Keeping
146 0 : aBuffer += bytesToRead;
147 0 : aCount -= bytesToRead;
148 0 : mPixBytesRead += bytesToRead;
149 :
150 : // If we've got all the pixel bytes, we're finished
151 0 : if (mPixBytesRead == mPixBytesTotal) {
152 0 : PostFrameStop();
153 0 : PostDecodeDone();
154 0 : mState = iconStateFinished;
155 : }
156 0 : break;
157 :
158 : case iconStateFinished:
159 :
160 : // Consume all excess data silently
161 0 : aCount = 0;
162 :
163 0 : break;
164 : }
165 : }
166 : }
167 :
168 : } // namespace image
169 : } // namespace mozilla
|