001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Oleg V. Khaschansky
019: * @version $Revision$
020: *
021: * @date: Sep 20, 2005
022: */package java.awt.image;
023:
024: import java.awt.*;
025: import java.awt.geom.Point2D;
026: import java.awt.geom.Rectangle2D;
027: import java.util.Arrays;
028:
029: import org.apache.harmony.awt.gl.AwtImageBackdoorAccessor;
030: import org.apache.harmony.awt.internal.nls.Messages;
031:
032: public class BandCombineOp implements RasterOp {
033: static final int offsets3c[] = { 16, 8, 0 };
034: static final int offsets4ac[] = { 16, 8, 0, 24 };
035: static final int masks3c[] = { 0xFF0000, 0xFF00, 0xFF };
036: static final int masks4ac[] = { 0xFF0000, 0xFF00, 0xFF, 0xFF000000 };
037: private static final int piOffsets[] = { 0, 1, 2 };
038: private static final int piInvOffsets[] = { 2, 1, 0 };
039:
040: private static final int TYPE_BYTE3C = 0;
041: private static final int TYPE_BYTE4AC = 1;
042: private static final int TYPE_USHORT3C = 2;
043: private static final int TYPE_SHORT3C = 3;
044:
045: private int mxWidth;
046: private int mxHeight;
047: private float matrix[][];
048: private RenderingHints rHints;
049:
050: static {
051: // XXX - todo
052: //System.loadLibrary("imageops");
053: }
054:
055: public BandCombineOp(float matrix[][], RenderingHints hints) {
056: this .mxHeight = matrix.length;
057: this .mxWidth = matrix[0].length;
058: this .matrix = new float[mxHeight][mxWidth];
059:
060: for (int i = 0; i < mxHeight; i++) {
061: System.arraycopy(matrix[i], 0, this .matrix[i], 0, mxWidth);
062: }
063:
064: this .rHints = hints;
065: }
066:
067: public final RenderingHints getRenderingHints() {
068: return this .rHints;
069: }
070:
071: public final float[][] getMatrix() {
072: float res[][] = new float[mxHeight][mxWidth];
073:
074: for (int i = 0; i < mxHeight; i++) {
075: System.arraycopy(matrix[i], 0, res[i], 0, mxWidth);
076: }
077:
078: return res;
079: }
080:
081: public final Point2D getPoint2D(Point2D srcPoint, Point2D dstPoint) {
082: if (dstPoint == null) {
083: dstPoint = new Point2D.Float();
084: }
085:
086: dstPoint.setLocation(srcPoint);
087: return dstPoint;
088: }
089:
090: public final Rectangle2D getBounds2D(Raster src) {
091: return src.getBounds();
092: }
093:
094: public WritableRaster createCompatibleDestRaster(Raster src) {
095: int numBands = src.getNumBands();
096: if (mxWidth != numBands && mxWidth != (numBands + 1)
097: || numBands != mxHeight) {
098: // awt.254=Number of bands in the source raster ({0}) is
099: // incompatible with the matrix [{1}x{2}]
100: throw new IllegalArgumentException(Messages.getString(
101: "awt.254", //$NON-NLS-1$
102: new Object[] { numBands, mxWidth, mxHeight }));
103: }
104:
105: return src.createCompatibleWritableRaster(src.getWidth(), src
106: .getHeight());
107: }
108:
109: public WritableRaster filter(Raster src, WritableRaster dst) {
110: int numBands = src.getNumBands();
111:
112: if (mxWidth != numBands && mxWidth != (numBands + 1)) {
113: // awt.254=Number of bands in the source raster ({0}) is
114: // incompatible with the matrix [{1}x{2}]
115: throw new IllegalArgumentException(Messages.getString(
116: "awt.254", //$NON-NLS-1$
117: new Object[] { numBands, mxWidth, mxHeight }));
118: }
119:
120: if (dst == null) {
121: dst = createCompatibleDestRaster(src);
122: } else if (dst.getNumBands() != mxHeight) {
123: // awt.255=Number of bands in the destination raster ({0}) is incompatible with the matrix [{1}x{2}]
124: throw new IllegalArgumentException(Messages
125: .getString("awt.255", //$NON-NLS-1$
126: new Object[] { dst.getNumBands(), mxWidth,
127: mxHeight }));
128: }
129:
130: // XXX - todo
131: //if (ippFilter(src, dst) != 0)
132: if (verySlowFilter(src, dst) != 0) {
133: // awt.21F=Unable to transform source
134: throw new ImagingOpException(Messages.getString("awt.21F")); //$NON-NLS-1$
135: }
136:
137: return dst;
138: }
139:
140: private static final class SampleModelInfo {
141: int channels;
142: int channelsOrder[];
143: int stride;
144: }
145:
146: private final SampleModelInfo checkSampleModel(SampleModel sm) {
147: SampleModelInfo ret = new SampleModelInfo();
148:
149: if (sm instanceof PixelInterleavedSampleModel) {
150: // Check PixelInterleavedSampleModel
151: if (sm.getDataType() != DataBuffer.TYPE_BYTE) {
152: return null;
153: }
154:
155: ret.channels = sm.getNumBands();
156: ret.stride = ((ComponentSampleModel) sm)
157: .getScanlineStride();
158: ret.channelsOrder = ((ComponentSampleModel) sm)
159: .getBandOffsets();
160:
161: } else if (sm instanceof SinglePixelPackedSampleModel) {
162: // Check SinglePixelPackedSampleModel
163: SinglePixelPackedSampleModel sppsm1 = (SinglePixelPackedSampleModel) sm;
164:
165: ret.channels = sppsm1.getNumBands();
166: if (sppsm1.getDataType() != DataBuffer.TYPE_INT) {
167: return null;
168: }
169:
170: // Check sample models
171: for (int i = 0; i < ret.channels; i++) {
172: if (sppsm1.getSampleSize(i) != 8) {
173: return null;
174: }
175: }
176:
177: ret.channelsOrder = new int[ret.channels];
178: int bitOffsets[] = sppsm1.getBitOffsets();
179: for (int i = 0; i < ret.channels; i++) {
180: if (bitOffsets[i] % 8 != 0) {
181: return null;
182: }
183:
184: ret.channelsOrder[i] = bitOffsets[i] / 8;
185: }
186:
187: ret.channels = 4;
188: ret.stride = sppsm1.getScanlineStride() * 4;
189: } else {
190: return null;
191: }
192:
193: return ret;
194: }
195:
196: private final int slowFilter(Raster src, WritableRaster dst) {
197: int res = 0;
198:
199: SampleModelInfo srcInfo, dstInfo;
200: int offsets[] = null;
201:
202: srcInfo = checkSampleModel(src.getSampleModel());
203: dstInfo = checkSampleModel(dst.getSampleModel());
204: if (srcInfo == null || dstInfo == null) {
205: return verySlowFilter(src, dst);
206: }
207:
208: // Fill offsets if there's a child raster
209: if (src.getParent() != null || dst.getParent() != null) {
210: if (src.getSampleModelTranslateX() != 0
211: || src.getSampleModelTranslateY() != 0
212: || dst.getSampleModelTranslateX() != 0
213: || dst.getSampleModelTranslateY() != 0) {
214: offsets = new int[4];
215: offsets[0] = -src.getSampleModelTranslateX()
216: + src.getMinX();
217: offsets[1] = -src.getSampleModelTranslateY()
218: + src.getMinY();
219: offsets[2] = -dst.getSampleModelTranslateX()
220: + dst.getMinX();
221: offsets[3] = -dst.getSampleModelTranslateY()
222: + dst.getMinY();
223: }
224: }
225:
226: int rmxWidth = (srcInfo.channels + 1); // width of the reordered matrix
227: float reorderedMatrix[] = new float[rmxWidth * dstInfo.channels];
228: for (int j = 0; j < dstInfo.channels; j++) {
229: if (j >= dstInfo.channelsOrder.length) {
230: continue;
231: }
232:
233: for (int i = 0; i < srcInfo.channels; i++) {
234: if (i >= srcInfo.channelsOrder.length) {
235: break;
236: }
237:
238: reorderedMatrix[dstInfo.channelsOrder[j] * rmxWidth
239: + srcInfo.channelsOrder[i]] = matrix[j][i];
240: }
241: if (mxWidth == rmxWidth) {
242: reorderedMatrix[(dstInfo.channelsOrder[j] + 1)
243: * rmxWidth - 1] = matrix[j][mxWidth - 1];
244: }
245: }
246:
247: Object srcData, dstData;
248: AwtImageBackdoorAccessor dbAccess = AwtImageBackdoorAccessor
249: .getInstance();
250: try {
251: srcData = dbAccess.getData(src.getDataBuffer());
252: dstData = dbAccess.getData(dst.getDataBuffer());
253: } catch (IllegalArgumentException e) {
254: return -1; // Unknown data buffer type
255: }
256:
257: simpleCombineBands(srcData, src.getWidth(), src.getHeight(),
258: srcInfo.stride, srcInfo.channels, dstData,
259: dstInfo.stride, dstInfo.channels, reorderedMatrix,
260: offsets);
261:
262: return res;
263: }
264:
265: private int verySlowFilter(Raster src, WritableRaster dst) {
266: int numBands = src.getNumBands();
267:
268: int srcMinX = src.getMinX();
269: int srcY = src.getMinY();
270:
271: int dstMinX = dst.getMinX();
272: int dstY = dst.getMinY();
273:
274: int dX = src.getWidth();//< dst.getWidth() ? src.getWidth() : dst.getWidth();
275: int dY = src.getHeight();//< dst.getHeight() ? src.getHeight() : dst.getHeight();
276:
277: float sample;
278: int srcPixels[] = new int[numBands * dX * dY];
279: int dstPixels[] = new int[mxHeight * dX * dY];
280:
281: srcPixels = src.getPixels(srcMinX, srcY, dX, dY, srcPixels);
282:
283: if (numBands == mxWidth) {
284: for (int i = 0, j = 0; i < srcPixels.length; i += numBands) {
285: for (int dstB = 0; dstB < mxHeight; dstB++) {
286: sample = 0f;
287: for (int srcB = 0; srcB < numBands; srcB++) {
288: sample += matrix[dstB][srcB]
289: * srcPixels[i + srcB];
290: }
291: dstPixels[j++] = (int) sample;
292: }
293: }
294: } else {
295: for (int i = 0, j = 0; i < srcPixels.length; i += numBands) {
296: for (int dstB = 0; dstB < mxHeight; dstB++) {
297: sample = 0f;
298: for (int srcB = 0; srcB < numBands; srcB++) {
299: sample += matrix[dstB][srcB]
300: * srcPixels[i + srcB];
301: }
302: dstPixels[j++] = (int) (sample + matrix[dstB][numBands]);
303: }
304: }
305: }
306:
307: dst.setPixels(dstMinX, dstY, dX, dY, dstPixels);
308:
309: return 0;
310: }
311:
312: //TODO remove when method is used
313: @SuppressWarnings("unused")
314: private int ippFilter(Raster src, WritableRaster dst) {
315: boolean invertChannels;
316: boolean inPlace = (src == dst);
317: int type;
318: int srcStride, dstStride;
319: int offsets[] = null;
320:
321: int srcBands = src.getNumBands();
322: int dstBands = dst.getNumBands();
323:
324: if (dstBands != 3
325: || (srcBands != 3 && !(srcBands == 4
326: && matrix[0][3] == 0 && matrix[1][3] == 0 && matrix[2][3] == 0))) {
327: return slowFilter(src, dst);
328: }
329:
330: SampleModel srcSM = src.getSampleModel();
331: SampleModel dstSM = dst.getSampleModel();
332:
333: if (srcSM instanceof SinglePixelPackedSampleModel
334: && dstSM instanceof SinglePixelPackedSampleModel) {
335: // Check SinglePixelPackedSampleModel
336: SinglePixelPackedSampleModel sppsm1 = (SinglePixelPackedSampleModel) srcSM;
337: SinglePixelPackedSampleModel sppsm2 = (SinglePixelPackedSampleModel) dstSM;
338:
339: if (sppsm1.getDataType() != DataBuffer.TYPE_INT
340: || sppsm2.getDataType() != DataBuffer.TYPE_INT) {
341: return slowFilter(src, dst);
342: }
343:
344: // Check sample models
345: if (!Arrays.equals(sppsm2.getBitOffsets(), offsets3c)
346: || !Arrays.equals(sppsm2.getBitMasks(), masks3c)) {
347: return slowFilter(src, dst);
348: }
349:
350: if (srcBands == 3) {
351: if (!Arrays.equals(sppsm1.getBitOffsets(), offsets3c)
352: || !Arrays
353: .equals(sppsm1.getBitMasks(), masks3c)) {
354: return slowFilter(src, dst);
355: }
356: } else if (srcBands == 4) {
357: if (!Arrays.equals(sppsm1.getBitOffsets(), offsets4ac)
358: || !Arrays.equals(sppsm1.getBitMasks(),
359: masks4ac)) {
360: return slowFilter(src, dst);
361: }
362: }
363:
364: type = TYPE_BYTE4AC;
365: invertChannels = true;
366:
367: srcStride = sppsm1.getScanlineStride() * 4;
368: dstStride = sppsm2.getScanlineStride() * 4;
369: } else if (srcSM instanceof PixelInterleavedSampleModel
370: && dstSM instanceof PixelInterleavedSampleModel) {
371: if (srcBands != 3) {
372: return slowFilter(src, dst);
373: }
374:
375: int srcDataType = srcSM.getDataType();
376:
377: switch (srcDataType) {
378: case DataBuffer.TYPE_BYTE:
379: type = TYPE_BYTE3C;
380: break;
381: case DataBuffer.TYPE_USHORT:
382: type = TYPE_USHORT3C;
383: break;
384: case DataBuffer.TYPE_SHORT:
385: type = TYPE_SHORT3C;
386: break;
387: default:
388: return slowFilter(src, dst);
389: }
390:
391: // Check PixelInterleavedSampleModel
392: PixelInterleavedSampleModel pism1 = (PixelInterleavedSampleModel) srcSM;
393: PixelInterleavedSampleModel pism2 = (PixelInterleavedSampleModel) dstSM;
394:
395: if (srcDataType != pism2.getDataType()
396: || pism1.getPixelStride() != 3
397: || pism2.getPixelStride() != 3
398: || !Arrays.equals(pism1.getBandOffsets(), pism2
399: .getBandOffsets())) {
400: return slowFilter(src, dst);
401: }
402:
403: if (Arrays.equals(pism1.getBandOffsets(), piInvOffsets)) {
404: invertChannels = true;
405: } else if (Arrays.equals(pism1.getBandOffsets(), piOffsets)) {
406: invertChannels = false;
407: } else {
408: return slowFilter(src, dst);
409: }
410:
411: int dataTypeSize = DataBuffer.getDataTypeSize(srcDataType) / 8;
412:
413: srcStride = pism1.getScanlineStride() * dataTypeSize;
414: dstStride = pism2.getScanlineStride() * dataTypeSize;
415: } else { // XXX - todo - IPP allows support for planar data also
416: return slowFilter(src, dst);
417: }
418:
419: // Fill offsets if there's a child raster
420: if (src.getParent() != null || dst.getParent() != null) {
421: if (src.getSampleModelTranslateX() != 0
422: || src.getSampleModelTranslateY() != 0
423: || dst.getSampleModelTranslateX() != 0
424: || dst.getSampleModelTranslateY() != 0) {
425: offsets = new int[4];
426: offsets[0] = -src.getSampleModelTranslateX()
427: + src.getMinX();
428: offsets[1] = -src.getSampleModelTranslateY()
429: + src.getMinY();
430: offsets[2] = -dst.getSampleModelTranslateX()
431: + dst.getMinX();
432: offsets[3] = -dst.getSampleModelTranslateY()
433: + dst.getMinY();
434: }
435: }
436:
437: Object srcData, dstData;
438: AwtImageBackdoorAccessor dbAccess = AwtImageBackdoorAccessor
439: .getInstance();
440: try {
441: srcData = dbAccess.getData(src.getDataBuffer());
442: dstData = dbAccess.getData(dst.getDataBuffer());
443: } catch (IllegalArgumentException e) {
444: return -1; // Unknown data buffer type
445: }
446:
447: float ippMatrix[] = new float[12];
448:
449: if (invertChannels) {
450: // IPP treats big endian integers like BGR, so we have to
451: // swap columns 1 and 3 and rows 1 and 3
452: for (int i = 0; i < mxHeight; i++) {
453: ippMatrix[i * 4] = matrix[2 - i][2];
454: ippMatrix[i * 4 + 1] = matrix[2 - i][1];
455: ippMatrix[i * 4 + 2] = matrix[2 - i][0];
456:
457: if (mxWidth == 4) {
458: ippMatrix[i * 4 + 3] = matrix[2 - i][3];
459: } else if (mxWidth == 5) {
460: ippMatrix[i * 4 + 3] = matrix[2 - i][4];
461: }
462: }
463: } else {
464: for (int i = 0; i < mxHeight; i++) {
465: ippMatrix[i * 4] = matrix[i][0];
466: ippMatrix[i * 4 + 1] = matrix[i][1];
467: ippMatrix[i * 4 + 2] = matrix[i][2];
468:
469: if (mxWidth == 4) {
470: ippMatrix[i * 4 + 3] = matrix[i][3];
471: } else if (mxWidth == 5) {
472: ippMatrix[i * 4 + 3] = matrix[i][4];
473: }
474: }
475: }
476:
477: return ippColorTwist(srcData, src.getWidth(), src.getHeight(),
478: srcStride, dstData, dst.getWidth(), dst.getHeight(),
479: dstStride, ippMatrix, type, offsets, inPlace);
480: }
481:
482: private final native int ippColorTwist(Object srcData,
483: int srcWidth, int srcHeight, int srcStride, Object dstData,
484: int dstWidth, int dstHeight, int dstStride,
485: float ippMatrix[], int type, int offsets[], boolean inPlace);
486:
487: private final native int simpleCombineBands(Object srcData,
488: int srcWidth, int srcHeight, int srcStride,
489: int srcChannels, Object dstData, int dstStride,
490: int dstChannels, float m[], int offsets[]);
491: }
|