1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
/* GdkPixbufDecoder.java -- Image data decoding object
Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.java.awt.peer.gtk;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DirectColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Vector;
import javax.imageio.IIOImage;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import gnu.classpath.Configuration;
import gnu.classpath.Pointer;
public class GdkPixbufDecoder extends gnu.java.awt.image.ImageDecoder
{
static
{
if (true) // GCJ LOCAL
{
System.loadLibrary("gtkpeer");
}
initStaticState ();
}
/**
* Lock that should be held for all gdkpixbuf operations. We don't use
* the global gdk_threads_enter/leave functions since gdkpixbuf
* operations can be done in parallel to drawing and manipulating gtk
* widgets.
*/
static Object pixbufLock = new Object();
static native void initStaticState();
private final int native_state = GtkGenericPeer.getUniqueInteger ();
// initState() has been called, but pumpDone() has not yet been called.
private boolean needsClose = false;
// the current set of ImageConsumers for this decoder
Vector curr;
/**
* The pointer to the native pixbuf loader.
*
* This field is manipulated by native code. Don't change or remove
* without adjusting the native code.
*/
private Pointer nativeDecoder;
// interface to GdkPixbuf
// These native functions should be called with the pixbufLock held.
native void initState ();
native void pumpBytes (byte[] bytes, int len) throws IOException;
native void pumpDone () throws IOException;
native void finish (boolean needsClose);
/**
* Converts given image to bytes.
* Will call the GdkPixbufWriter for each chunk.
*/
static native void streamImage(int[] bytes, String format,
int width, int height,
boolean hasAlpha, GdkPixbufWriter writer);
// gdk-pixbuf provids data in RGBA format
static final ColorModel cm = new DirectColorModel (32, 0xff000000,
0x00ff0000,
0x0000ff00,
0x000000ff);
public GdkPixbufDecoder (DataInput datainput)
{
super (datainput);
}
public GdkPixbufDecoder (InputStream in)
{
super (in);
}
public GdkPixbufDecoder (String filename)
{
super (filename);
}
public GdkPixbufDecoder (URL url)
{
super (url);
}
public GdkPixbufDecoder (byte[] imagedata, int imageoffset, int imagelength)
{
super (imagedata, imageoffset, imagelength);
}
// called back by native side: area_prepared_cb
void areaPrepared (int width, int height)
{
if (curr == null)
return;
for (int i = 0; i < curr.size (); i++)
{
ImageConsumer ic = (ImageConsumer) curr.elementAt (i);
ic.setDimensions (width, height);
ic.setColorModel (cm);
ic.setHints (ImageConsumer.RANDOMPIXELORDER);
}
}
// called back by native side: area_updated_cb
void areaUpdated (int x, int y, int width, int height,
int pixels[], int scansize)
{
if (curr == null)
return;
for (int i = 0; i < curr.size (); i++)
{
ImageConsumer ic = (ImageConsumer) curr.elementAt (i);
ic.setPixels (x, y, width, height, cm, pixels, 0, scansize);
}
}
// called from an async image loader of one sort or another, this method
// repeatedly reads bytes from the input stream and passes them through a
// GdkPixbufLoader using the native method pumpBytes. pumpBytes in turn
// decodes the image data and calls back areaPrepared and areaUpdated on
// this object, feeding back decoded pixel blocks, which we pass to each
// of the ImageConsumers in the provided Vector.
public void produce (Vector v, InputStream is) throws IOException
{
curr = v;
byte bytes[] = new byte[4096];
int len = 0;
synchronized(pixbufLock)
{
initState();
}
needsClose = true;
// Note: We don't want the pixbufLock while reading from the InputStream.
while ((len = is.read (bytes)) != -1)
{
synchronized(pixbufLock)
{
pumpBytes (bytes, len);
}
}
synchronized(pixbufLock)
{
pumpDone();
}
needsClose = false;
for (int i = 0; i < curr.size (); i++)
{
ImageConsumer ic = (ImageConsumer) curr.elementAt (i);
ic.imageComplete (ImageConsumer.STATICIMAGEDONE);
}
curr = null;
}
public void finalize()
{
synchronized(pixbufLock)
{
finish(needsClose);
}
}
public static class ImageFormatSpec
{
public String name;
public boolean writable = false;
public ArrayList<String> mimeTypes = new ArrayList<String>();
public ArrayList<String> extensions = new ArrayList<String>();
public ImageFormatSpec(String name, boolean writable)
{
this.name = name;
this.writable = writable;
}
public synchronized void addMimeType(String m)
{
mimeTypes.add(m);
}
public synchronized void addExtension(String e)
{
extensions.add(e);
}
}
static ArrayList<ImageFormatSpec> imageFormatSpecs;
public static ImageFormatSpec registerFormat(String name, boolean writable)
{
ImageFormatSpec ifs = new ImageFormatSpec(name, writable);
synchronized(GdkPixbufDecoder.class)
{
if (imageFormatSpecs == null)
imageFormatSpecs = new ArrayList<ImageFormatSpec>();
imageFormatSpecs.add(ifs);
}
return ifs;
}
static String[] getFormatNames(boolean writable)
{
ArrayList<String> names = new ArrayList<String>();
synchronized (imageFormatSpecs)
{
Iterator<ImageFormatSpec> i = imageFormatSpecs.iterator();
while (i.hasNext())
{
ImageFormatSpec ifs = i.next();
if (writable && !ifs.writable)
continue;
names.add(ifs.name);
/*
* In order to make the filtering code work, we need to register
* this type under every "format name" likely to be used as a synonym.
* This generally means "all the extensions people might use".
*/
Iterator<String> j = ifs.extensions.iterator();
while (j.hasNext())
names.add(j.next());
}
}
return names.toArray(new String[names.size()]);
}
static String[] getFormatExtensions(boolean writable)
{
ArrayList<String> extensions = new ArrayList<String>();
synchronized (imageFormatSpecs)
{
Iterator<ImageFormatSpec> i = imageFormatSpecs.iterator();
while (i.hasNext())
{
ImageFormatSpec ifs = i.next();
if (writable && !ifs.writable)
continue;
Iterator<String> j = ifs.extensions.iterator();
while (j.hasNext())
extensions.add(j.next());
}
}
return extensions.toArray(new String[extensions.size()]);
}
static String[] getFormatMimeTypes(boolean writable)
{
ArrayList<String> mimeTypes = new ArrayList<String>();
synchronized (imageFormatSpecs)
{
Iterator<ImageFormatSpec> i = imageFormatSpecs.iterator();
while (i.hasNext())
{
ImageFormatSpec ifs = i.next();
if (writable && !ifs.writable)
continue;
Iterator<String> j = ifs.mimeTypes.iterator();
while (j.hasNext())
mimeTypes.add(j.next());
}
}
return mimeTypes.toArray(new String[mimeTypes.size()]);
}
static String findFormatName(Object ext, boolean needWritable)
{
if (ext == null)
return null;
if (!(ext instanceof String))
throw new IllegalArgumentException("extension is not a string");
String str = (String) ext;
Iterator<ImageFormatSpec> i = imageFormatSpecs.iterator();
while (i.hasNext())
{
ImageFormatSpec ifs = i.next();
if (needWritable && !ifs.writable)
continue;
if (ifs.name.equals(str))
return str;
Iterator<String> j = ifs.extensions.iterator();
while (j.hasNext())
{
String extension = j.next();
if (extension.equals(str))
return ifs.name;
}
j = ifs.mimeTypes.iterator();
while (j.hasNext())
{
String mimeType = j.next();
if (mimeType.equals(str))
return ifs.name;
}
}
throw new IllegalArgumentException("unknown extension '" + str + "'");
}
private static GdkPixbufReaderSpi readerSpi;
private static GdkPixbufWriterSpi writerSpi;
public static synchronized GdkPixbufReaderSpi getReaderSpi()
{
if (readerSpi == null)
readerSpi = new GdkPixbufReaderSpi();
return readerSpi;
}
public static synchronized GdkPixbufWriterSpi getWriterSpi()
{
if (writerSpi == null)
writerSpi = new GdkPixbufWriterSpi();
return writerSpi;
}
public static void registerSpis(IIORegistry reg)
{
reg.registerServiceProvider(getReaderSpi(), ImageReaderSpi.class);
reg.registerServiceProvider(getWriterSpi(), ImageWriterSpi.class);
}
public static class GdkPixbufWriterSpi extends ImageWriterSpi
{
public GdkPixbufWriterSpi()
{
super("GdkPixbuf", "2.x",
GdkPixbufDecoder.getFormatNames(true),
GdkPixbufDecoder.getFormatExtensions(true),
GdkPixbufDecoder.getFormatMimeTypes(true),
"gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufWriter",
new Class[] { ImageOutputStream.class },
new String[] { "gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufReaderSpi" },
false, null, null, null, null,
false, null, null, null, null);
}
public boolean canEncodeImage(ImageTypeSpecifier ts)
{
return true;
}
public ImageWriter createWriterInstance(Object ext)
{
return new GdkPixbufWriter(this, ext);
}
public String getDescription(java.util.Locale loc)
{
return "GdkPixbuf Writer SPI";
}
}
public static class GdkPixbufReaderSpi extends ImageReaderSpi
{
public GdkPixbufReaderSpi()
{
super("GdkPixbuf", "2.x",
GdkPixbufDecoder.getFormatNames(false),
GdkPixbufDecoder.getFormatExtensions(false),
GdkPixbufDecoder.getFormatMimeTypes(false),
"gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufReader",
new Class[] { ImageInputStream.class },
new String[] { "gnu.java.awt.peer.gtk.GdkPixbufDecoder$GdkPixbufWriterSpi" },
false, null, null, null, null,
false, null, null, null, null);
}
public boolean canDecodeInput(Object obj)
{
return true;
}
public ImageReader createReaderInstance(Object ext)
{
return new GdkPixbufReader(this, ext);
}
public String getDescription(Locale loc)
{
return "GdkPixbuf Reader SPI";
}
}
private static class GdkPixbufWriter
extends ImageWriter implements Runnable
{
String ext;
public GdkPixbufWriter(GdkPixbufWriterSpi ownerSpi, Object ext)
{
super(ownerSpi);
this.ext = findFormatName(ext, true);
}
public IIOMetadata convertImageMetadata (IIOMetadata inData,
ImageTypeSpecifier imageType,
ImageWriteParam param)
{
return null;
}
public IIOMetadata convertStreamMetadata (IIOMetadata inData,
ImageWriteParam param)
{
return null;
}
public IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType,
ImageWriteParam param)
{
return null;
}
public IIOMetadata getDefaultStreamMetadata (ImageWriteParam param)
{
return null;
}
public void write (IIOMetadata streamMetadata, IIOImage i, ImageWriteParam param)
throws IOException
{
RenderedImage image = i.getRenderedImage();
Raster ras = image.getData();
int width = ras.getWidth();
int height = ras.getHeight();
ColorModel model = image.getColorModel();
int[] pixels = CairoGraphics2D.findSimpleIntegerArray (image.getColorModel(), ras);
if (pixels == null)
{
BufferedImage img;
if(model != null && model.hasAlpha())
img = CairoSurface.getBufferedImage(width, height);
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] pix = new int[4];
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
img.setRGB(x, y, model.getRGB(ras.getPixel(x, y, pix)));
pixels = CairoGraphics2D.findSimpleIntegerArray (img.getColorModel(),
img.getRaster());
model = img.getColorModel();
}
Thread workerThread = new Thread(this, "GdkPixbufWriter");
workerThread.start();
processImageStarted(1);
synchronized(pixbufLock)
{
streamImage(pixels, this.ext, width, height, model.hasAlpha(),
this);
}
synchronized(data)
{
data.add(DATADONE);
data.notifyAll();
}
while (workerThread.isAlive())
{
try
{
workerThread.join();
}
catch (InterruptedException ioe)
{
// Ignored.
}
}
if (exception != null)
throw exception;
processImageComplete();
}
/**
* Object marking end of data from native streamImage code.
*/
private static final Object DATADONE = new Object();
/**
* Holds the data gotten from the native streamImage code.
* A worker thread will pull data out.
* Needs to be synchronized for access.
* The special object DATADONE is added when all data has been delivered.
*/
private ArrayList<Object> data = new ArrayList<Object>();
/**
* Holds any IOException thrown by the run method that needs
* to be rethrown by the write method.
*/
private IOException exception;
/** Callback for streamImage native code. **/
private void write(byte[] bs)
{
synchronized(data)
{
data.add(bs);
data.notifyAll();
}
}
public void run()
{
boolean done = false;
while (!done)
{
synchronized(data)
{
while (data.isEmpty())
{
try
{
data.wait();
}
catch (InterruptedException ie)
{
/* ignore */
}
}
Object o = data.remove(0);
if (o == DATADONE)
done = true;
else
{
DataOutput out = (DataOutput) getOutput();
try
{
out.write((byte[]) o);
}
catch (IOException ioe)
{
// We are only interested in the first exception.
if (exception == null)
exception = ioe;
}
}
}
}
}
}
private static class GdkPixbufReader
extends ImageReader
implements ImageConsumer
{
// ImageConsumer parts
GdkPixbufDecoder dec;
BufferedImage bufferedImage;
ColorModel defaultModel;
int width;
int height;
String ext;
public GdkPixbufReader(GdkPixbufReaderSpi ownerSpi, Object ext)
{
super(ownerSpi);
this.ext = findFormatName(ext, false);
}
public GdkPixbufReader(GdkPixbufReaderSpi ownerSpi, Object ext,
GdkPixbufDecoder d)
{
this(ownerSpi, ext);
dec = d;
}
public void setDimensions(int w, int h)
{
processImageStarted(1);
width = w;
height = h;
}
public void setProperties(Hashtable props) {}
public void setColorModel(ColorModel model)
{
defaultModel = model;
}
public void setHints(int flags) {}
public void setPixels(int x, int y, int w, int h,
ColorModel model, byte[] pixels,
int offset, int scansize)
{
}
public void setPixels(int x, int y, int w, int h,
ColorModel model, int[] pixels,
int offset, int scansize)
{
if (model == null)
model = defaultModel;
if (bufferedImage == null)
{
if(model != null && model.hasAlpha())
bufferedImage = new BufferedImage (width, height,
BufferedImage.TYPE_INT_ARGB);
else
bufferedImage = new BufferedImage (width, height,
BufferedImage.TYPE_INT_RGB);
}
int pixels2[];
if (model != null)
{
pixels2 = new int[pixels.length];
for (int yy = 0; yy < h; yy++)
for (int xx = 0; xx < w; xx++)
{
int i = yy * scansize + xx;
pixels2[i] = model.getRGB (pixels[i]);
}
}
else
pixels2 = pixels;
bufferedImage.setRGB (x, y, w, h, pixels2, offset, scansize);
processImageProgress(y / (height == 0 ? 1 : height));
}
public void imageComplete(int status)
{
processImageComplete();
}
public BufferedImage getBufferedImage()
{
if (bufferedImage == null && dec != null)
dec.startProduction (this);
return bufferedImage;
}
// ImageReader parts
public int getNumImages(boolean allowSearch)
throws IOException
{
return 1;
}
public IIOMetadata getImageMetadata(int i)
{
return null;
}
public IIOMetadata getStreamMetadata()
throws IOException
{
return null;
}
public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex)
throws IOException
{
BufferedImage img = getBufferedImage();
Vector<ImageTypeSpecifier> vec = new Vector<ImageTypeSpecifier>();
vec.add(new ImageTypeSpecifier(img));
return vec.iterator();
}
public int getHeight(int imageIndex)
throws IOException
{
return getBufferedImage().getHeight();
}
public int getWidth(int imageIndex)
throws IOException
{
return getBufferedImage().getWidth();
}
public void setInput(Object input,
boolean seekForwardOnly,
boolean ignoreMetadata)
{
super.setInput(input, seekForwardOnly, ignoreMetadata);
Object get = getInput();
if (get instanceof InputStream)
dec = new GdkPixbufDecoder((InputStream) get);
else if (get instanceof DataInput)
dec = new GdkPixbufDecoder((DataInput) get);
else
throw new IllegalArgumentException("input object not supported: "
+ get);
}
public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException
{
return getBufferedImage ();
}
}
}
|