revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / diskimage / png_image / writergbapixels_p96.c
blob04db761cbbe0d6b84d7422742b218d452b90748a
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
27 #include "class.h"
28 #include "endian.h"
29 #include <libraries/Picasso96.h>
30 #include <proto/Picasso96API.h>
32 BOOL IsSupportedRGBFormat_P96 (ULONG rgbformat) {
33 switch (rgbformat) {
34 case RGBFB_R5G6B5:
35 case RGBFB_R5G6B5PC:
36 case RGBFB_B5G6R5PC:
37 case RGBFB_R5G5B5:
38 case RGBFB_R5G5B5PC:
39 case RGBFB_B5G5R5PC:
40 case RGBFB_A8R8G8B8:
41 case RGBFB_R8G8B8A8:
42 case RGBFB_R8G8B8:
43 case RGBFB_B8G8R8A8:
44 case RGBFB_A8B8G8R8:
45 case RGBFB_B8G8R8:
46 return TRUE;
48 default:
49 return FALSE;
53 IPTR WriteRGBAPixels_P96 (REG(a0, struct Hook *hook), REG(a2, struct RastPort *rp),
54 REG(a1, struct BackFillMessage *msg))
56 struct HookData *data = hook->h_Data;
57 WORD x, y, w, h;
58 ULONG bpp;
59 struct RenderInfo ri;
60 LONG lock;
61 UBYTE *src, *dst;
62 ULONG src_mod, dst_mod;
64 w = msg->Bounds.MaxX - msg->Bounds.MinX + 1;
65 h = msg->Bounds.MaxY - msg->Bounds.MinY + 1;
67 src = data->image + (msg->OffsetY - data->y) * (data->w * 4UL) + (msg->OffsetX - data->x) * 4UL;
68 src_mod = (data->w - w) * 4UL;
70 lock = p96LockBitMap(rp->BitMap, (UBYTE *)&ri, sizeof(ri));
72 bpp = p96GetBitMapAttr(rp->BitMap, P96BMA_BYTESPERPIXEL);
73 dst = (UBYTE *)ri.Memory + msg->Bounds.MinY * ri.BytesPerRow + msg->Bounds.MinX * bpp;
74 dst_mod = ri.BytesPerRow - w * bpp;
76 switch (ri.RGBFormat) {
77 case RGBFB_R5G6B5:
79 UWORD rgb;
80 UWORD a6, a5;
81 UWORD r, g, b;
82 for (y = 0; y < h; y++) {
83 for (x = 0; x < w; x++) {
84 a6 = src[3] >> 2;
85 a5 = src[3] >> 3;
86 if (a6 != 63) {
87 rgb = rbe16(dst);
88 r = (src[0] >> 3) + ((a5 * ((rgb >> 11) & 31)) >> 5);
89 g = (src[1] >> 2) + ((a6 * ((rgb >> 5) & 63)) >> 6);
90 b = (src[2] >> 3) + ((a5 * ((rgb) & 31)) >> 5);
91 wbe16(dst, (r << 11)|(g << 5)|(b));
93 src += 4;
94 dst += 2;
96 src += src_mod;
97 dst += dst_mod;
100 break;
102 case RGBFB_R5G6B5PC:
104 UWORD rgb;
105 UWORD a6, a5;
106 UWORD r, g, b;
107 for (y = 0; y < h; y++) {
108 for (x = 0; x < w; x++) {
109 a6 = src[3] >> 2;
110 a5 = src[3] >> 3;
111 if (a6 != 63) {
112 rgb = rle16(dst);
113 r = (src[0] >> 3) + ((a5 * ((rgb >> 11) & 31)) >> 5);
114 g = (src[1] >> 2) + ((a6 * ((rgb >> 5) & 63)) >> 6);
115 b = (src[2] >> 3) + ((a5 * ((rgb) & 31)) >> 5);
116 wle16(dst, (r << 11)|(g << 5)|(b));
118 src += 4;
119 dst += 2;
121 src += src_mod;
122 dst += dst_mod;
125 break;
127 case RGBFB_B5G6R5PC:
129 UWORD rgb;
130 UWORD a6, a5;
131 UWORD r, g, b;
132 for (y = 0; y < h; y++) {
133 for (x = 0; x < w; x++) {
134 a6 = src[3] >> 2;
135 a5 = src[3] >> 3;
136 if (a6 != 63) {
137 rgb = rle16(dst);
138 b = (src[2] >> 3) + ((a5 * ((rgb >> 11) & 31)) >> 5);
139 g = (src[1] >> 2) + ((a6 * ((rgb >> 5) & 63)) >> 6);
140 r = (src[0] >> 3) + ((a5 * ((rgb) & 31)) >> 5);
141 wle16(dst, (b << 11)|(g << 5)|(r));
143 src += 4;
144 dst += 2;
146 src += src_mod;
147 dst += dst_mod;
150 break;
152 case RGBFB_R5G5B5:
154 UWORD rgb;
155 UWORD a5;
156 UWORD r, g, b;
157 for (y = 0; y < h; y++) {
158 for (x = 0; x < w; x++) {
159 a5 = src[3] >> 3;
160 if (a5 != 31) {
161 rgb = rbe16(dst);
162 r = (src[0] >> 3) + ((a5 * ((rgb >> 10) & 31)) >> 5);
163 g = (src[1] >> 3) + ((a5 * ((rgb >> 5) & 31)) >> 5);
164 b = (src[2] >> 3) + ((a5 * ((rgb) & 31)) >> 5);
165 wbe16(dst, (r << 10)|(g << 5)|(b));
167 src += 4;
168 dst += 2;
170 src += src_mod;
171 dst += dst_mod;
174 break;
176 case RGBFB_R5G5B5PC:
178 UWORD rgb;
179 UWORD a5;
180 UWORD r, g, b;
181 for (y = 0; y < h; y++) {
182 for (x = 0; x < w; x++) {
183 a5 = src[3] >> 3;
184 if (a5 != 31) {
185 rgb = rle16(dst);
186 r = (src[0] >> 3) + ((a5 * ((rgb >> 10) & 31)) >> 5);
187 g = (src[1] >> 3) + ((a5 * ((rgb >> 5) & 31)) >> 5);
188 b = (src[2] >> 3) + ((a5 * ((rgb) & 31)) >> 5);
189 wle16(dst, (r << 10)|(g << 5)|(b));
191 src += 4;
192 dst += 2;
194 src += src_mod;
195 dst += dst_mod;
198 break;
200 case RGBFB_B5G5R5PC:
202 UWORD rgb;
203 UWORD a5;
204 UWORD r, g, b;
205 for (y = 0; y < h; y++) {
206 for (x = 0; x < w; x++) {
207 a5 = src[3] >> 3;
208 if (a5 != 31) {
209 rgb = rle16(dst);
210 b = (src[2] >> 3) + ((a5 * ((rgb >> 10) & 31)) >> 5);
211 g = (src[1] >> 3) + ((a5 * ((rgb >> 5) & 31)) >> 5);
212 r = (src[0] >> 3) + ((a5 * ((rgb) & 31)) >> 5);
213 wle16(dst, (b << 10)|(g << 5)|(r));
215 src += 4;
216 dst += 2;
218 src += src_mod;
219 dst += dst_mod;
222 break;
224 case RGBFB_A8R8G8B8:
225 case RGBFB_R8G8B8A8:
226 case RGBFB_R8G8B8:
228 BOOL pre, post;
229 UWORD a;
230 pre = (ri.RGBFormat == RGBFB_A8R8G8B8);
231 post = (ri.RGBFormat == RGBFB_R8G8B8A8);
232 for (y = 0; y < h; y++) {
233 for (x = 0; x < w; x++) {
234 a = src[3];
235 if (pre) dst++;
236 if (a != 255) {
237 dst[0] = src[0] + ((a * dst[0]) >> 8);
238 dst[1] = src[1] + ((a * dst[1]) >> 8);
239 dst[2] = src[2] + ((a * dst[2]) >> 8);
241 if (post) dst++;
242 src += 4;
243 dst += 3;
245 src += src_mod;
246 dst += dst_mod;
249 break;
251 case RGBFB_A8B8G8R8:
252 case RGBFB_B8G8R8A8:
253 case RGBFB_B8G8R8:
255 BOOL pre, post;
256 UWORD a;
257 pre = (ri.RGBFormat == RGBFB_A8B8G8R8);
258 post = (ri.RGBFormat == RGBFB_B8G8R8A8);
259 for (y = 0; y < h; y++) {
260 for (x = 0; x < w; x++) {
261 a = src[3];
262 if (pre) dst++;
263 if (a != 255) {
264 dst[0] = src[2] + ((a * dst[0]) >> 8);
265 dst[1] = src[1] + ((a * dst[1]) >> 8);
266 dst[2] = src[0] + ((a * dst[2]) >> 8);
268 if (post) dst++;
269 src += 4;
270 dst += 3;
272 src += src_mod;
273 dst += dst_mod;
276 break;
279 p96UnlockBitMap(rp->BitMap, lock);
281 return 0;