1 22 23 package org.meshcms.util; 24 25 import java.awt.*; 26 import java.awt.image.*; 27 import java.util.*; 28 29 34 public class ImageLoader implements ImageConsumer { 35 boolean completed, notStarted = true; 36 Image imgImage; 37 int[] imgPixels; 38 int width, height; 39 40 45 public ImageLoader(String imageFilePath) { 46 imgImage = Toolkit.getDefaultToolkit().getImage(imageFilePath); 47 } 48 49 54 public boolean check() { 55 if (notStarted) { 56 imgImage.getSource().startProduction(this); 57 notStarted = false; 58 } 59 60 return completed; 61 } 62 63 66 public int getWidth() { 67 return width; 68 } 69 70 73 public int getHeight() { 74 return height; 75 } 76 77 public void setDimensions(int width, int height) { 78 this.width = width; 79 this.height = height; 80 imgPixels = new int[width * height]; 81 } 82 83 public void setProperties(Hashtable props) { 84 } 86 87 public void setColorModel(ColorModel model) { 88 } 90 91 public void setHints(int hintflags) { 92 } 94 95 public void setPixels(int x, int y, int w, int h, ColorModel model, 96 byte pixels[], int off, int scansize) { 97 int palette = 1; 98 int sx; 99 100 if (model instanceof IndexColorModel) { 101 palette = ((IndexColorModel) model).getMapSize(); 102 } 103 104 for (int i = 0; i < h; i++) { 105 sx = off; 106 107 for (int j = 0; j < w; j++) { 108 int idx = pixels[sx++]; 109 110 while (idx < 0) { 111 idx += palette; 112 } 113 114 imgPixels[(i + y) * width + j + x] = model.getRGB(idx % palette); 115 } 116 117 off += scansize; 118 } 119 } 120 121 public void setPixels(int x, int y, int w, int h, ColorModel model, 122 int pixels[], int off, int scansize) { 123 int sx; 124 125 for (int i = 0; i < h; i++) { 126 sx = off; 127 128 for (int j = 0; j < w; j++) { 129 imgPixels[(i + y) * width + j + x] = model.getRGB(pixels[sx++]); 130 } 131 132 off += scansize; 133 } 134 } 135 136 public void imageComplete(int status) { 137 completed = true; 138 imgImage.getSource().removeConsumer(this); 139 imgImage.flush(); 140 imgImage = null; 141 } 142 143 148 public Image getImage() { 149 waitForImage(); 150 151 return Toolkit.getDefaultToolkit().createImage( 152 new MemoryImageSource(width, height, imgPixels, 0, width) 153 ); 154 } 155 156 161 public int[] getPixels() { 162 waitForImage(); 163 return imgPixels; 164 } 165 166 169 public void waitForImage() { 170 while (!check()) { 171 try { 172 Thread.sleep(500L); 173 } catch (InterruptedException ex) {} 174 } 175 } 176 } 177 | Popular Tags |