src/share/classes/java/awt/image/LookupOp.java

Print this page



   1 /*
   2  * Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 356      * @param destCM    the destination's <code>ColorModel</code>, which
 357      *                  can be <code>null</code>.
 358      * @return a filtered destination <code>BufferedImage</code>.
 359      */
 360     public BufferedImage createCompatibleDestImage (BufferedImage src,
 361                                                     ColorModel destCM) {
 362         BufferedImage image;
 363         int w = src.getWidth();
 364         int h = src.getHeight();
 365         int transferType = DataBuffer.TYPE_BYTE;
 366         if (destCM == null) {
 367             ColorModel cm = src.getColorModel();
 368             Raster raster = src.getRaster();
 369             if (cm instanceof ComponentColorModel) {
 370                 DataBuffer db = raster.getDataBuffer();
 371                 boolean hasAlpha = cm.hasAlpha();
 372                 boolean isPre    = cm.isAlphaPremultiplied();
 373                 int trans        = cm.getTransparency();
 374                 int[] nbits = null;
 375                 if (ltable instanceof ByteLookupTable) {
 376                     if (db.getDataType() == db.TYPE_USHORT) {
 377                         // Dst raster should be of type byte
 378                         if (hasAlpha) {
 379                             nbits = new int[2];
 380                             if (trans == cm.BITMASK) {
 381                                 nbits[1] = 1;
 382                             }
 383                             else {
 384                                 nbits[1] = 8;
 385                             }
 386                         }
 387                         else {
 388                             nbits = new int[1];
 389                         }
 390                         nbits[0] = 8;
 391                     }
 392                     // For byte, no need to change the cm
 393                 }
 394                 else if (ltable instanceof ShortLookupTable) {
 395                     transferType = DataBuffer.TYPE_USHORT;
 396                     if (db.getDataType() == db.TYPE_BYTE) {
 397                         if (hasAlpha) {
 398                             nbits = new int[2];
 399                             if (trans == cm.BITMASK) {
 400                                 nbits[1] = 1;
 401                             }
 402                             else {
 403                                 nbits[1] = 16;
 404                             }
 405                         }
 406                         else {
 407                             nbits = new int[1];
 408                         }
 409                         nbits[0] = 16;
 410                     }
 411                 }
 412                 if (nbits != null) {
 413                     cm = new ComponentColorModel(cm.getColorSpace(),
 414                                                  nbits, hasAlpha, isPre,
 415                                                  trans, transferType);
 416                 }
 417             }
 418             image = new BufferedImage(cm,
 419                                       cm.createCompatibleWritableRaster(w, h),


   1 
   2 /*
   3  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.  Oracle designates this
   9  * particular file as subject to the "Classpath" exception as provided
  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any


 357      * @param destCM    the destination's <code>ColorModel</code>, which
 358      *                  can be <code>null</code>.
 359      * @return a filtered destination <code>BufferedImage</code>.
 360      */
 361     public BufferedImage createCompatibleDestImage (BufferedImage src,
 362                                                     ColorModel destCM) {
 363         BufferedImage image;
 364         int w = src.getWidth();
 365         int h = src.getHeight();
 366         int transferType = DataBuffer.TYPE_BYTE;
 367         if (destCM == null) {
 368             ColorModel cm = src.getColorModel();
 369             Raster raster = src.getRaster();
 370             if (cm instanceof ComponentColorModel) {
 371                 DataBuffer db = raster.getDataBuffer();
 372                 boolean hasAlpha = cm.hasAlpha();
 373                 boolean isPre    = cm.isAlphaPremultiplied();
 374                 int trans        = cm.getTransparency();
 375                 int[] nbits = null;
 376                 if (ltable instanceof ByteLookupTable) {
 377                     if (db.getDataType() == DataBuffer.TYPE_USHORT) {
 378                         // Dst raster should be of type byte
 379                         if (hasAlpha) {
 380                             nbits = new int[2];
 381                             if (trans == java.awt.Transparency.BITMASK) {
 382                                 nbits[1] = 1;
 383                             }
 384                             else {
 385                                 nbits[1] = 8;
 386                             }
 387                         }
 388                         else {
 389                             nbits = new int[1];
 390                         }
 391                         nbits[0] = 8;
 392                     }
 393                     // For byte, no need to change the cm
 394                 }
 395                 else if (ltable instanceof ShortLookupTable) {
 396                     transferType = DataBuffer.TYPE_USHORT;
 397                     if (db.getDataType() == DataBuffer.TYPE_BYTE) {
 398                         if (hasAlpha) {
 399                             nbits = new int[2];
 400                             if (trans == java.awt.Transparency.BITMASK) {
 401                                 nbits[1] = 1;
 402                             }
 403                             else {
 404                                 nbits[1] = 16;
 405                             }
 406                         }
 407                         else {
 408                             nbits = new int[1];
 409                         }
 410                         nbits[0] = 16;
 411                     }
 412                 }
 413                 if (nbits != null) {
 414                     cm = new ComponentColorModel(cm.getColorSpace(),
 415                                                  nbits, hasAlpha, isPre,
 416                                                  trans, transferType);
 417                 }
 418             }
 419             image = new BufferedImage(cm,
 420                                       cm.createCompatibleWritableRaster(w, h),