1 /*
2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "ImageDecoder.h"
28
29 // FIXME: Are all these needed?
30 #include <wx/defs.h>
31 #include <wx/bitmap.h>
32 #if USE(WXGC)
33 #include <wx/graphics.h>
34 #endif
35 #include <wx/image.h>
36 #include <wx/rawbmp.h>
37
38 namespace WebCore {
39
RGBA32Buffer()40 RGBA32Buffer::RGBA32Buffer()
41 : m_hasAlpha(false)
42 , m_status(FrameEmpty)
43 , m_duration(0)
44 , m_disposalMethod(DisposeNotSpecified)
45 {
46 }
47
clear()48 void RGBA32Buffer::clear()
49 {
50 m_bytes.clear();
51 m_status = FrameEmpty;
52 // NOTE: Do not reset other members here; clearFrameBufferCache()
53 // calls this to free the bitmap data, but other functions like
54 // initFrameBuffer() and frameComplete() may still need to read
55 // other metadata out of this frame later.
56 }
57
zeroFill()58 void RGBA32Buffer::zeroFill()
59 {
60 m_bytes.fill(0);
61 m_hasAlpha = true;
62 }
63
copyBitmapData(const RGBA32Buffer & other)64 void RGBA32Buffer::copyBitmapData(const RGBA32Buffer& other)
65 {
66 if (this == &other)
67 return;
68
69 m_bytes = other.m_bytes;
70 m_size = other.m_size;
71 setHasAlpha(other.m_hasAlpha);
72 }
73
setSize(int newWidth,int newHeight)74 bool RGBA32Buffer::setSize(int newWidth, int newHeight)
75 {
76 // NOTE: This has no way to check for allocation failure if the
77 // requested size was too big...
78 m_bytes.resize(newWidth * newHeight);
79 m_size = IntSize(newWidth, newHeight);
80
81 // Zero the image.
82 zeroFill();
83
84 return true;
85 }
86
asNewNativeImage() const87 NativeImagePtr RGBA32Buffer::asNewNativeImage() const
88 {
89 const unsigned char* bytes = (const unsigned char*)m_bytes.data();
90
91 typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> WxPixelData;
92
93 wxBitmap* bmp = new wxBitmap(width(), height(), 32);
94 WxPixelData data(*bmp);
95
96 int rowCounter = 0;
97 long pixelCounter = 0;
98
99 WxPixelData::Iterator p(data);
100
101 WxPixelData::Iterator rowStart = p;
102
103 // NB: It appears that the data is in BGRA format instead of RGBA format.
104 // This code works properly on both ppc and intel, meaning the issue is
105 // likely not an issue of byte order getting mixed up on different archs.
106 for (long i = 0; i < m_bytes.size() * sizeof(PixelData); i += sizeof(PixelData)) {
107 p.Red() = bytes[i+2];
108 p.Green() = bytes[i+1];
109 p.Blue() = bytes[i+0];
110 p.Alpha() = bytes[i+3];
111
112 p++;
113
114 pixelCounter++;
115 if ( (pixelCounter % width() ) == 0 ) {
116 rowCounter++;
117 p = rowStart;
118 p.MoveTo(data, 0, rowCounter);
119 }
120
121 }
122 #if !wxCHECK_VERSION(2,9,0)
123 bmp->UseAlpha();
124 #endif
125 ASSERT(bmp->IsOk());
126
127 #if USE(WXGC)
128 wxGraphicsBitmap* bitmap = new wxGraphicsBitmap(wxGraphicsRenderer::GetDefaultRenderer()->CreateBitmap(*bmp));
129 delete bmp;
130 return bitmap;
131 #else
132 return bmp;
133 #endif
134 }
135
hasAlpha() const136 bool RGBA32Buffer::hasAlpha() const
137 {
138 return m_hasAlpha;
139 }
140
setHasAlpha(bool alpha)141 void RGBA32Buffer::setHasAlpha(bool alpha)
142 {
143 m_hasAlpha = alpha;
144 }
145
setStatus(FrameStatus status)146 void RGBA32Buffer::setStatus(FrameStatus status)
147 {
148 m_status = status;
149 }
150
operator =(const RGBA32Buffer & other)151 RGBA32Buffer& RGBA32Buffer::operator=(const RGBA32Buffer& other)
152 {
153 if (this == &other)
154 return *this;
155
156 copyBitmapData(other);
157 setRect(other.rect());
158 setStatus(other.status());
159 setDuration(other.duration());
160 setDisposalMethod(other.disposalMethod());
161 return *this;
162 }
163
width() const164 int RGBA32Buffer::width() const {
165 return m_size.width();
166 }
167
height() const168 int RGBA32Buffer::height() const {
169 return m_size.height();
170 }
171
172 } // namespace WebCore
173