summaryrefslogtreecommitdiff
path: root/java/awt/image/ColorConvertOp.java
blob: e6c85412d34d043e4193cf9b765ae745d91db9c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* ColorConvertOp.java --
   Copyright (C) 2004, 2006  Free Software Foundation

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt.image;

import gnu.java.awt.Buffers;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * ColorConvertOp is a filter for converting images or rasters between
 * colorspaces, either through a sequence of colorspaces or just from source to 
 * destination.
 * 
 * Color conversion is done on the color components without alpha.  Thus
 * if a BufferedImage has alpha premultiplied, this is divided out before
 * color conversion, and premultiplication applied if the destination
 * requires it.
 * 
 * Color rendering and dithering hints may be applied if specified.  This is
 * likely platform-dependent.
 * 
 * @author jlquinn@optonline.net
 */
public class ColorConvertOp implements BufferedImageOp, RasterOp
{
  private RenderingHints hints;
  private ICC_Profile[] profiles = null;
  private ColorSpace[] spaces;
  

  /**
   * Convert a BufferedImage through a ColorSpace.
   * 
   * Objects created with this constructor can be used to convert
   * BufferedImage's to a destination ColorSpace.  Attempts to convert Rasters
   * with this constructor will result in an IllegalArgumentException when the
   * filter(Raster, WritableRaster) method is called.  
   * 
   * @param cspace The target color space.
   * @param hints Rendering hints to use in conversion, if any (may be null)
   * @throws NullPointerException if the ColorSpace is null.
   */
  public ColorConvertOp(ColorSpace cspace, RenderingHints hints)
  {
    if (cspace == null)
      throw new NullPointerException();
    spaces = new ColorSpace[]{cspace};
    this.hints = hints;
  }
  
  /**
   * Convert from a source colorspace to a destination colorspace.
   * 
   * This constructor takes two ColorSpace arguments as the source and
   * destination color spaces.  It is usually used with the
   * filter(Raster, WritableRaster) method, in which case the source colorspace 
   * is assumed to correspond to the source Raster, and the destination 
   * colorspace with the destination Raster.
   * 
   * If used with BufferedImages that do not match the source or destination 
   * colorspaces specified here, there is an implicit conversion from the 
   * source image to the source ColorSpace, or the destination ColorSpace to 
   * the destination image.
   * 
   * @param srcCspace The source ColorSpace.
   * @param dstCspace The destination ColorSpace.
   * @param hints Rendering hints to use in conversion, if any (may be null).
   * @throws NullPointerException if any ColorSpace is null.
   */
  public ColorConvertOp(ColorSpace srcCspace, ColorSpace dstCspace,
			RenderingHints hints)
  {
    if (srcCspace == null || dstCspace == null)
      throw new NullPointerException();
    spaces = new ColorSpace[]{srcCspace, dstCspace};
    this.hints = hints;     
  }

  /**
   * Convert from a source colorspace to a destinatino colorspace.
   * 
   * This constructor builds a ColorConvertOp from an array of ICC_Profiles.
   * The source will be converted through the sequence of color spaces
   * defined by the profiles.  If the sequence of profiles doesn't give a
   * well-defined conversion, an IllegalArgumentException is thrown.
   * 
   * If used with BufferedImages that do not match the source or destination 
   * colorspaces specified here, there is an implicit conversion from the 
   * source image to the source ColorSpace, or the destination ColorSpace to 
   * the destination image.
   * 
   * For Rasters, the first and last profiles must have the same number of
   * bands as the source and destination Rasters, respectively.  If this is
   * not the case, or there fewer than 2 profiles, an IllegalArgumentException
   * will be thrown. 
   * 
   * @param profiles An array of ICC_Profile's to convert through.
   * @param hints Rendering hints to use in conversion, if any (may be null).
   * @throws NullPointerException if the profile array is null.
   * @throws IllegalArgumentException if the array is not a well-defined
   *            conversion.
   */
  public ColorConvertOp(ICC_Profile[] profiles, RenderingHints hints)
  {
    if (profiles == null)
      throw new NullPointerException();
    
    this.hints = hints; 
    this.profiles = profiles;
    
    // Create colorspace array with space for src and dest colorspace
    // Note that the ICC_ColorSpace constructor will throw an
    // IllegalArgumentException if the profile is invalid; thus we check
    // for a "well defined conversion"
    spaces = new ColorSpace[profiles.length];
    for (int i = 0; i < profiles.length; i++)
      spaces[i] = new ICC_ColorSpace(profiles[i]);
  }
  
  /**
   * Convert from source color space to destination color space.
   * 
   * Only valid for BufferedImage objects, this Op converts from the source
   * image's color space to the destination image's color space.
   * 
   * The destination in the filter(BufferedImage, BufferedImage) method cannot 
   * be null for this operation, and it also cannot be used with the
   * filter(Raster, WritableRaster) method.
   * 
   * @param hints Rendering hints to use in conversion, if any (may be null).
   */
  public ColorConvertOp(RenderingHints hints)
  {
    this.hints = hints;
    spaces = new ColorSpace[0];
  }
  
  /**
   * Converts the source image using the conversion path specified in the
   * constructor.  The resulting image is stored in the destination image if one
   * is provided; otherwise a new BufferedImage is created and returned. 
   * 
   * The source and destination BufferedImage (if one is supplied) must have
   * the same dimensions.
   *
   * @param src The source image.
   * @param dst The destination image.
   * @throws IllegalArgumentException if the rasters and/or color spaces are
   *            incompatible.
   * @return The transformed image.
   */
  public final BufferedImage filter(BufferedImage src, BufferedImage dst)
  {
    // TODO: The plan is to create a scanline buffer for intermediate buffers.
    // For now we just suck it up and create intermediate buffers.
    
    if (dst == null && spaces.length == 0)
      throw new IllegalArgumentException("Not enough color space information "
                                         + "to complete conversion.");

    if (dst != null
        && (src.getHeight() != dst.getHeight() || src.getWidth() != dst.getWidth()))
      throw new IllegalArgumentException("Source and destination images have "
                                         + "different dimensions");

    // Make sure input isn't premultiplied by alpha
    if (src.isAlphaPremultiplied())
      {
        BufferedImage tmp = createCompatibleDestImage(src, src.getColorModel());
        copyimage(src, tmp);
        tmp.coerceData(false);
        src = tmp;
      }

    // Convert through defined intermediate conversions
    BufferedImage tmp;
    for (int i = 0; i < spaces.length; i++)
      {
        if (src.getColorModel().getColorSpace().getType() != spaces[i].getType())
          {
            tmp = createCompatibleDestImage(src,
                                            createCompatibleColorModel(src,
                                                                       spaces[i]));
            copyimage(src, tmp);
            src = tmp;
          }
      }

    // No implicit conversion to destination type needed; return result from the
    // last intermediate conversions (which was left in src)
    if (dst == null)
      dst = src;

    // Implicit conversion to destination image's color space
    else
      copyimage(src, dst);

    return dst;
  }

  /**
   * Converts the source raster using the conversion path specified in the
   * constructor.  The resulting raster is stored in the destination raster if
   * one is provided; otherwise a new WritableRaster is created and returned.
   * 
   * This operation is not valid with every constructor of this class; see
   * the constructors for details.  Further, the source raster must have the
   * same number of bands as the source ColorSpace, and the destination raster
   * must have the same number of bands as the destination ColorSpace.
   * 
   * The source and destination raster (if one is supplied) must also have the
   * same dimensions.
   *
   * @param src The source raster.
   * @param dest The destination raster.
   * @throws IllegalArgumentException if the rasters and/or color spaces are
   *            incompatible.
   * @return The transformed raster.
   */
  public final WritableRaster filter(Raster src, WritableRaster dest)
  {
    // Various checks to ensure that the rasters and color spaces are compatible
    if (spaces.length < 2)
      throw new IllegalArgumentException("Not enough information about " +
            "source and destination colorspaces.");

    if (spaces[0].getNumComponents() != src.getNumBands()
        || (dest != null && spaces[spaces.length - 1].getNumComponents() != dest.getNumBands()))
      throw new IllegalArgumentException("Source or destination raster " +
            "contains the wrong number of bands.");

    if (dest != null
        && (src.getHeight() != dest.getHeight() || src.getWidth() != dest.getWidth()))
      throw new IllegalArgumentException("Source and destination rasters " +
            "have different dimensions");

    // Need to iterate through each color space.
    // spaces[0] corresponds to the ColorSpace of the source raster, and
    // spaces[spaces.length - 1] corresponds to the ColorSpace of the
    // destination, with any number (or zero) of intermediate conversions.

    for (int i = 0; i < spaces.length - 2; i++)
      {
        WritableRaster tmp = createCompatibleDestRaster(src, spaces[i + 1],
                                                        false,
                                                        src.getTransferType());
        copyraster(src, spaces[i], tmp, spaces[i + 1]);
        src = tmp;
      }

    // The last conversion is done outside of the loop so that we can
    // use the dest raster supplied, instead of creating our own temp raster
    if (dest == null)
      dest = createCompatibleDestRaster(src, spaces[spaces.length - 1], false,
                                        DataBuffer.TYPE_BYTE);
    copyraster(src, spaces[spaces.length - 2], dest, spaces[spaces.length - 1]);

    return dest;
  }

  /**
   * Creates an empty BufferedImage with the size equal to the source and the
   * correct number of bands for the conversion defined in this Op. The newly 
   * created image is created with the specified ColorModel, or if no ColorModel
   * is supplied, an appropriate one is chosen.
   *
   * @param src The source image.
   * @param dstCM A color model for the destination image (may be null).
   * @throws IllegalArgumentException if an appropriate colormodel cannot be
   *            chosen with the information given.
   * @return The new compatible destination image.
   */
  public BufferedImage createCompatibleDestImage(BufferedImage src,
                         ColorModel dstCM)
  {
    if (dstCM == null && spaces.length == 0)
      throw new IllegalArgumentException("Don't know the destination " +
            "colormodel");

    if (dstCM == null)
      {
        dstCM = createCompatibleColorModel(src, spaces[spaces.length - 1]);
      }

    return new BufferedImage(dstCM,
                             createCompatibleDestRaster(src.getRaster(),
                                                        dstCM.getColorSpace(),
                                                        src.getColorModel().hasAlpha,
                                                        dstCM.getTransferType()),
                             src.isPremultiplied, null);
  }
  
  /**
   * Creates a new WritableRaster with the size equal to the source and the
   * correct number of bands.
   * 
   * Note, the new Raster will always use a BYTE storage size, regardless of
   * the color model or defined destination; this is for compatibility with
   * the reference implementation.
   *
   * @param src The source Raster.
   * @throws IllegalArgumentException if there isn't enough colorspace
   *            information to create a compatible Raster.
   * @return The new compatible destination raster.
   */
  public WritableRaster createCompatibleDestRaster(Raster src)
  {
    if (spaces.length < 2)
      throw new IllegalArgumentException("Not enough destination colorspace " +
            "information");

    // Create a new raster with the last ColorSpace in the conversion
    // chain, and with no alpha (implied)
    return createCompatibleDestRaster(src, spaces[spaces.length-1], false,
                                      DataBuffer.TYPE_BYTE);
  }

  /**
   * Returns the array of ICC_Profiles used to create this Op, or null if the
   * Op was created using ColorSpace arguments.
   * 
   * @return The array of ICC_Profiles, or null.
   */
  public final ICC_Profile[] getICC_Profiles()
  {
    return profiles;
  }

  /**
   * Returns the rendering hints for this op.
   * 
   * @return The rendering hints for this Op, or null.
   */
  public final RenderingHints getRenderingHints()
  {
    return hints;
  }

  /**
   * Returns the corresponding destination point for a source point.
   * Because this is not a geometric operation, the destination and source
   * points will be identical.
   * 
   * @param src The source point.
   * @param dst The transformed destination point.
   * @return The transformed destination point.
   */
  public final Point2D getPoint2D(Point2D src, Point2D dst)
  {
    if (dst == null)
      return (Point2D)src.clone();
    
    dst.setLocation(src);
    return dst;
  }

  /**
   * Returns the corresponding destination boundary of a source boundary.
   * Because this is not a geometric operation, the destination and source
   * boundaries will be identical.
   * 
   * @param src The source boundary.
   * @return The boundaries of the destination.
   */
  public final Rectangle2D getBounds2D(BufferedImage src)
  {
    return src.getRaster().getBounds();
  }

  /**
   * Returns the corresponding destination boundary of a source boundary.
   * Because this is not a geometric operation, the destination and source
   * boundaries will be identical.
   * 
   * @param src The source boundary.
   * @return The boundaries of the destination.
   */
  public final Rectangle2D getBounds2D(Raster src)
  {
    return src.getBounds();
  }

  /**
   * Copy a source image to a destination image, respecting their colorspaces 
   * and performing colorspace conversions if necessary.  
   * 
   * @param src The source image.
   * @param dst The destination image.
   */
  private void copyimage(BufferedImage src, BufferedImage dst)
  {
    // This is done using Graphics2D in order to respect the rendering hints.
    Graphics2D gg = dst.createGraphics();
    
    // If no hints are set there is no need to call
    // setRenderingHints on the Graphics2D object.
    if (hints != null)
      gg.setRenderingHints(hints);
    
    gg.drawImage(src, 0, 0, null);
    gg.dispose();
  }
  
  /**
   * Copy a source raster to a destination raster, performing a colorspace
   * conversion between the two.  The conversion will respect the
   * KEY_COLOR_RENDERING rendering hint if one is present.
   * 
   * @param src The source raster.
   * @param scs The colorspace of the source raster.
   * @dst The destination raster.
   * @dcs The colorspace of the destination raster.
   */
  private void copyraster(Raster src, ColorSpace scs, WritableRaster dst, ColorSpace dcs)
  {
    float[] sbuf = new float[src.getNumBands()];
    
    if (hints != null
        && hints.get(RenderingHints.KEY_COLOR_RENDERING) ==
                 RenderingHints.VALUE_COLOR_RENDER_QUALITY)
    {
      // use cie for accuracy
      for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
        for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
          dst.setPixel(x, y,
		       dcs.fromCIEXYZ(scs.toCIEXYZ(src.getPixel(x, y, sbuf))));
    }
    else
    {
      // use rgb - it's probably faster
      for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
        for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
          dst.setPixel(x, y,
		       dcs.fromRGB(scs.toRGB(src.getPixel(x, y, sbuf))));
    }
  }

  /**
   * This method creates a color model with the same colorspace and alpha
   * settings as the source image.  The created color model will always be a
   * ComponentColorModel and have a BYTE transfer type.
   * 
   * @param img The source image.
   * @param cs The ColorSpace to use.
   * @return A color model compatible with the source image.
   */ 
  private ColorModel createCompatibleColorModel(BufferedImage img, ColorSpace cs)
  {
    // The choice of ComponentColorModel and DataBuffer.TYPE_BYTE is based on
    // Mauve testing of the reference implementation.
    return new ComponentColorModel(cs,
                                   img.getColorModel().hasAlpha(), 
                                   img.isAlphaPremultiplied(),
                                   img.getColorModel().getTransparency(),
                                   DataBuffer.TYPE_BYTE);    
  }

  /**
   * This method creates a compatible Raster, given a source raster, colorspace,
   * alpha value, and transfer type.
   * 
   * @param src The source raster.
   * @param cs The ColorSpace to use.
   * @param hasAlpha Whether the raster should include a component for an alpha.
   * @param transferType The size of a single data element.
   * @return A compatible WritableRaster. 
   */
  private WritableRaster createCompatibleDestRaster(Raster src, ColorSpace cs,
                                                    boolean hasAlpha,
                                                    int transferType)
  {
    // The use of a PixelInterleavedSampleModel weas determined using mauve
    // tests, based on the reference implementation
    
    int numComponents = cs.getNumComponents();
    if (hasAlpha)
      numComponents++;
    
    int[] offsets = new int[numComponents];
    for (int i = 0; i < offsets.length; i++)
      offsets[i] = i;

    DataBuffer db = Buffers.createBuffer(transferType,
                                         src.getWidth() * src.getHeight() * numComponents,
                                         1);
    return new WritableRaster(new PixelInterleavedSampleModel(transferType,
                                                              src.getWidth(),
                                                              src.getHeight(),
                                                              numComponents,
                                                              numComponents * src.getWidth(),
                                                              offsets),
                              db, new Point(src.getMinX(), src.getMinY()));
  }
}