Source Code Cross Referenced for ComponentColorModel.java in  » Apache-Harmony-Java-SE » java-package » java » awt » image » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.awt.image 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
0003:         *  contributor license agreements.  See the NOTICE file distributed with
0004:         *  this work for additional information regarding copyright ownership.
0005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
0006:         *  (the "License"); you may not use this file except in compliance with
0007:         *  the License.  You may obtain a copy of the License at
0008:         *
0009:         *     http://www.apache.org/licenses/LICENSE-2.0
0010:         *
0011:         *  Unless required by applicable law or agreed to in writing, software
0012:         *  distributed under the License is distributed on an "AS IS" BASIS,
0013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         *  See the License for the specific language governing permissions and
0015:         *  limitations under the License.
0016:         */
0017:        /**
0018:         * @author Igor V. Stolyarov
0019:         * @version $Revision$
0020:         */package java.awt.image;
0021:
0022:        import java.awt.color.ColorSpace;
0023:
0024:        import org.apache.harmony.awt.gl.color.LUTColorConverter;
0025:        import org.apache.harmony.awt.internal.nls.Messages;
0026:
0027:        public class ComponentColorModel extends ColorModel {
0028:
0029:            private boolean signed; // Pixel samples are signed.
0030:            // Samples with TransferType DataBuffer.TYPE_BYTE,
0031:            // DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT - 
0032:            // unsigned. Samples with others TransferType - 
0033:            // signed.
0034:
0035:            private boolean integral; // Pixel samples are integral.
0036:            // Samples with TransferType DataBuffer.TYPE_BYTE,
0037:            // DataBuffer.TYPE_USHORT, DataBuffer.Short and 
0038:            // DataBuffer.TYPE_INT - integral.
0039:
0040:            private float scaleFactors[]; // Array of factors for reduction components 
0041:            // values into the form scaled from 0 to 255
0042:
0043:            private boolean donotSupportUnnormalized; // This Color Model don't support
0044:            // unnormolized form 
0045:
0046:            private boolean needAlphaDivide; // hasAlpha && isAlphaPremultiplied
0047:
0048:            private boolean calcValue; // Value was culculated
0049:
0050:            private boolean needScale; // Normalized value need to scale
0051:
0052:            private float minVals[]; // Array of Min normalized values
0053:
0054:            private float ranges[]; // Array of range normalized values  
0055:
0056:            private byte alphaLUT[]; // Lookup table for scale alpha value
0057:
0058:            private byte colorLUTs[][]; // Lookup tables for scale color values
0059:
0060:            private byte from_LINEAR_RGB_LUT[]; // Lookup table for conversion from
0061:            // Linear RGB Color Space into sRGB
0062:
0063:            private byte to_LINEAR_8RGB_LUT[]; // Lookup table for conversion from
0064:            // sRGB Color Space into Linear RGB 
0065:            // 8 bit 
0066:
0067:            private short to_LINEAR_16RGB_LUT[]; // Lookup table for conversion from
0068:            // sRGB Color Space into Linear RGB 
0069:            // 16 bit 
0070:
0071:            private int LINEAR_RGB_Length; // Linear RGB bit length
0072:
0073:            private float fFactor; // Scale factor
0074:
0075:            private boolean is_sRGB; // ColorModel has sRGB ColorSpace  
0076:
0077:            private boolean is_LINEAR_RGB; // Color Model has Linear RGB Color 
0078:
0079:            // Space 
0080:
0081:            public ComponentColorModel(ColorSpace colorSpace, int bits[],
0082:                    boolean hasAlpha, boolean isAlphaPremultiplied,
0083:                    int transparency, int transferType) {
0084:                super (createPixelBits(colorSpace, hasAlpha, transferType),
0085:                        validateBits(bits, colorSpace, hasAlpha, transferType),
0086:                        colorSpace, hasAlpha, isAlphaPremultiplied,
0087:                        transparency, transferType);
0088:
0089:                needScale = false;
0090:                switch (transferType) {
0091:                case DataBuffer.TYPE_BYTE:
0092:                case DataBuffer.TYPE_USHORT:
0093:                case DataBuffer.TYPE_INT:
0094:                    signed = false;
0095:                    integral = true;
0096:                    donotSupportUnnormalized = false;
0097:                    scaleFactors = new float[numComponents];
0098:                    for (int i = 0; i < numColorComponents; i++) {
0099:                        scaleFactors[i] = 1.0f / maxValues[i];
0100:                        if (cs.getMinValue(i) != 0.0f
0101:                                || cs.getMaxValue(i) != 1.0f) {
0102:                            donotSupportUnnormalized = true;
0103:                        }
0104:                    }
0105:                    if (hasAlpha) {
0106:                        maxValues[numColorComponents] = (1 << bits[numColorComponents]) - 1;
0107:                        scaleFactors[numColorComponents] = 1.0f / maxValues[numColorComponents];
0108:                    }
0109:                    break;
0110:                case DataBuffer.TYPE_SHORT:
0111:                    signed = true;
0112:                    integral = true;
0113:                    donotSupportUnnormalized = true;
0114:                    scaleFactors = new float[numComponents];
0115:                    for (int i = 0; i < numComponents; i++) {
0116:                        maxValues[i] = Short.MAX_VALUE;
0117:                        scaleFactors[i] = 1.0f / maxValues[i];
0118:                        if (cs.getMinValue(i) != 0.0f
0119:                                || cs.getMaxValue(i) != 1.0f) {
0120:                            needScale = true;
0121:                        }
0122:                    }
0123:                    if (needScale) {
0124:                        minVals = new float[numColorComponents];
0125:                        ranges = new float[numColorComponents];
0126:                        for (int i = 0; i < numColorComponents; i++) {
0127:                            minVals[i] = cs.getMinValue(i);
0128:                            ranges[i] = cs.getMaxValue(i) - minVals[i];
0129:                        }
0130:                    }
0131:                    break;
0132:                case DataBuffer.TYPE_FLOAT:
0133:                case DataBuffer.TYPE_DOUBLE:
0134:                    signed = true;
0135:                    integral = false;
0136:                    donotSupportUnnormalized = true;
0137:                    break;
0138:                default:
0139:                    // awt.215=transferType is not one of DataBuffer.TYPE_BYTE,
0140:                    //          DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT,
0141:                    //          DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or
0142:                    //          DataBuffer.TYPE_DOUBLE
0143:                    throw new IllegalArgumentException(Messages
0144:                            .getString("awt.215")); //$NON-NLS-1$
0145:                }
0146:
0147:                needAlphaDivide = hasAlpha && isAlphaPremultiplied;
0148:                initLUTs();
0149:            }
0150:
0151:            public ComponentColorModel(ColorSpace colorSpace, boolean hasAlpha,
0152:                    boolean isAlphaPremultiplied, int transparency,
0153:                    int transferType) {
0154:
0155:                this (colorSpace, createPixelBitsArray(colorSpace, hasAlpha,
0156:                        transferType), hasAlpha, isAlphaPremultiplied,
0157:                        transparency, transferType);
0158:            }
0159:
0160:            private static int[] validateBits(int bits[],
0161:                    ColorSpace colorSpace, boolean hasAlpha, int transferType) {
0162:                if (bits != null) {
0163:                    return bits;
0164:                }
0165:
0166:                int numComponents = colorSpace.getNumComponents();
0167:                if (hasAlpha) {
0168:                    numComponents++;
0169:                }
0170:                bits = new int[numComponents];
0171:
0172:                int componentLength = DataBuffer.getDataTypeSize(transferType);
0173:
0174:                for (int i = 0; i < numComponents; i++) {
0175:                    bits[i] = componentLength;
0176:                }
0177:
0178:                return bits;
0179:            }
0180:
0181:            private static int createPixelBits(ColorSpace colorSpace,
0182:                    boolean hasAlpha, int transferType) {
0183:                int numComponents = colorSpace.getNumComponents();
0184:                if (hasAlpha) {
0185:                    numComponents++;
0186:                }
0187:                int componentLength = DataBuffer.getDataTypeSize(transferType);
0188:                return numComponents * componentLength;
0189:            }
0190:
0191:            private static int[] createPixelBitsArray(ColorSpace colorSpace,
0192:                    boolean hasAlpha, int transferType) {
0193:
0194:                int numComponents = colorSpace.getNumComponents();
0195:                if (hasAlpha) {
0196:                    numComponents++;
0197:                }
0198:
0199:                int bits[] = new int[numComponents];
0200:                for (int i = 0; i < numComponents; i++) {
0201:                    bits[i] = DataBuffer.getDataTypeSize(transferType);
0202:                }
0203:                return bits;
0204:            }
0205:
0206:            @Override
0207:            public Object getDataElements(int components[], int offset,
0208:                    Object obj) {
0209:                if (donotSupportUnnormalized) {
0210:                    // awt.213=This ComponentColorModel does not support the unnormalized form
0211:                    throw new IllegalArgumentException(Messages
0212:                            .getString("awt.213")); //$NON-NLS-1$
0213:                }
0214:
0215:                if (offset + numComponents > components.length) {
0216:                    // awt.216=The components array is not large enough to hold all the color and alpha components
0217:                    throw new IllegalArgumentException(Messages
0218:                            .getString("awt.216")); //$NON-NLS-1$
0219:                }
0220:
0221:                switch (transferType) {
0222:                case DataBuffer.TYPE_BYTE:
0223:                    byte ba[];
0224:                    if (obj == null) {
0225:                        ba = new byte[numComponents];
0226:                    } else {
0227:                        ba = (byte[]) obj;
0228:                    }
0229:                    for (int i = 0, idx = offset; i < numComponents; i++, idx++) {
0230:                        ba[i] = (byte) components[idx];
0231:                    }
0232:                    return ba;
0233:                case DataBuffer.TYPE_USHORT:
0234:                    short sa[];
0235:                    if (obj == null) {
0236:                        sa = new short[numComponents];
0237:                    } else {
0238:                        sa = (short[]) obj;
0239:                    }
0240:                    for (int i = 0, idx = offset; i < numComponents; i++, idx++) {
0241:                        sa[i] = (short) components[idx];
0242:                    }
0243:                    return sa;
0244:                case DataBuffer.TYPE_INT:
0245:                    int ia[];
0246:                    if (obj == null) {
0247:                        ia = new int[numComponents];
0248:                    } else {
0249:                        ia = (int[]) obj;
0250:                    }
0251:                    for (int i = 0, idx = offset; i < numComponents; i++, idx++) {
0252:                        ia[i] = components[idx];
0253:                    }
0254:                    return ia;
0255:                default:
0256:                    // awt.217=The transfer type of this ComponentColorModel is not one
0257:                    //          of the following transfer types: DataBuffer.TYPE_BYTE,
0258:                    //          DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT
0259:                    throw new UnsupportedOperationException(Messages
0260:                            .getString("awt.217")); //$NON-NLS-1$
0261:                }
0262:            }
0263:
0264:            @Override
0265:            public Object getDataElements(float normComponents[],
0266:                    int normOffset, Object obj) {
0267:                if (needScale) {
0268:                    for (int i = 0, idx = 0; i < numColorComponents; i++, idx++) {
0269:                        normComponents[idx] = (normComponents[idx] - minVals[i])
0270:                                / ranges[i];
0271:                    }
0272:                }
0273:
0274:                switch (transferType) {
0275:                case DataBuffer.TYPE_BYTE:
0276:                    byte ba[];
0277:                    if (obj == null) {
0278:                        ba = new byte[numComponents];
0279:                    } else {
0280:                        ba = (byte[]) obj;
0281:                    }
0282:
0283:                    if (needAlphaDivide) {
0284:                        float alpha = normComponents[normOffset
0285:                                + numColorComponents];
0286:                        for (int i = 0, idx = normOffset; i < numColorComponents; i++, idx++) {
0287:                            ba[i] = (byte) (normComponents[idx] * alpha
0288:                                    * maxValues[i] + 0.5f);
0289:                        }
0290:                        ba[numColorComponents] = (byte) (normComponents[normOffset
0291:                                + numColorComponents]
0292:                                * maxValues[numColorComponents] + 0.5f);
0293:                    } else {
0294:                        for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0295:                            ba[idx] = (byte) (normComponents[idx]
0296:                                    * maxValues[i] + 0.5f);
0297:                        }
0298:                    }
0299:                    return ba;
0300:
0301:                case DataBuffer.TYPE_USHORT:
0302:                    short usa[];
0303:                    if (obj == null) {
0304:                        usa = new short[numComponents];
0305:                    } else {
0306:                        usa = (short[]) obj;
0307:                    }
0308:
0309:                    if (needAlphaDivide) {
0310:                        float alpha = normComponents[normOffset
0311:                                + numColorComponents];
0312:                        for (int i = 0, idx = 0; i < numColorComponents; i++, idx++) {
0313:                            usa[i] = (short) (normComponents[idx] * alpha
0314:                                    * maxValues[i] + 0.5f);
0315:                        }
0316:                        usa[numColorComponents] = (short) (alpha
0317:                                * maxValues[numColorComponents] + 0.5f);
0318:                    } else {
0319:                        for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0320:                            usa[i] = (short) (normComponents[idx]
0321:                                    * maxValues[i] + 0.5f);
0322:                        }
0323:                    }
0324:                    return usa;
0325:
0326:                case DataBuffer.TYPE_INT:
0327:                    int ia[];
0328:                    if (obj == null) {
0329:                        ia = new int[numComponents];
0330:                    } else {
0331:                        ia = (int[]) obj;
0332:                    }
0333:
0334:                    if (needAlphaDivide) {
0335:                        float alpha = normComponents[normOffset
0336:                                + numColorComponents];
0337:                        for (int i = 0, idx = 0; i < numColorComponents; i++, idx++) {
0338:                            ia[i] = (int) (normComponents[idx] * alpha
0339:                                    * maxValues[i] + 0.5f);
0340:                        }
0341:                        ia[numColorComponents] = (int) (alpha
0342:                                * maxValues[numColorComponents] + 0.5f);
0343:                    } else {
0344:                        for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0345:                            ia[i] = (int) (normComponents[idx] * maxValues[i] + 0.5f);
0346:                        }
0347:                    }
0348:                    return ia;
0349:
0350:                case DataBuffer.TYPE_SHORT:
0351:                    short sa[];
0352:                    if (obj == null) {
0353:                        sa = new short[numComponents];
0354:                    } else {
0355:                        sa = (short[]) obj;
0356:                    }
0357:
0358:                    if (needAlphaDivide) {
0359:                        float alpha = normComponents[normOffset
0360:                                + numColorComponents];
0361:                        for (int i = 0, idx = 0; i < numColorComponents; i++, idx++) {
0362:                            sa[i] = (short) (normComponents[idx] * alpha
0363:                                    * maxValues[i] + 0.5f);
0364:                        }
0365:                        sa[numColorComponents] = (short) (alpha
0366:                                * maxValues[numColorComponents] + 0.5f);
0367:                    } else {
0368:                        for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0369:                            sa[i] = (short) (normComponents[idx] * maxValues[i] + 0.5f);
0370:                        }
0371:                    }
0372:                    return sa;
0373:
0374:                case DataBuffer.TYPE_FLOAT:
0375:                    float fa[];
0376:                    if (obj == null) {
0377:                        fa = new float[numComponents];
0378:                    } else {
0379:                        fa = (float[]) obj;
0380:                    }
0381:
0382:                    if (needAlphaDivide) {
0383:                        float alpha = normComponents[normOffset
0384:                                + numColorComponents];
0385:                        for (int i = 0, idx = 0; i < numColorComponents; i++, idx++) {
0386:                            fa[i] = normComponents[idx] * alpha;
0387:                        }
0388:                        fa[numColorComponents] = alpha;
0389:                    } else {
0390:                        for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0391:                            fa[i] = normComponents[idx];
0392:                        }
0393:                    }
0394:                    return fa;
0395:
0396:                case DataBuffer.TYPE_DOUBLE:
0397:                    double da[];
0398:                    if (obj == null) {
0399:                        da = new double[numComponents];
0400:                    } else {
0401:                        da = (double[]) obj;
0402:                    }
0403:
0404:                    if (needAlphaDivide) {
0405:                        double alpha = normComponents[normOffset
0406:                                + numColorComponents];
0407:                        for (int i = 0, idx = 0; i < numColorComponents; i++, idx++) {
0408:                            da[i] = normComponents[idx] * alpha;
0409:                        }
0410:                        da[numColorComponents] = alpha;
0411:                    } else {
0412:                        for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0413:                            da[i] = normComponents[idx];
0414:                        }
0415:                    }
0416:                    return da;
0417:
0418:                default:
0419:                    // awt.213=This ComponentColorModel does not support the unnormalized form
0420:                    throw new IllegalArgumentException(Messages
0421:                            .getString("awt.213")); //$NON-NLS-1$
0422:                }
0423:            }
0424:
0425:            @Override
0426:            public Object getDataElements(int rgb, Object pixel) {
0427:                float normComp[];
0428:                float comp[];
0429:
0430:                int red = (rgb >> 16) & 0xff;
0431:                int green = (rgb >> 8) & 0xff;
0432:                int blue = rgb & 0xff;
0433:                int alpha = (rgb >> 24) & 0xff;
0434:
0435:                comp = new float[3];
0436:                if (is_sRGB || is_LINEAR_RGB) {
0437:                    if (is_LINEAR_RGB) {
0438:                        if (LINEAR_RGB_Length == 8) {
0439:                            red = to_LINEAR_8RGB_LUT[red] & 0xff;
0440:                            green = to_LINEAR_8RGB_LUT[green] & 0xff;
0441:                            blue = to_LINEAR_8RGB_LUT[blue] & 0xff;
0442:                        } else {
0443:                            red = to_LINEAR_16RGB_LUT[red] & 0xffff;
0444:                            green = to_LINEAR_16RGB_LUT[green] & 0xffff;
0445:                            blue = to_LINEAR_16RGB_LUT[blue] & 0xffff;
0446:                        }
0447:                    }
0448:                    comp[0] = red / fFactor;
0449:                    comp[1] = green / fFactor;
0450:                    comp[2] = blue / fFactor;
0451:                    if (!hasAlpha) {
0452:                        normComp = comp;
0453:                    } else {
0454:                        float normAlpha = alpha / 255.0f;
0455:                        normComp = new float[numComponents];
0456:                        for (int i = 0; i < numColorComponents; i++) {
0457:                            normComp[i] = comp[i];
0458:                        }
0459:                        normComp[numColorComponents] = normAlpha;
0460:                    }
0461:                } else {
0462:                    comp[0] = red / fFactor;
0463:                    comp[1] = green / fFactor;
0464:                    comp[2] = blue / fFactor;
0465:                    float[] defComp = cs.fromRGB(comp);
0466:                    if (!hasAlpha) {
0467:                        normComp = defComp;
0468:                    } else {
0469:                        float normAlpha = alpha / 255.0f;
0470:                        normComp = new float[numComponents];
0471:                        for (int i = 0; i < numColorComponents; i++) {
0472:                            normComp[i] = defComp[i];
0473:                        }
0474:                        normComp[numColorComponents] = normAlpha;
0475:                    }
0476:                }
0477:                if (hasAlpha && isAlphaPremultiplied) {
0478:                    normComp[0] *= normComp[numColorComponents];
0479:                    normComp[1] *= normComp[numColorComponents];
0480:                    normComp[2] *= normComp[numColorComponents];
0481:                }
0482:
0483:                return getDataElements(normComp, 0, pixel);
0484:            }
0485:
0486:            @Override
0487:            public WritableRaster getAlphaRaster(WritableRaster raster) {
0488:                if (!hasAlpha) {
0489:                    return null;
0490:                }
0491:
0492:                int x = raster.getMinX();
0493:                int y = raster.getMinY();
0494:                int bandList[] = new int[1];
0495:                bandList[0] = raster.getNumBands() - 1;
0496:
0497:                return raster.createWritableChild(x, y, raster.getWidth(),
0498:                        raster.getHeight(), x, y, bandList);
0499:            }
0500:
0501:            @Override
0502:            public ColorModel coerceData(WritableRaster raster,
0503:                    boolean isAlphaPremultiplied) {
0504:                if (!hasAlpha
0505:                        || this .isAlphaPremultiplied == isAlphaPremultiplied) {
0506:                    return this ;
0507:                }
0508:
0509:                int minX = raster.getMinX();
0510:                int minY = raster.getMinY();
0511:                int w = raster.getWidth();
0512:                int h = raster.getHeight();
0513:
0514:                if (isAlphaPremultiplied) {
0515:                    switch (transferType) {
0516:                    case DataBuffer.TYPE_BYTE:
0517:                    case DataBuffer.TYPE_USHORT:
0518:                    case DataBuffer.TYPE_INT:
0519:                        float alphaFactor = maxValues[numColorComponents];
0520:                        int iComponents[] = null;
0521:                        int iTransparentComponents[] = new int[numComponents];
0522:                        for (int i = 0; i < h; i++, minY++) {
0523:                            for (int j = 0, x = minX; j < w; j++, x++) {
0524:                                iComponents = raster.getPixel(x, minY,
0525:                                        iComponents);
0526:                                if (iComponents[numColorComponents] == 0) {
0527:                                    raster.setPixel(x, minY,
0528:                                            iTransparentComponents);
0529:                                } else {
0530:                                    float alpha = iComponents[numColorComponents]
0531:                                            / alphaFactor;
0532:                                    for (int n = 0; n < numColorComponents; n++) {
0533:                                        iComponents[n] = (int) (alpha
0534:                                                * iComponents[n] + 0.5f);
0535:                                    }
0536:                                    raster.setPixel(x, minY, iComponents);
0537:                                }
0538:                            }
0539:
0540:                        }
0541:                        break;
0542:
0543:                    case DataBuffer.TYPE_SHORT:
0544:                        float sAlphaFactor = maxValues[numColorComponents];
0545:                        short sComponents[] = null;
0546:                        short sTransparentComponents[] = new short[numComponents];
0547:                        for (int i = 0; i < h; i++, minY++) {
0548:                            for (int j = 0, x = minX; j < w; j++, x++) {
0549:                                sComponents = (short[]) raster.getDataElements(
0550:                                        x, minY, sComponents);
0551:                                if (sComponents[numColorComponents] == 0) {
0552:                                    raster.setDataElements(x, minY,
0553:                                            sTransparentComponents);
0554:                                } else {
0555:                                    float alpha = sComponents[numColorComponents]
0556:                                            / sAlphaFactor;
0557:                                    for (int n = 0; n < numColorComponents; n++) {
0558:                                        sComponents[n] = (byte) (alpha
0559:                                                * sComponents[n] + 0.5f);
0560:                                    }
0561:                                    raster
0562:                                            .setDataElements(x, minY,
0563:                                                    sComponents);
0564:                                }
0565:                            }
0566:
0567:                        }
0568:                        break;
0569:
0570:                    case DataBuffer.TYPE_FLOAT:
0571:                        float fComponents[] = null;
0572:                        float fTransparentComponents[] = new float[numComponents];
0573:                        for (int i = 0; i < h; i++, minY++) {
0574:                            for (int j = 0, x = minX; j < w; j++, x++) {
0575:                                fComponents = raster.getPixel(x, minY,
0576:                                        fComponents);
0577:                                if (fComponents[numColorComponents] == 0.0f) {
0578:                                    raster.setDataElements(x, minY,
0579:                                            fTransparentComponents);
0580:                                } else {
0581:                                    float alpha = fComponents[numColorComponents];
0582:                                    for (int n = 0; n < numColorComponents; n++) {
0583:                                        fComponents[n] = fComponents[n] * alpha;
0584:                                    }
0585:                                    raster.setPixel(x, minY, fComponents);
0586:                                }
0587:                            }
0588:
0589:                        }
0590:                        break;
0591:
0592:                    case DataBuffer.TYPE_DOUBLE:
0593:                        double dComponents[] = null;
0594:                        double dTransparentComponents[] = new double[numComponents];
0595:                        for (int i = 0; i < h; i++, minY++) {
0596:                            for (int j = 0, x = minX; j < w; j++, x++) {
0597:                                dComponents = raster.getPixel(x, minY,
0598:                                        dComponents);
0599:                                if (dComponents[numColorComponents] == 0.0) {
0600:                                    raster.setPixel(x, minY,
0601:                                            dTransparentComponents);
0602:                                } else {
0603:                                    double alpha = dComponents[numColorComponents];
0604:                                    for (int n = 0; n < numColorComponents; n++) {
0605:                                        dComponents[n] = dComponents[n] * alpha;
0606:                                    }
0607:                                    raster.setPixel(x, minY, dComponents);
0608:                                }
0609:                            }
0610:
0611:                        }
0612:                        break;
0613:
0614:                    default:
0615:                        // awt.219=This transferType is not supported by this color model
0616:                        throw new UnsupportedOperationException(Messages
0617:                                .getString("awt.219")); //$NON-NLS-1$
0618:                    }
0619:                } else {
0620:                    switch (transferType) {
0621:                    case DataBuffer.TYPE_BYTE:
0622:                    case DataBuffer.TYPE_USHORT:
0623:                    case DataBuffer.TYPE_INT:
0624:                        float alphaFactor = maxValues[numColorComponents];
0625:                        int iComponents[] = null;
0626:                        int iTransparentComponents[] = new int[numComponents];
0627:                        for (int i = 0; i < h; i++, minY++) {
0628:                            for (int j = 0, x = minX; j < w; j++, x++) {
0629:                                iComponents = raster.getPixel(x, minY,
0630:                                        iComponents);
0631:                                if (iComponents[numColorComponents] == 0) {
0632:                                    raster.setPixel(x, minY,
0633:                                            iTransparentComponents);
0634:                                } else {
0635:                                    float alpha = iComponents[numColorComponents]
0636:                                            / alphaFactor;
0637:                                    for (int n = 0; n < numColorComponents; n++) {
0638:                                        iComponents[n] = (int) (iComponents[n]
0639:                                                / alpha + 0.5f);
0640:                                    }
0641:                                    raster.setPixel(x, minY, iComponents);
0642:                                }
0643:                            }
0644:
0645:                        }
0646:                        break;
0647:
0648:                    case DataBuffer.TYPE_SHORT:
0649:                        float sAlphaFactor = maxValues[numColorComponents];
0650:                        short sComponents[] = null;
0651:                        short sTransparentComponents[] = new short[numComponents];
0652:                        for (int i = 0; i < h; i++, minY++) {
0653:                            for (int j = 0, x = minX; j < w; j++, x++) {
0654:                                sComponents = (short[]) raster.getDataElements(
0655:                                        x, minY, sComponents);
0656:                                if (sComponents[numColorComponents] == 0) {
0657:                                    raster.setDataElements(x, minY,
0658:                                            sTransparentComponents);
0659:                                } else {
0660:                                    float alpha = sComponents[numColorComponents]
0661:                                            / sAlphaFactor;
0662:                                    for (int n = 0; n < numColorComponents; n++) {
0663:                                        sComponents[n] = (byte) (sComponents[n]
0664:                                                / alpha + 0.5f);
0665:                                    }
0666:                                    raster
0667:                                            .setDataElements(x, minY,
0668:                                                    sComponents);
0669:                                }
0670:                            }
0671:
0672:                        }
0673:                        break;
0674:
0675:                    case DataBuffer.TYPE_FLOAT:
0676:                        float fComponents[] = null;
0677:                        float fTransparentComponents[] = new float[numComponents];
0678:                        for (int i = 0; i < h; i++, minY++) {
0679:                            for (int j = 0, x = minX; j < w; j++, x++) {
0680:                                fComponents = raster.getPixel(x, minY,
0681:                                        fComponents);
0682:                                if (fComponents[numColorComponents] == 0.0f) {
0683:                                    raster.setDataElements(x, minY,
0684:                                            fTransparentComponents);
0685:                                } else {
0686:                                    float alpha = fComponents[numColorComponents];
0687:                                    for (int n = 0; n < numColorComponents; n++) {
0688:                                        fComponents[n] = fComponents[n] / alpha;
0689:                                    }
0690:                                    raster.setPixel(x, minY, fComponents);
0691:                                }
0692:                            }
0693:
0694:                        }
0695:                        break;
0696:
0697:                    case DataBuffer.TYPE_DOUBLE:
0698:                        double dComponents[] = null;
0699:                        double dTransparentComponents[] = new double[numComponents];
0700:                        for (int i = 0; i < h; i++, minY++) {
0701:                            for (int j = 0, x = minX; j < w; j++, x++) {
0702:                                dComponents = raster.getPixel(x, minY,
0703:                                        dComponents);
0704:                                if (dComponents[numColorComponents] == 0.0) {
0705:                                    raster.setPixel(x, minY,
0706:                                            dTransparentComponents);
0707:                                } else {
0708:                                    double alpha = dComponents[numColorComponents];
0709:                                    for (int n = 0; n < numColorComponents; n++) {
0710:                                        dComponents[n] = dComponents[n] / alpha;
0711:                                    }
0712:                                    raster.setPixel(x, minY, dComponents);
0713:                                }
0714:                            }
0715:
0716:                        }
0717:                        break;
0718:                    default:
0719:                        // awt.219=This transferType is not supported by this color model
0720:                        throw new UnsupportedOperationException(Messages
0721:                                .getString("awt.219")); //$NON-NLS-1$
0722:                    }
0723:                }
0724:
0725:                if (!signed) {
0726:                    return new ComponentColorModel(cs, bits, hasAlpha,
0727:                            isAlphaPremultiplied, transparency, transferType);
0728:                }
0729:
0730:                return new ComponentColorModel(cs, null, hasAlpha,
0731:                        isAlphaPremultiplied, transparency, transferType);
0732:            }
0733:
0734:            @Override
0735:            public int[] getComponents(Object pixel, int[] components,
0736:                    int offset) {
0737:                if (donotSupportUnnormalized) {
0738:                    // awt.213=This ComponentColorModel does not support the unnormalized form
0739:                    throw new IllegalArgumentException(Messages
0740:                            .getString("awt.213")); //$NON-NLS-1$
0741:                }
0742:
0743:                if (components == null) {
0744:                    components = new int[offset + numComponents];
0745:                } else if (offset + numComponents > components.length) {
0746:                    // awt.218=The components array is not large enough to hold all the color and alpha components
0747:                    throw new IllegalArgumentException(Messages
0748:                            .getString("awt.218")); //$NON-NLS-1$
0749:                }
0750:
0751:                switch (transferType) {
0752:                case DataBuffer.TYPE_BYTE:
0753:                    byte ba[] = (byte[]) pixel;
0754:
0755:                    for (int i = 0, idx = offset; i < numComponents; i++, idx++) {
0756:                        components[idx] = ba[i] & 0xff;
0757:                    }
0758:                    return components;
0759:
0760:                case DataBuffer.TYPE_USHORT:
0761:                    short sa[] = (short[]) pixel;
0762:                    for (int i = 0, idx = offset; i < numComponents; i++, idx++) {
0763:                        components[idx] = sa[i] & 0xffff;
0764:                    }
0765:                    return components;
0766:
0767:                case DataBuffer.TYPE_INT:
0768:                    int ia[] = (int[]) pixel;
0769:                    for (int i = 0, idx = offset; i < numComponents; i++, idx++) {
0770:                        components[idx] = ia[i];
0771:                    }
0772:                    return components;
0773:
0774:                default:
0775:                    // awt.217=The transfer type of this ComponentColorModel is not one
0776:                    //          of the following transfer types: DataBuffer.TYPE_BYTE,
0777:                    //          DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT
0778:                    throw new UnsupportedOperationException(Messages
0779:                            .getString("awt.217")); //$NON-NLS-1$
0780:                }
0781:
0782:            }
0783:
0784:            @Override
0785:            public float[] getNormalizedComponents(Object pixel,
0786:                    float normComponents[], int normOffset) {
0787:
0788:                if (normComponents == null) {
0789:                    normComponents = new float[numComponents + normOffset];
0790:                }
0791:
0792:                switch (transferType) {
0793:                case DataBuffer.TYPE_BYTE:
0794:                    byte ba[] = (byte[]) pixel;
0795:                    for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0796:                        normComponents[idx] = (ba[i] & 0xff) * scaleFactors[i];
0797:                    }
0798:                    break;
0799:
0800:                case DataBuffer.TYPE_USHORT:
0801:                    short usa[] = (short[]) pixel;
0802:                    for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0803:                        normComponents[idx] = (usa[i] & 0xffff)
0804:                                * scaleFactors[i];
0805:                    }
0806:                    break;
0807:
0808:                case DataBuffer.TYPE_INT:
0809:                    int ia[] = (int[]) pixel;
0810:                    for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0811:                        normComponents[idx] = ia[i] * scaleFactors[i];
0812:                    }
0813:                    break;
0814:
0815:                case DataBuffer.TYPE_SHORT:
0816:                    short sa[] = (short[]) pixel;
0817:                    for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0818:                        normComponents[idx] = sa[i] * scaleFactors[i];
0819:                    }
0820:                    break;
0821:
0822:                case DataBuffer.TYPE_FLOAT:
0823:                    float fa[] = (float[]) pixel;
0824:                    for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0825:                        normComponents[idx] = fa[i];
0826:                    }
0827:                    break;
0828:
0829:                case DataBuffer.TYPE_DOUBLE:
0830:                    double da[] = (double[]) pixel;
0831:                    for (int i = 0, idx = normOffset; i < numComponents; i++, idx++) {
0832:                        normComponents[idx] = (float) da[i];
0833:                    }
0834:                    break;
0835:
0836:                default:
0837:                    // awt.21A=This ComponentColorModel does not support this transferType
0838:                    throw new IllegalArgumentException(Messages
0839:                            .getString("awt.21A")); //$NON-NLS-1$
0840:                }
0841:
0842:                if (needAlphaDivide) {
0843:                    float alpha = normComponents[normOffset
0844:                            + numColorComponents];
0845:                    for (int i = 0, idx = normOffset; i < numColorComponents; i++, idx++) {
0846:                        normComponents[idx] /= alpha;
0847:                    }
0848:                }
0849:
0850:                if (needScale) {
0851:                    for (int i = 0, idx = normOffset; i < numColorComponents; i++, idx++) {
0852:                        normComponents[idx] = minVals[i] + ranges[i]
0853:                                * normComponents[idx];
0854:                    }
0855:                }
0856:                return normComponents;
0857:            }
0858:
0859:            @Override
0860:            public boolean equals(Object obj) {
0861:                if (!(obj instanceof  ComponentColorModel)) {
0862:                    return false;
0863:                }
0864:                return super .equals(obj);
0865:            }
0866:
0867:            @Override
0868:            public int getRed(Object inData) {
0869:                return getRGBComponent(inData, 0);
0870:            }
0871:
0872:            @Override
0873:            public int getRGB(Object inData) {
0874:                int alpha = getAlpha(inData);
0875:                if (cs.getType() == ColorSpace.TYPE_GRAY) {
0876:                    int gray = getRed(inData);
0877:                    return (alpha << 24 | gray << 16 | gray << 8 | gray);
0878:                }
0879:                return (alpha << 24 | getRed(inData) << 16
0880:                        | getGreen(inData) << 8 | getBlue(inData));
0881:            }
0882:
0883:            @Override
0884:            public int getGreen(Object inData) {
0885:                return getRGBComponent(inData, 1);
0886:            }
0887:
0888:            @Override
0889:            public int getBlue(Object inData) {
0890:                return getRGBComponent(inData, 2);
0891:            }
0892:
0893:            @Override
0894:            public int getAlpha(Object inData) {
0895:                if (!hasAlpha) {
0896:                    return 255;
0897:                }
0898:                int alpha = 0;
0899:
0900:                switch (transferType) {
0901:                case DataBuffer.TYPE_BYTE: {
0902:                    byte ba[] = (byte[]) inData;
0903:                    alpha = ba[numColorComponents] & 0xff;
0904:                    if (bits[numColorComponents] != 8) {
0905:                        return alphaLUT[alpha] & 0xff;
0906:                    }
0907:                    return alpha;
0908:                }
0909:                case DataBuffer.TYPE_USHORT: {
0910:                    short usa[] = (short[]) inData;
0911:                    alpha = usa[numColorComponents] & 0xffff;
0912:                    if (bits[numColorComponents] != 8) {
0913:                        return alphaLUT[alpha] & 0xff;
0914:                    }
0915:                    return alpha;
0916:                }
0917:                case DataBuffer.TYPE_INT: {
0918:                    int ia[] = (int[]) inData;
0919:                    alpha = ia[numColorComponents];
0920:                    if (bits[numColorComponents] != 8) {
0921:                        return alphaLUT[alpha] & 0xff;
0922:                    }
0923:                    return alpha;
0924:                }
0925:                case DataBuffer.TYPE_SHORT: {
0926:                    short sa[] = (short[]) inData;
0927:                    alpha = sa[numColorComponents];
0928:                    if (bits[numColorComponents] != 8) {
0929:                        return alphaLUT[alpha] & 0xff;
0930:                    }
0931:                    return alpha;
0932:                }
0933:                case DataBuffer.TYPE_FLOAT: {
0934:                    float fa[] = (float[]) inData;
0935:                    return (int) (fa[numColorComponents] * 255.0f + 0.5f);
0936:                }
0937:                case DataBuffer.TYPE_DOUBLE: {
0938:                    double da[] = (double[]) inData;
0939:                    return (int) (da[numColorComponents] * 255.0 + 0.5);
0940:                }
0941:                default: {
0942:                    // awt.214=This Color Model doesn't support this transferType
0943:                    throw new UnsupportedOperationException(Messages
0944:                            .getString("awt.214")); //$NON-NLS-1$
0945:                }
0946:                }
0947:            }
0948:
0949:            @Override
0950:            public WritableRaster createCompatibleWritableRaster(int w, int h) {
0951:                SampleModel sm = createCompatibleSampleModel(w, h);
0952:                DataBuffer db = sm.createDataBuffer();
0953:                return Raster.createWritableRaster(sm, db, null);
0954:            }
0955:
0956:            @Override
0957:            public boolean isCompatibleSampleModel(SampleModel sm) {
0958:                if (!(sm instanceof  ComponentSampleModel)) {
0959:                    return false;
0960:                }
0961:                if (numComponents != sm.getNumBands()) {
0962:                    return false;
0963:                }
0964:                if (transferType != sm.getTransferType()) {
0965:                    return false;
0966:                }
0967:                return true;
0968:            }
0969:
0970:            @Override
0971:            public SampleModel createCompatibleSampleModel(int w, int h) {
0972:                int bandOffsets[] = new int[numComponents];
0973:                for (int i = 0; i < numComponents; i++) {
0974:                    bandOffsets[i] = i;
0975:                }
0976:
0977:                switch (transferType) {
0978:                case DataBuffer.TYPE_BYTE:
0979:                case DataBuffer.TYPE_USHORT:
0980:                    return new PixelInterleavedSampleModel(transferType, w, h,
0981:                            numComponents, w * numComponents, bandOffsets);
0982:
0983:                default:
0984:                    return new ComponentSampleModel(transferType, w, h,
0985:                            numComponents, w * numComponents, bandOffsets);
0986:                }
0987:            }
0988:
0989:            @Override
0990:            public boolean isCompatibleRaster(Raster raster) {
0991:                SampleModel sm = raster.getSampleModel();
0992:                if (!(sm instanceof  ComponentSampleModel)) {
0993:                    return false;
0994:                }
0995:
0996:                if (sm.getNumBands() != numComponents) {
0997:                    return false;
0998:                }
0999:                if (raster.getTransferType() != transferType) {
1000:                    return false;
1001:                }
1002:
1003:                int sampleSizes[] = sm.getSampleSize();
1004:                for (int i = 0; i < numComponents; i++) {
1005:                    if (bits[i] != sampleSizes[i]) {
1006:                        return false;
1007:                    }
1008:                }
1009:                return true;
1010:            }
1011:
1012:            @Override
1013:            public float[] getNormalizedComponents(int components[],
1014:                    int offset, float normComponents[], int normOffset) {
1015:                if (donotSupportUnnormalized) {
1016:                    // awt.213=This ComponentColorModel does not support the unnormalized form
1017:                    throw new IllegalArgumentException(Messages
1018:                            .getString("awt.213")); //$NON-NLS-1$
1019:                }
1020:
1021:                return super .getNormalizedComponents(components, offset,
1022:                        normComponents, normOffset);
1023:            }
1024:
1025:            @Override
1026:            public int getDataElement(int[] components, int offset) {
1027:                if (numComponents > 1) {
1028:                    // awt.212=There is more than one component in this ColorModel
1029:                    throw new IllegalArgumentException(Messages
1030:                            .getString("awt.212")); //$NON-NLS-1$
1031:                }
1032:                if (donotSupportUnnormalized) {
1033:                    // awt.213=This ComponentColorModel does not support the unnormalized form
1034:                    throw new IllegalArgumentException(Messages
1035:                            .getString("awt.213")); //$NON-NLS-1$
1036:                }
1037:                return components[offset];
1038:            }
1039:
1040:            @Override
1041:            public int[] getUnnormalizedComponents(float[] normComponents,
1042:                    int normOffset, int[] components, int offset) {
1043:
1044:                if (donotSupportUnnormalized) {
1045:                    // awt.213=This ComponentColorModel does not support the unnormalized form
1046:                    throw new IllegalArgumentException(Messages
1047:                            .getString("awt.213")); //$NON-NLS-1$
1048:                }
1049:
1050:                if (normComponents.length - normOffset < numComponents) {
1051:                    // awt.21B=The length of normComponents minus normOffset is less than numComponents
1052:                    throw new IllegalArgumentException(Messages
1053:                            .getString("awt.21B")); //$NON-NLS-1$
1054:                }
1055:
1056:                return super .getUnnormalizedComponents(normComponents,
1057:                        normOffset, components, offset);
1058:            }
1059:
1060:            @Override
1061:            public int getDataElement(float normComponents[], int normOffset) {
1062:                if (numComponents > 1) {
1063:                    // awt.212=There is more than one component in this ColorModel
1064:                    throw new IllegalArgumentException(Messages
1065:                            .getString("awt.212")); //$NON-NLS-1$
1066:                }
1067:                if (signed) {
1068:                    // awt.210=The component value for this ColorModel is signed
1069:                    throw new IllegalArgumentException(Messages
1070:                            .getString("awt.210")); //$NON-NLS-1$
1071:                }
1072:
1073:                Object pixel = getDataElements(normComponents, normOffset, null);
1074:
1075:                switch (transferType) {
1076:                case DataBuffer.TYPE_BYTE:
1077:                    byte ba[] = (byte[]) pixel;
1078:                    return ba[0] & 0xff;
1079:                case DataBuffer.TYPE_USHORT:
1080:                    short sa[] = (short[]) pixel;
1081:                    return sa[0] & 0xffff;
1082:                case DataBuffer.TYPE_INT:
1083:                    int ia[] = (int[]) pixel;
1084:                    return ia[0];
1085:                default:
1086:                    // awt.211=Pixel values for this ColorModel are not conveniently
1087:                    //          representable as a single int
1088:                    throw new IllegalArgumentException(Messages
1089:                            .getString("awt.211")); //$NON-NLS-1$
1090:                }
1091:            }
1092:
1093:            @Override
1094:            public int[] getComponents(int pixel, int components[], int offset) {
1095:                if (numComponents > 1) {
1096:                    // awt.212=There is more than one component in this ColorModel
1097:                    throw new IllegalArgumentException(Messages
1098:                            .getString("awt.212")); //$NON-NLS-1$
1099:                }
1100:                if (donotSupportUnnormalized) {
1101:                    // awt.213=This ComponentColorModel does not support the unnormalized form
1102:                    throw new IllegalArgumentException(Messages
1103:                            .getString("awt.213")); //$NON-NLS-1$
1104:                }
1105:
1106:                if (components == null) {
1107:                    components = new int[offset + 1];
1108:                }
1109:
1110:                components[offset] = pixel & maxValues[0];
1111:                return components;
1112:            }
1113:
1114:            @Override
1115:            public int getRed(int pixel) {
1116:                float rgb[] = toRGB(pixel);
1117:                return (int) (rgb[0] * 255.0f + 0.5f);
1118:            }
1119:
1120:            @Override
1121:            public int getRGB(int pixel) {
1122:                return (getAlpha(pixel) << 24) | (getRed(pixel) << 16)
1123:                        | (getGreen(pixel) << 8) | getBlue(pixel);
1124:            }
1125:
1126:            @Override
1127:            public int getGreen(int pixel) {
1128:                float rgb[] = toRGB(pixel);
1129:                return (int) (rgb[1] * 255.0f + 0.5f);
1130:            }
1131:
1132:            @Override
1133:            public int getBlue(int pixel) {
1134:                float rgb[] = toRGB(pixel);
1135:                return (int) (rgb[2] * 255.0f + 0.5f);
1136:            }
1137:
1138:            @Override
1139:            public int getAlpha(int pixel) {
1140:
1141:                // This method throw IllegalArgumentException according to 
1142:                // Java API Spacification
1143:                if (signed) {
1144:                    // awt.210=The component value for this ColorModel is signed
1145:                    throw new IllegalArgumentException(Messages
1146:                            .getString("awt.210")); //$NON-NLS-1$
1147:                }
1148:
1149:                if (numComponents > 1) {
1150:                    // awt.212=There is more than one component in this ColorModel
1151:                    throw new IllegalArgumentException(Messages
1152:                            .getString("awt.212")); //$NON-NLS-1$
1153:                }
1154:
1155:                return 255;
1156:            }
1157:
1158:            /**
1159:             * Initialization of Lookup tables
1160:             */
1161:            private void initLUTs() {
1162:                is_sRGB = cs.isCS_sRGB();
1163:                is_LINEAR_RGB = (cs == LUTColorConverter.LINEAR_RGB_CS);
1164:
1165:                if (hasAlpha && bits[numColorComponents] != 8 && integral) {
1166:                    alphaLUT = new byte[maxValues[numColorComponents] + 1];
1167:                    for (int i = 0; i <= maxValues[numColorComponents]; i++) {
1168:                        alphaLUT[i] = (byte) (scaleFactors[numColorComponents]
1169:                                * i + 0.5f);
1170:                    }
1171:                }
1172:
1173:                if (is_LINEAR_RGB) {
1174:                    if (maxBitLength > 8) {
1175:                        LINEAR_RGB_Length = 16;
1176:                        from_LINEAR_RGB_LUT = LUTColorConverter
1177:                                .getFrom16lRGBtosRGB_LUT();
1178:                        to_LINEAR_16RGB_LUT = LUTColorConverter
1179:                                .getFromsRGBto16lRGB_LUT();
1180:                    } else {
1181:                        LINEAR_RGB_Length = 8;
1182:                        from_LINEAR_RGB_LUT = LUTColorConverter
1183:                                .getFrom8lRGBtosRGB_LUT();
1184:                        to_LINEAR_8RGB_LUT = LUTColorConverter
1185:                                .getFromsRGBto8lRGB_LUT();
1186:                    }
1187:                    fFactor = ((1 << LINEAR_RGB_Length) - 1);
1188:                } else {
1189:                    fFactor = 255.0f;
1190:                }
1191:
1192:                if (!isAlphaPremultiplied && integral) {
1193:                    colorLUTs = new byte[3][];
1194:
1195:                    if (is_sRGB) {
1196:                        for (int i = 0; i < numColorComponents; i++) {
1197:                            if (bits[i] != 8) {
1198:                                for (int j = 0; j < i; j++) {
1199:                                    if (bits[i] == bits[j]) {
1200:                                        colorLUTs[i] = colorLUTs[j];
1201:                                        break;
1202:                                    }
1203:                                }
1204:                                colorLUTs[i] = new byte[maxValues[i] + 1];
1205:                                for (int j = 0; j <= maxValues[0]; j++) {
1206:                                    colorLUTs[i][j] = (byte) (scaleFactors[i]
1207:                                            * j + 0.5f);
1208:                                }
1209:                            }
1210:                        }
1211:                    }
1212:
1213:                    if (is_LINEAR_RGB) {
1214:
1215:                        for (int i = 0; i < numColorComponents; i++) {
1216:                            if (bits[i] != LINEAR_RGB_Length) {
1217:                                for (int j = 0; j < i; j++) {
1218:                                    if (bits[i] == bits[j]) {
1219:                                        colorLUTs[i] = colorLUTs[j];
1220:                                        break;
1221:                                    }
1222:                                }
1223:                                colorLUTs[i] = new byte[maxValues[i] + 1];
1224:                                for (int j = 0; j <= maxValues[0]; j++) {
1225:                                    int idx;
1226:                                    if (LINEAR_RGB_Length == 8) {
1227:                                        idx = (int) (scaleFactors[i] * j + 0.5f);
1228:                                    } else {
1229:                                        idx = (int) (scaleFactors[i] * j
1230:                                                * 257.0f + 0.5f);
1231:                                    }
1232:                                    colorLUTs[i][j] = from_LINEAR_RGB_LUT[idx];
1233:                                }
1234:                            }
1235:                        }
1236:                    }
1237:
1238:                }
1239:            }
1240:
1241:            /**
1242:             * @param pixel - int representation of pixel
1243:             * @return - array of normalized sRGB components
1244:             */
1245:            private float[] toRGB(int pixel) {
1246:
1247:                // This method throw IllegalArgumentException according to 
1248:                // Java API Spacification
1249:                if (signed) {
1250:                    // awt.210=The component value for this ColorModel is signed
1251:                    throw new IllegalArgumentException(Messages
1252:                            .getString("awt.210")); //$NON-NLS-1$
1253:                }
1254:
1255:                if (numComponents > 1) {
1256:                    // awt.212=There is more than one component in this ColorModel
1257:                    throw new IllegalArgumentException(Messages
1258:                            .getString("awt.212")); //$NON-NLS-1$
1259:                }
1260:
1261:                Object obj = null;
1262:
1263:                switch (transferType) {
1264:                case DataBuffer.TYPE_BYTE:
1265:                    byte ba[] = new byte[1];
1266:                    ba[0] = (byte) pixel;
1267:                    obj = ba;
1268:                    break;
1269:
1270:                case DataBuffer.TYPE_USHORT:
1271:                    short sa[] = new short[1];
1272:                    sa[0] = (short) pixel;
1273:                    obj = sa;
1274:                    break;
1275:
1276:                case DataBuffer.TYPE_INT:
1277:                    int ia[] = new int[1];
1278:                    ia[0] = pixel;
1279:                    obj = ia;
1280:                    break;
1281:
1282:                }
1283:
1284:                return cs.toRGB(getNormalizedComponents(obj, null, 0));
1285:            }
1286:
1287:            /**
1288:             * @param pixel - pixel 
1289:             * @param idx - index of component
1290:             * @return - RGB value from 0 to 255 pixel's component 
1291:             */
1292:            private int getRGBComponent(Object pixel, int idx) {
1293:                if (is_sRGB) {
1294:                    int comp = getDefComponent(pixel, idx);
1295:                    if (calcValue || bits[idx] == 8) {
1296:                        return comp;
1297:                    }
1298:                    return colorLUTs[idx][comp] & 0xff;
1299:                } else if (is_LINEAR_RGB) {
1300:                    int comp = getDefComponent(pixel, idx);
1301:                    if (calcValue || bits[idx] == LINEAR_RGB_Length) {
1302:                        return from_LINEAR_RGB_LUT[comp] & 0xff;
1303:                    }
1304:                    return colorLUTs[idx][comp] & 0xff;
1305:                }
1306:
1307:                float normComp[] = getNormalizedComponents(pixel, null, 0);
1308:                float rgbComp[] = cs.toRGB(normComp);
1309:                return (int) (rgbComp[idx] * 255.0f + 0.5f);
1310:            }
1311:
1312:            /**
1313:             * @param pixel - pixel
1314:             * @param idx - index of component
1315:             * @return - tentative value of the pixel component
1316:             */
1317:            private int getDefComponent(Object pixel, int idx) {
1318:                int comp = 0;
1319:                calcValue = false;
1320:
1321:                switch (transferType) {
1322:                case DataBuffer.TYPE_BYTE:
1323:                    byte ba[] = (byte[]) pixel;
1324:                    comp = ba[idx] & 0xff;
1325:                    if (needAlphaDivide) {
1326:                        int alpha = ba[numColorComponents] & 0xff;
1327:                        if (alpha == 0) {
1328:                            comp = 0;
1329:                        } else {
1330:                            float normAlpha = scaleFactors[numColorComponents]
1331:                                    * alpha;
1332:                            comp = (int) (comp * fFactor / normAlpha + 0.5f);
1333:                        }
1334:                        calcValue = true;
1335:                    }
1336:                    return comp;
1337:
1338:                case DataBuffer.TYPE_USHORT:
1339:                    short usa[] = (short[]) pixel;
1340:                    comp = usa[idx] & 0xffff;
1341:                    if (needAlphaDivide) {
1342:                        int alpha = usa[numColorComponents] & 0xffff;
1343:                        if (alpha == 0) {
1344:                            comp = 0;
1345:                        } else {
1346:                            float normAlpha = scaleFactors[numColorComponents]
1347:                                    * alpha;
1348:                            comp = (int) (comp * fFactor / normAlpha + 0.5f);
1349:                        }
1350:                        calcValue = true;
1351:                    }
1352:                    return comp;
1353:
1354:                case DataBuffer.TYPE_INT:
1355:                    int ia[] = (int[]) pixel;
1356:                    comp = ia[idx];
1357:                    if (needAlphaDivide) {
1358:                        int alpha = ia[numColorComponents];
1359:                        if (alpha == 0) {
1360:                            comp = 0;
1361:                        } else {
1362:                            float normAlpha = scaleFactors[numColorComponents]
1363:                                    * alpha;
1364:                            comp = (int) (comp * fFactor / normAlpha + 0.5f);
1365:                        }
1366:                        calcValue = true;
1367:                    }
1368:                    return comp;
1369:
1370:                case DataBuffer.TYPE_SHORT:
1371:                    short sa[] = (short[]) pixel;
1372:                    comp = sa[idx];
1373:                    if (needAlphaDivide) {
1374:                        int alpha = sa[numColorComponents];
1375:                        if (alpha == 0) {
1376:                            comp = 0;
1377:                        } else {
1378:                            float normAlpha = scaleFactors[numColorComponents]
1379:                                    * alpha;
1380:                            comp = (int) (comp * fFactor / normAlpha + 0.5f);
1381:                        }
1382:                        calcValue = true;
1383:                    }
1384:                    return comp;
1385:
1386:                case DataBuffer.TYPE_FLOAT:
1387:                    float fa[] = (float[]) pixel;
1388:                    if (needAlphaDivide) {
1389:                        float alpha = fa[numColorComponents];
1390:                        if (fa[numColorComponents] == 0.0f) {
1391:                            comp = 0;
1392:                        } else {
1393:                            comp = (int) (fa[idx] * fFactor / alpha + 0.5f);
1394:                        }
1395:                    } else {
1396:                        comp = (int) (fa[idx] * fFactor + 0.5f);
1397:                    }
1398:                    calcValue = true;
1399:                    return comp;
1400:
1401:                case DataBuffer.TYPE_DOUBLE:
1402:                    double da[] = (double[]) pixel;
1403:                    if (needAlphaDivide) {
1404:                        if (da[numColorComponents] == 0.0) {
1405:                            comp = 0;
1406:                        } else {
1407:                            comp = (int) (da[idx] * fFactor
1408:                                    / da[numColorComponents] + 0.5);
1409:                        }
1410:                    } else {
1411:                        comp = (int) (da[idx] * fFactor + 0.5);
1412:                    }
1413:                    calcValue = true;
1414:                    return comp;
1415:
1416:                default:
1417:                    // awt.214=This Color Model doesn't support this transferType
1418:                    throw new UnsupportedOperationException(Messages
1419:                            .getString("awt.214")); //$NON-NLS-1$
1420:                }
1421:            }
1422:
1423:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.