source: trunk/src/org/expeditee/gio/ImageManager.java@ 1426

Last change on this file since 1426 was 1426, checked in by bln4, 5 years ago

Implemented surrogates for images. When you add an encryption label to a picture, the default is a on-the-fly generated image of noise. This generated image has the same size specifications as the primary image.

File size: 4.0 KB
Line 
1package org.expeditee.gio;
2
3import java.io.File;
4import java.io.IOException;
5import java.net.HttpURLConnection;
6import java.net.URL;
7import java.util.Random;
8
9import org.expeditee.core.Colour;
10import org.expeditee.core.Dimension;
11import org.expeditee.core.Image;
12
13import 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 */
22public 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 /**
44 * Creates an image of the given width and height in either main or video RAM.
45 *
46 * @param hintUseAcceleratedMemory
47 * True to use video RAM, false for main memory.
48 */
49 public abstract Image createImage(int width, int height, boolean hintUseAcceleratedMemory);
50
51 /** Creates an image in main memory with the given size. */
52 public Image createImage(Dimension size)
53 {
54 return createImage(size, false);
55 }
56
57 /** Creates an image in main memory with the given width and height. */
58 public Image createImage(int width, int height)
59 {
60 return createImage(width, height, false);
61 }
62
63 /** Creates an image from packed 32-bit ARGB data in an array. */
64 public abstract Image createImage(int width, int height, int[] pixelData);
65
66 /** Creates an image of a PDF page. */
67 public abstract Image createImage(int width, int height, PDFPage page);
68
69 /** Creates a new image which is a cropped section of another image. */
70 public abstract Image createImageAsCroppedCopy(Image orig, int x, int y, int width, int height);
71
72 /** Gets image data from a URL. */
73 public abstract Image getImage(URL url);
74
75 /** Gets image data from a file. */
76 public abstract Image getImage(String filename);
77
78 /** Gets an image that is just noise */
79 public Image getNoise() {
80 int width = 100;
81 int height = 100;
82 int[] pixelData = new int[width * height];
83 Random rand = new Random();
84
85 for (int i = 0; i < pixelData.length; i++) {
86 int alpha = rand.nextInt(256) << 24;
87 int red = rand.nextInt(256) << 16;
88 int green = rand.nextInt(256) << 8;
89 int blue = rand.nextInt(256);
90 pixelData[i] = alpha | red | green | blue;
91 }
92
93 return createImage(width, height, pixelData);
94 }
95
96 /** Gets image data from a HTTP connection. */
97 public abstract Image getImage(HttpURLConnection connection) throws IOException;
98
99 /*
100 *
101 * Image-destruction functions
102 *
103 */
104
105 /** Tells the image manager that the given image is no longer needed. */
106 public abstract void releaseImage(Image image);
107
108 /*
109 *
110 * Image info getters
111 *
112 */
113
114 /** Checks if the given handle corresponds to a real image in the image manager. */
115 public abstract boolean isImageValid(Image image);
116
117 /** Gets the width of the given image. */
118 public abstract int getWidth(Image image);
119
120 /** Gets the height of the given image. */
121 public abstract int getHeight(Image image);
122
123 /** Gets the colours of an area of pixels from the image. */
124 public abstract Colour[] getPixels(Image image, int x, int y, int width, int height);
125
126 /** Gets the colour of a single pixel in the image. */
127 public Colour getPixel(Image image, int x, int y)
128 {
129 Colour[] pixels = getPixels(image, x, y, 1, 1);
130
131 if (pixels == null) return null;
132
133 return pixels[0];
134 }
135
136 /*
137 *
138 * Image-manipulation functions
139 *
140 */
141
142 /** Sets the colour of a single pixel in the image. */
143 public abstract void setPixel(Image image, int x, int y, Colour c);
144
145 /** Writes the image data to disk. Format should be png, bmp, etc. */
146 public abstract boolean writeImageToDisk(Image image, String format, File file) throws IOException;
147}
Note: See TracBrowser for help on using the repository browser.