54 * The width of the source image. 55 */ 56 protected int srcWidth; 57 58 /** 59 * The height of the source image. 60 */ 61 protected int srcHeight; 62 63 /** 64 * The target width to scale the image. 65 */ 66 protected int destWidth; 67 68 /** 69 * The target height to scale the image. 70 */ 71 protected int destHeight; 72 73 /** 74 * An <code>int</code> array containing information about a 75 * row of pixels. 76 */ 77 protected int srcrows[]; 78 79 /** 80 * An <code>int</code> array containing information about a 81 * column of pixels. 82 */ 83 protected int srccols[]; 84 85 /** 86 * A <code>byte</code> array initialized with a size of 87 * {@link #destWidth} and used to deliver a row of pixel 88 * data to the {@link ImageConsumer}. 89 */ 90 protected Object outpixbuf; 91 92 /** 93 * Constructs a ReplicateScaleFilter that scales the pixels from 94 * its source Image as specified by the width and height parameters. 95 * @param width the target width to scale the image 96 * @param height the target height to scale the image 97 * @throws IllegalArgumentException if <code>width</code> equals 98 * zero or <code>height</code> equals zero 99 */ 100 public ReplicateScaleFilter(int width, int height) { 101 if (width == 0 || height == 0) { 102 throw new IllegalArgumentException("Width ("+width+ 103 ") and height ("+height+ 104 ") must be non-zero"); 105 } 106 destWidth = width; 107 destHeight = height; 108 } 109 110 /** 111 * Passes along the properties from the source object after adding a 112 * property indicating the scale applied. 113 * This method invokes <code>super.setProperties</code>, 114 * which might result in additional properties being added. 115 * <p> 116 * Note: This method is intended to be called by the 117 * <code>ImageProducer</code> of the <code>Image</code> whose pixels 118 * are being filtered. Developers using 119 * this class to filter pixels from an image should avoid calling 120 * this method directly since that operation could interfere 121 * with the filtering operation. 122 */ 123 public void setProperties(Hashtable<?,?> props) { 124 @SuppressWarnings("unchecked") 125 Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone(); 126 String key = "rescale"; 127 String val = destWidth + "x" + destHeight; 128 Object o = p.get(key); 129 if (o != null && o instanceof String) { 130 val = ((String) o) + ", " + val; 131 } 132 p.put(key, val); 133 super.setProperties(p); 134 } 135 136 /** 137 * Override the dimensions of the source image and pass the dimensions 138 * of the new scaled size to the ImageConsumer. 139 * <p> 140 * Note: This method is intended to be called by the 141 * <code>ImageProducer</code> of the <code>Image</code> whose pixels 142 * are being filtered. Developers using 143 * this class to filter pixels from an image should avoid calling 144 * this method directly since that operation could interfere 145 * with the filtering operation. 146 * @see ImageConsumer 147 */ 148 public void setDimensions(int w, int h) { 149 srcWidth = w; 150 srcHeight = h; 151 if (destWidth < 0) { 152 if (destHeight < 0) { 153 destWidth = srcWidth; 154 destHeight = srcHeight; 155 } else { 156 destWidth = srcWidth * destHeight / srcHeight; 157 } 158 } else if (destHeight < 0) { 159 destHeight = srcHeight * destWidth / srcWidth; 160 } 161 consumer.setDimensions(destWidth, destHeight); 162 } 163 164 private void calculateMaps() { 165 srcrows = new int[destHeight + 1]; 166 for (int y = 0; y <= destHeight; y++) { 167 srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight); 168 } 169 srccols = new int[destWidth + 1]; 170 for (int x = 0; x <= destWidth; x++) { 171 srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth); 172 } 173 } 174 175 /** 176 * Choose which rows and columns of the delivered byte pixels are 177 * needed for the destination scaled image and pass through just 178 * those rows and columns that are needed, replicated as necessary. 179 * <p> 180 * Note: This method is intended to be called by the 181 * <code>ImageProducer</code> of the <code>Image</code> whose pixels 182 * are being filtered. Developers using 183 * this class to filter pixels from an image should avoid calling 184 * this method directly since that operation could interfere 185 * with the filtering operation. 186 */ 187 public void setPixels(int x, int y, int w, int h, 188 ColorModel model, byte pixels[], int off, 189 int scansize) { 190 if (srcrows == null || srccols == null) { 191 calculateMaps(); 192 } 193 int sx, sy; 194 int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); 195 int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); 196 byte outpix[]; 197 if (outpixbuf != null && outpixbuf instanceof byte[]) { 198 outpix = (byte[]) outpixbuf; 199 } else { 200 outpix = new byte[destWidth]; 201 outpixbuf = outpix; 202 } 203 for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) { 204 int srcoff = off + scansize * (sy - y); 205 int dx; 206 for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) { 207 outpix[dx] = pixels[srcoff + sx - x]; 208 } 209 if (dx > dx1) { 210 consumer.setPixels(dx1, dy, dx - dx1, 1, 211 model, outpix, dx1, destWidth); 212 } 213 } 214 } 215 216 /** 217 * Choose which rows and columns of the delivered int pixels are 218 * needed for the destination scaled image and pass through just 219 * those rows and columns that are needed, replicated as necessary. 220 * <p> 221 * Note: This method is intended to be called by the 222 * <code>ImageProducer</code> of the <code>Image</code> whose pixels 223 * are being filtered. Developers using 224 * this class to filter pixels from an image should avoid calling 225 * this method directly since that operation could interfere 226 * with the filtering operation. 227 */ 228 public void setPixels(int x, int y, int w, int h, 229 ColorModel model, int pixels[], int off, 230 int scansize) { 231 if (srcrows == null || srccols == null) { 232 calculateMaps(); 233 } 234 int sx, sy; 235 int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); 236 int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); 237 int outpix[]; 238 if (outpixbuf != null && outpixbuf instanceof int[]) { 239 outpix = (int[]) outpixbuf; 240 } else { 241 outpix = new int[destWidth]; 242 outpixbuf = outpix; | 54 * The width of the source image. 55 */ 56 protected int srcWidth; 57 58 /** 59 * The height of the source image. 60 */ 61 protected int srcHeight; 62 63 /** 64 * The target width to scale the image. 65 */ 66 protected int destWidth; 67 68 /** 69 * The target height to scale the image. 70 */ 71 protected int destHeight; 72 73 /** 74 * An {@code int} array containing information about a 75 * row of pixels. 76 */ 77 protected int srcrows[]; 78 79 /** 80 * An {@code int} array containing information about a 81 * column of pixels. 82 */ 83 protected int srccols[]; 84 85 /** 86 * A {@code byte} array initialized with a size of 87 * {@link #destWidth} and used to deliver a row of pixel 88 * data to the {@link ImageConsumer}. 89 */ 90 protected Object outpixbuf; 91 92 /** 93 * Constructs a ReplicateScaleFilter that scales the pixels from 94 * its source Image as specified by the width and height parameters. 95 * @param width the target width to scale the image 96 * @param height the target height to scale the image 97 * @throws IllegalArgumentException if {@code width} equals 98 * zero or {@code height} equals zero 99 */ 100 public ReplicateScaleFilter(int width, int height) { 101 if (width == 0 || height == 0) { 102 throw new IllegalArgumentException("Width ("+width+ 103 ") and height ("+height+ 104 ") must be non-zero"); 105 } 106 destWidth = width; 107 destHeight = height; 108 } 109 110 /** 111 * Passes along the properties from the source object after adding a 112 * property indicating the scale applied. 113 * This method invokes {@code super.setProperties}, 114 * which might result in additional properties being added. 115 * <p> 116 * Note: This method is intended to be called by the 117 * {@code ImageProducer} of the {@code Image} whose pixels 118 * are being filtered. Developers using 119 * this class to filter pixels from an image should avoid calling 120 * this method directly since that operation could interfere 121 * with the filtering operation. 122 */ 123 public void setProperties(Hashtable<?,?> props) { 124 @SuppressWarnings("unchecked") 125 Hashtable<Object,Object> p = (Hashtable<Object,Object>)props.clone(); 126 String key = "rescale"; 127 String val = destWidth + "x" + destHeight; 128 Object o = p.get(key); 129 if (o != null && o instanceof String) { 130 val = ((String) o) + ", " + val; 131 } 132 p.put(key, val); 133 super.setProperties(p); 134 } 135 136 /** 137 * Override the dimensions of the source image and pass the dimensions 138 * of the new scaled size to the ImageConsumer. 139 * <p> 140 * Note: This method is intended to be called by the 141 * {@code ImageProducer} of the {@code Image} whose pixels 142 * are being filtered. Developers using 143 * this class to filter pixels from an image should avoid calling 144 * this method directly since that operation could interfere 145 * with the filtering operation. 146 * @see ImageConsumer 147 */ 148 public void setDimensions(int w, int h) { 149 srcWidth = w; 150 srcHeight = h; 151 if (destWidth < 0) { 152 if (destHeight < 0) { 153 destWidth = srcWidth; 154 destHeight = srcHeight; 155 } else { 156 destWidth = srcWidth * destHeight / srcHeight; 157 } 158 } else if (destHeight < 0) { 159 destHeight = srcHeight * destWidth / srcWidth; 160 } 161 consumer.setDimensions(destWidth, destHeight); 162 } 163 164 private void calculateMaps() { 165 srcrows = new int[destHeight + 1]; 166 for (int y = 0; y <= destHeight; y++) { 167 srcrows[y] = (2 * y * srcHeight + srcHeight) / (2 * destHeight); 168 } 169 srccols = new int[destWidth + 1]; 170 for (int x = 0; x <= destWidth; x++) { 171 srccols[x] = (2 * x * srcWidth + srcWidth) / (2 * destWidth); 172 } 173 } 174 175 /** 176 * Choose which rows and columns of the delivered byte pixels are 177 * needed for the destination scaled image and pass through just 178 * those rows and columns that are needed, replicated as necessary. 179 * <p> 180 * Note: This method is intended to be called by the 181 * {@code ImageProducer} of the {@code Image} whose pixels 182 * are being filtered. Developers using 183 * this class to filter pixels from an image should avoid calling 184 * this method directly since that operation could interfere 185 * with the filtering operation. 186 */ 187 public void setPixels(int x, int y, int w, int h, 188 ColorModel model, byte pixels[], int off, 189 int scansize) { 190 if (srcrows == null || srccols == null) { 191 calculateMaps(); 192 } 193 int sx, sy; 194 int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); 195 int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); 196 byte outpix[]; 197 if (outpixbuf != null && outpixbuf instanceof byte[]) { 198 outpix = (byte[]) outpixbuf; 199 } else { 200 outpix = new byte[destWidth]; 201 outpixbuf = outpix; 202 } 203 for (int dy = dy1; (sy = srcrows[dy]) < y + h; dy++) { 204 int srcoff = off + scansize * (sy - y); 205 int dx; 206 for (dx = dx1; (sx = srccols[dx]) < x + w; dx++) { 207 outpix[dx] = pixels[srcoff + sx - x]; 208 } 209 if (dx > dx1) { 210 consumer.setPixels(dx1, dy, dx - dx1, 1, 211 model, outpix, dx1, destWidth); 212 } 213 } 214 } 215 216 /** 217 * Choose which rows and columns of the delivered int pixels are 218 * needed for the destination scaled image and pass through just 219 * those rows and columns that are needed, replicated as necessary. 220 * <p> 221 * Note: This method is intended to be called by the 222 * {@code ImageProducer} of the {@code Image} whose pixels 223 * are being filtered. Developers using 224 * this class to filter pixels from an image should avoid calling 225 * this method directly since that operation could interfere 226 * with the filtering operation. 227 */ 228 public void setPixels(int x, int y, int w, int h, 229 ColorModel model, int pixels[], int off, 230 int scansize) { 231 if (srcrows == null || srccols == null) { 232 calculateMaps(); 233 } 234 int sx, sy; 235 int dx1 = (2 * x * destWidth + srcWidth - 1) / (2 * srcWidth); 236 int dy1 = (2 * y * destHeight + srcHeight - 1) / (2 * srcHeight); 237 int outpix[]; 238 if (outpixbuf != null && outpixbuf instanceof int[]) { 239 outpix = (int[]) outpixbuf; 240 } else { 241 outpix = new int[destWidth]; 242 outpixbuf = outpix; |