52 |
import java.io.BufferedInputStream; |
import java.io.BufferedInputStream; |
53 |
import java.net.URL; |
import java.net.URL; |
54 |
import java.util.Hashtable; |
import java.util.Hashtable; |
55 |
|
import java.util.WeakHashMap; |
56 |
import java.util.Vector; |
import java.util.Vector; |
57 |
|
|
58 |
/** |
/** |
101 |
0x0000FF00, |
0x0000FF00, |
102 |
0x000000FF, |
0x000000FF, |
103 |
0xFF000000); |
0xFF000000); |
104 |
|
/** |
105 |
|
* HashMap of Graphics objects painting on this Image. |
106 |
|
*/ |
107 |
|
WeakHashMap painters; |
108 |
|
|
109 |
|
/** |
110 |
|
* Flags if this image is to be destroyed. |
111 |
|
*/ |
112 |
|
boolean killFlag; |
113 |
|
|
114 |
/** |
/** |
115 |
* Clears the image to RGBA 0 |
* Clears the image to RGBA 0 |
174 |
/** |
/** |
175 |
* Draws the image scaled flipped and optionally composited. |
* Draws the image scaled flipped and optionally composited. |
176 |
*/ |
*/ |
177 |
private native void drawPixelsScaledFlipped (QtGraphics gc, |
native void drawPixelsScaledFlipped (QtGraphics gc, |
178 |
int bg_red, int bg_green, |
int bg_red, int bg_green, |
179 |
int bg_blue, |
int bg_blue, |
180 |
boolean flipX, boolean flipY, |
boolean flipX, boolean flipY, |
181 |
int srcX, int srcY, |
int srcX, int srcY, |
182 |
int srcWidth, int srcHeight, |
int srcWidth, int srcHeight, |
183 |
int dstX, int dstY, |
int dstX, int dstY, |
184 |
int dstWidth, int dstHeight, |
int dstWidth, int dstHeight, |
185 |
boolean composite); |
boolean composite); |
186 |
|
|
187 |
/** |
/** |
188 |
* Creates the image from an ImageProducer. May result in an error image. |
* Creates the image from an ImageProducer. May result in an error image. |
189 |
*/ |
*/ |
190 |
public QtImage (ImageProducer producer) |
public QtImage (ImageProducer producer) |
191 |
{ |
{ |
192 |
|
killFlag = false; |
193 |
isLoaded = false; |
isLoaded = false; |
194 |
observers = new Vector(); |
observers = new Vector(); |
195 |
source = producer; |
source = producer; |
203 |
*/ |
*/ |
204 |
public QtImage (URL url) |
public QtImage (URL url) |
205 |
{ |
{ |
206 |
|
killFlag = false; |
207 |
isLoaded = false; |
isLoaded = false; |
208 |
observers = new Vector(); |
observers = new Vector(); |
209 |
errorLoading = false; |
errorLoading = false; |
210 |
|
if( url == null) |
211 |
|
return; |
212 |
ByteArrayOutputStream baos = new ByteArrayOutputStream( 5000 ); |
ByteArrayOutputStream baos = new ByteArrayOutputStream( 5000 ); |
213 |
try |
try |
214 |
{ |
{ |
240 |
*/ |
*/ |
241 |
public QtImage (String filename) |
public QtImage (String filename) |
242 |
{ |
{ |
243 |
|
killFlag = false; |
244 |
File f = new File(filename); |
File f = new File(filename); |
245 |
observers = null; |
observers = null; |
246 |
props = new Hashtable(); |
props = new Hashtable(); |
260 |
isLoaded = false; |
isLoaded = false; |
261 |
return; |
return; |
262 |
} |
} |
|
|
|
263 |
errorLoading = false; |
errorLoading = false; |
264 |
isLoaded = true; |
isLoaded = true; |
265 |
} |
} |
274 |
if (loadImageFromData(data) != true) |
if (loadImageFromData(data) != true) |
275 |
throw new IllegalArgumentException("Couldn't load image."); |
throw new IllegalArgumentException("Couldn't load image."); |
276 |
|
|
277 |
|
killFlag = false; |
278 |
isLoaded = true; |
isLoaded = true; |
279 |
observers = null; |
observers = null; |
280 |
errorLoading = false; |
errorLoading = false; |
290 |
this.height = height; |
this.height = height; |
291 |
props = new Hashtable(); |
props = new Hashtable(); |
292 |
isLoaded = true; |
isLoaded = true; |
293 |
|
killFlag = false; |
294 |
observers = null; |
observers = null; |
295 |
errorLoading = false; |
errorLoading = false; |
296 |
createImage(); |
createImage(); |
306 |
this.height = height; |
this.height = height; |
307 |
props = new Hashtable(); |
props = new Hashtable(); |
308 |
isLoaded = true; |
isLoaded = true; |
309 |
|
killFlag = false; |
310 |
observers = null; |
observers = null; |
311 |
errorLoading = false; |
errorLoading = false; |
312 |
|
|
373 |
0, width); |
0, width); |
374 |
} |
} |
375 |
|
|
376 |
|
void putPainter(QtImageGraphics g) |
377 |
|
{ |
378 |
|
if( painters == null ) |
379 |
|
painters = new WeakHashMap(); |
380 |
|
painters.put( g, "dummy" ); |
381 |
|
} |
382 |
|
|
383 |
|
void removePainter(QtImageGraphics g) |
384 |
|
{ |
385 |
|
painters.remove( g ); |
386 |
|
if( killFlag && painters.isEmpty() ) |
387 |
|
freeImage(); |
388 |
|
} |
389 |
|
|
390 |
/** |
/** |
391 |
* Creates a Graphics context for this image. |
* Creates a Graphics context for this image. |
392 |
*/ |
*/ |
393 |
public Graphics getGraphics () |
public Graphics getGraphics () |
394 |
{ |
{ |
395 |
if (!isLoaded) |
if (!isLoaded || killFlag) |
396 |
return null; |
return null; |
397 |
|
|
398 |
return new QtImageGraphics(this); |
return new QtImageGraphics(this); |
399 |
} |
} |
400 |
|
|
401 |
|
/** |
402 |
|
* Creates a Graphics context for this image. |
403 |
|
*/ |
404 |
|
Graphics getDirectGraphics(QtComponentPeer peer) |
405 |
|
{ |
406 |
|
if (!isLoaded) |
407 |
|
return null; |
408 |
|
|
409 |
|
return new QtImageDirectGraphics(this, peer); |
410 |
|
} |
411 |
|
|
412 |
/** |
/** |
413 |
* Returns a scaled instance of this image. |
* Returns a scaled instance of this image. |
444 |
|
|
445 |
public void finalize() |
public void finalize() |
446 |
{ |
{ |
447 |
if (isLoaded) |
dispose(); |
|
freeImage(); |
|
448 |
} |
} |
449 |
|
|
450 |
public void dispose() |
public void dispose() |
451 |
{ |
{ |
452 |
finalize(); |
if (isLoaded) |
453 |
|
{ |
454 |
|
if( painters == null || painters.isEmpty() ) |
455 |
|
freeImage(); |
456 |
|
else |
457 |
|
killFlag = true; // can't destroy image yet. |
458 |
|
// Do so when all painters are gone. |
459 |
|
} |
460 |
} |
} |
461 |
|
|
462 |
/** |
/** |
633 |
} |
} |
634 |
return false; |
return false; |
635 |
} |
} |
636 |
|
|
637 |
|
public String toString() |
638 |
|
{ |
639 |
|
return "QtImage [isLoaded="+isLoaded+", width="+width+", height="+height |
640 |
|
+"]"; |
641 |
|
} |
642 |
} |
} |