comparison dwt/internal/image/PngEncoder.d @ 213:36f5cb12e1a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 17:34:28 +0200
parents 57151e2793a2
children fd9c62a2998e
comparison
equal deleted inserted replaced
212:ab60f3309436 213:36f5cb12e1a2
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
13 module dwt.internal.image.PngEncoder; 13 module dwt.internal.image.PngEncoder;
14 14
15 import dwt.internal.image.LEDataOutputStream; 15 import dwt.internal.image.LEDataOutputStream;
16 import dwt.internal.image.PngDeflater; 16 import dwt.internal.image.PngDeflater;
17 import dwt.dwthelper.ByteArrayOutputStream; 17 import dwt.dwthelper.ByteArrayOutputStream;
18 import dwt.dwthelper.OutputStream;
18 import dwt.DWT; 19 import dwt.DWT;
19 import dwt.graphics.ImageData; 20 import dwt.graphics.ImageData;
20 import dwt.graphics.ImageLoader; 21 import dwt.graphics.ImageLoader;
21 import dwt.graphics.RGB; 22 import dwt.graphics.RGB;
23 import dwt.internal.Compatibility;
22 import dwt.internal.image.PngChunk; 24 import dwt.internal.image.PngChunk;
23 25
24 import tango.core.Exception; 26 import tango.core.Exception;
25 27
26 final class PngEncoder { 28 final class PngEncoder {
234 } 236 }
235 237
236 void writeImageData() { 238 void writeImageData() {
237 239
238 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); 240 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
241 OutputStream os = Compatibility.newDeflaterOutputStream(baos);
242 if (os is null) os = baos;
239 243
240 if (colorType is 3) { 244 if (colorType is 3) {
241 245
246 byte[] lineData = new byte[width];
247
248 for (int y = 0; y < height; y++) {
249
250 int filter = 0;
251 os.write(filter);
252
253 data.getPixels(0, y, width, lineData, 0);
254
255 for (int x = 0; x < lineData.length; x++) {
256
257 os.write(lineData[x]);
258
259 }
260
261 }
262
263 }
264
265 else {
266
242 int[] lineData = new int[width]; 267 int[] lineData = new int[width];
243 268 byte[] alphaData = null;
244 for (int y = 0; y < height; y++) { 269 if (colorType is 6) {
245 270 alphaData = new byte[width];
246 byte filter[] = [0]; 271 }
247 baos.write(filter, 0, 1);
248
249 data.getPixels(0, y, width, lineData, 0);
250
251 for (int x = 0; x < lineData.length; x++) {
252
253 baos.write(cast(byte) lineData[x]);
254
255 }
256
257 }
258
259 }
260
261 else {
262
263 int[] lineData = new int[width];
264 byte[] alphaData = new byte[width];
265 272
266 int redMask = data.palette.redMask; 273 int redMask = data.palette.redMask;
267 int redShift = data.palette.redShift; 274 int redShift = data.palette.redShift;
268 int greenMask = data.palette.greenMask; 275 int greenMask = data.palette.greenMask;
269 int greenShift = data.palette.greenShift; 276 int greenShift = data.palette.greenShift;
270 int blueShift = data.palette.blueShift; 277 int blueShift = data.palette.blueShift;
271 int blueMask = data.palette.blueMask; 278 int blueMask = data.palette.blueMask;
272 279
273 for (int y = 0; y < height; y++) { 280 for (int y = 0; y < height; y++) {
274 281
275 byte filter[] = [0]; 282 int filter = 0;
276 baos.write(filter, 0, 1); 283 os.write(filter);
277 284
278 data.getPixels(0, y, width, lineData, 0); 285 data.getPixels(0, y, width, lineData, 0);
279 286
280 if (colorType is 6) { 287 if (colorType is 6) {
281 data.getAlphas(0, y, width, alphaData, 0); 288 data.getAlphas(0, y, width, alphaData, 0);
290 int g = pixel & greenMask; 297 int g = pixel & greenMask;
291 g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; 298 g = (greenShift < 0) ? g >>> -greenShift : g << greenShift;
292 int b = pixel & blueMask; 299 int b = pixel & blueMask;
293 b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; 300 b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
294 301
295 byte pixels[] = [cast(byte) r, cast(byte) g, cast(byte) b]; 302 os.write(r);
296 baos.write(pixels, 0, 3); 303 os.write(g);
304 os.write(b);
297 305
298 if (colorType is 6) { 306 if (colorType is 6) {
299 307 os.write(alphaData[x]);
300 byte alpha[] = [alphaData[x]];
301 baos.write(alpha, 0, 1);
302
303 } 308 }
304 309
305 } 310 }
306 311
307 } 312 }
308 313
309 } 314 }
310 315
311 PngDeflater deflater = new PngDeflater(); 316 os.flush();
312 byte[] compressed = deflater.deflate(baos.toByteArray()); 317 os.close();
318
319 byte[] compressed = baos.toByteArray();
320 if (os is baos) {
321 PngDeflater deflater = new PngDeflater();
322 compressed = deflater.deflate(compressed);
323 }
313 324
314 writeChunk(TAG_IDAT, compressed); 325 writeChunk(TAG_IDAT, compressed);
315 326
316 } 327 }
317 328