001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package graphical;
012:
013: import netscape.application.AWTCompatibility;
014: import netscape.application.Bitmap;
015: import netscape.application.Image;
016:
017: import java.awt.Dimension;
018: import java.net.URL;
019: import java.net.MalformedURLException;
020:
021: import util.ReportError;
022:
023: // SGP: lots of debugging strings in here
024:
025: /**
026: Handle images.
027: *
028: */
029: public class BitmapHandle {
030: private Bitmap bitmap;
031: private Dimension dimension;
032:
033: private String url;
034:
035: public BitmapHandle(String url) {
036: this .url = url;
037:
038: try {
039: bitmap = Bitmap.bitmapFromURL(new URL(url));
040:
041: } catch (MalformedURLException e) {
042: ReportError.reportError(ReportError.ADMIN, "BitmapHandle",
043: "grabPixels", Messages.CANTACCESSURL + ": " + url);
044: bitmap = null;
045: dimension = new Dimension();
046: }
047: }
048:
049: public Bitmap bitmap() {
050: bitmap.loadData();
051: return bitmap;
052: }
053:
054: public int[] grabPixels() {
055: return grabPixels(Header.GRABPIXELSTIMEOUT);
056: }
057:
058: public int[] grabPixels(long timeout) {
059: bitmap.loadData();
060:
061: int i = 0;
062: while ((!bitmap.hasLoadedData())
063: && (i < Header.BITMAPMAXREREAD)) {
064: System.out.println("try " + i + " " + url);
065: try {
066: Thread.sleep(Header.BITMAPMAXRETRYSLEEP);
067: } catch (InterruptedException e) {
068: break;
069: }
070:
071: i++;
072: }
073:
074: dimension = new Dimension(bitmap.width(), bitmap.height());
075: int pixels[] = new int[dimension.width * dimension.height];
076:
077: /*
078: System.out.println(
079: "url: "
080: + bitmap.hasLoadedData()
081: + " "
082: + dimension
083: );
084: */
085:
086: // if ( !bitmap.grabPixels( pixels ) )
087: if (!grabPixels(pixels, timeout)) {
088: ReportError.reportError(ReportError.SYSTEM, "BitmapHandle",
089: "grabPixels", Messages.IMAGENOTOBTAINED + ": "
090: + url);
091: return null;
092: } else {
093: // SGP: remove
094: System.out.println(Messages.IMAGEOBTAINED + ": " + url);
095: }
096:
097: return pixels;
098: }
099:
100: private boolean grabPixels(int pixels[], long timeout) {
101: java.awt.image.PixelGrabber grabber;
102: java.awt.Image image;
103: boolean status;
104:
105: image = AWTCompatibility.awtImageForBitmap(bitmap);
106: grabber = new java.awt.image.PixelGrabber(image, 0, 0, bitmap
107: .width(), bitmap.height(), pixels, 0, bitmap.width());
108:
109: try {
110: status = grabber.grabPixels(timeout);
111: } catch (InterruptedException e) {
112: ReportError.reportError(ReportError.SYSTEM, "BitmapHandle",
113: "grabPixels", url + " " + Messages.PIXELGRABFAIL);
114: status = false;
115: }
116:
117: return status;
118: }
119:
120: /**
121: * Minimum size.
122: */
123: public Dimension minimumSize() {
124: bitmap.loadData();
125: return dimension;
126: }
127:
128: /**
129: * Preferred size.
130: */
131: public Dimension preferredSize() {
132: bitmap.loadData();
133: return dimension;
134: }
135:
136: /**
137: * Size.
138: */
139: public Dimension size() {
140: bitmap.loadData();
141: return dimension;
142: }
143:
144: /**
145: * Static method that returns a bitmap at the passed URL
146: * without instantiating an object.
147: * On failure, returns a new default Bitmap instance.
148: * @param url URL of bitmap
149: */
150: public static Bitmap acquireBitmap(String url) {
151: try {
152: return Bitmap.bitmapFromURL(new URL(url));
153:
154: } catch (MalformedURLException e) {
155: ReportError.reportError(ReportError.ADMIN, "BitmapHandle",
156: "acquireBitmap", Messages.CANTACCESSURL + ": "
157: + url);
158: return new Bitmap();
159: }
160: }
161:
162: public static boolean checkDimensions(Bitmap b, Dimension d) {
163: if (b == null) {
164: ReportError.reportError(ReportError.ADMIN, "BitmapHandle",
165: "checkDimensions", Messages.IMAGENULL);
166: return false;
167: }
168:
169: if ((b.width() != d.width) || (b.height() != d.height)) {
170: ReportError.reportError(ReportError.ADMIN, "BitmapHandle",
171: "checkDimensions", Messages.IMAGEBADSIZE + ": ("
172: + b.width() + "," + b.height() + ") != "
173: + d.toString());
174: return false;
175: }
176:
177: return true;
178: }
179:
180: public static int[] pixelGrab(Bitmap bitmap, int pixelCount) {
181: int pixels[] = new int[pixelCount];
182: if (!bitmap.grabPixels(pixels)) {
183: ReportError.reportError(ReportError.ADMIN, "BitmapHandle",
184: "pixelGrab", Messages.PIXELGRABFAIL);
185: }
186:
187: return pixels;
188: }
189:
190: public String toString() {
191: return "BitmapHandle:" + " (" + Header.VERSION + ")" + "\n"
192: + "\t" + "bitmap: " + bitmap.toString() + "\n" + "\t"
193: + "dimension: " + dimension.toString() + "\n";
194: }
195: }
|