1 | package org.expeditee.gio;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.HttpURLConnection;
|
---|
6 | import java.net.URL;
|
---|
7 | import java.util.Random;
|
---|
8 |
|
---|
9 | import org.expeditee.core.Colour;
|
---|
10 | import org.expeditee.core.Dimension;
|
---|
11 | import org.expeditee.core.Image;
|
---|
12 |
|
---|
13 | import com.sun.pdfview.PDFPage;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Manager in charge of creating and working with images in memory. Maintains a mapping
|
---|
17 | * from org.expeditee.core.Image handles to the underlying images, and provides methods
|
---|
18 | * for manipulating the underlying images through those handles.
|
---|
19 | *
|
---|
20 | * @author cts16
|
---|
21 | */
|
---|
22 | public abstract class ImageManager {
|
---|
23 |
|
---|
24 | /*
|
---|
25 | *
|
---|
26 | * Image-creation functions
|
---|
27 | *
|
---|
28 | */
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Creates an image of the given size in either main or video RAM.
|
---|
32 | *
|
---|
33 | * @param hintUseAcceleratedMemory
|
---|
34 | * True to use video RAM, false for main memory.
|
---|
35 | */
|
---|
36 | public Image createImage(Dimension size, boolean hintUseAcceleratedMemory)
|
---|
37 | {
|
---|
38 | if (size == null) return null;
|
---|
39 |
|
---|
40 | return createImage(size.width, size.height, hintUseAcceleratedMemory);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /** Creates an image in main memory with the given size. */
|
---|
44 | public Image createImage(Dimension size)
|
---|
45 | {
|
---|
46 | return createImage(size, false);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /** Creates an image in main memory with the given width and height. */
|
---|
50 | public Image createImage(int width, int height) {
|
---|
51 | return createImage(width, height, false);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Creates an image of the given width and height in either main or video RAM.
|
---|
56 | *
|
---|
57 | * @param hintUseAcceleratedMemory
|
---|
58 | * True to use video RAM, false for main memory.
|
---|
59 | */
|
---|
60 | public abstract Image createImage(int width, int height, boolean hintUseAcceleratedMemory);
|
---|
61 |
|
---|
62 | /** Creates an image from packed 32-bit ARGB data in an array. */
|
---|
63 | public abstract Image createImage(int width, int height, int[] pixelData);
|
---|
64 |
|
---|
65 | /** Creates an image of a PDF page. */
|
---|
66 | public abstract Image createImage(int width, int height, PDFPage page);
|
---|
67 |
|
---|
68 | /** Creates an image from the raw byte data read from a file. Must be in a format recognised by the implementing Ecosystem. **/
|
---|
69 | public abstract Image createImage(byte[] rawImageData);
|
---|
70 |
|
---|
71 | /** Creates a new image which is a cropped section of another image. */
|
---|
72 | public abstract Image createImageAsCroppedCopy(Image orig, int x, int y, int width, int height);
|
---|
73 |
|
---|
74 | /** Gets image data from a URL. */
|
---|
75 | public abstract Image getImage(URL url);
|
---|
76 |
|
---|
77 | /** Gets image data from a file. */
|
---|
78 | public abstract Image getImage(String filename);
|
---|
79 |
|
---|
80 | //public abstract Image getItem(byte[] imageData);
|
---|
81 |
|
---|
82 | /** Gets an image that is just noise */
|
---|
83 | public Image getNoise() {
|
---|
84 | int width = 100;
|
---|
85 | int height = 100;
|
---|
86 | int[] pixelData = new int[width * height];
|
---|
87 | Random rand = new Random();
|
---|
88 |
|
---|
89 | for (int i = 0; i < pixelData.length; i++) {
|
---|
90 | int alpha = rand.nextInt(256) << 24;
|
---|
91 | int red = rand.nextInt(256) << 16;
|
---|
92 | int green = rand.nextInt(256) << 8;
|
---|
93 | int blue = rand.nextInt(256);
|
---|
94 | pixelData[i] = alpha | red | green | blue;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return createImage(width, height, pixelData);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Gets image data from a HTTP connection. */
|
---|
101 | public abstract Image getImage(HttpURLConnection connection) throws IOException;
|
---|
102 |
|
---|
103 | /*
|
---|
104 | *
|
---|
105 | * Image-destruction functions
|
---|
106 | *
|
---|
107 | */
|
---|
108 |
|
---|
109 | /** Tells the image manager that the given image is no longer needed. */
|
---|
110 | public abstract void releaseImage(Image image);
|
---|
111 |
|
---|
112 | /*
|
---|
113 | *
|
---|
114 | * Image info getters
|
---|
115 | *
|
---|
116 | */
|
---|
117 |
|
---|
118 | /** Checks if the given handle corresponds to a real image in the image manager. */
|
---|
119 | public abstract boolean isImageValid(Image image);
|
---|
120 |
|
---|
121 | /** Gets the width of the given image. */
|
---|
122 | public abstract int getWidth(Image image);
|
---|
123 |
|
---|
124 | /** Gets the height of the given image. */
|
---|
125 | public abstract int getHeight(Image image);
|
---|
126 |
|
---|
127 | /** Gets the colours of an area of pixels from the image. */
|
---|
128 | public abstract Colour[] getPixels(Image image, int x, int y, int width, int height);
|
---|
129 |
|
---|
130 | /** Gets the colour of a single pixel in the image. */
|
---|
131 | public Colour getPixel(Image image, int x, int y)
|
---|
132 | {
|
---|
133 | Colour[] pixels = getPixels(image, x, y, 1, 1);
|
---|
134 |
|
---|
135 | if (pixels == null) return null;
|
---|
136 |
|
---|
137 | return pixels[0];
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | *
|
---|
142 | * Image-manipulation functions
|
---|
143 | *
|
---|
144 | */
|
---|
145 |
|
---|
146 | /** Sets the colour of a single pixel in the image. */
|
---|
147 | public abstract void setPixel(Image image, int x, int y, Colour c);
|
---|
148 |
|
---|
149 | /** Writes the image data to disk. Format should be png, bmp, etc. */
|
---|
150 | public abstract boolean writeImageToDisk(Image image, String format, File file) throws IOException;
|
---|
151 | }
|
---|