Source Code Cross Referenced for IndexColorModel.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) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /**
018:         * @author Igor V. Stolyarov
019:         * @version $Revision$
020:         */package java.awt.image;
021:
022:        import java.awt.Transparency;
023:        import java.awt.color.ColorSpace;
024:        import java.math.BigInteger;
025:
026:        import org.apache.harmony.awt.internal.nls.Messages;
027:
028:        public class IndexColorModel extends ColorModel {
029:
030:            private int colorMap[]; // Color Map  
031:
032:            private int mapSize; // Color Map size
033:
034:            private int transparentIndex; // Index of fully transparent pixel
035:
036:            private boolean grayPalette; // Color Model has Color Map with Gray Pallete
037:
038:            private BigInteger validBits; // Specify valid Color Map values
039:
040:            private static final int CACHESIZE = 20; // Cache size. Cache used for 
041:            // improving performance of selection
042:            // nearest color in Color Map
043:
044:            private final int cachetable[] = new int[CACHESIZE * 2]; // Cache table - used for 
045:            // storing RGB values and that appropriate indices 
046:            // in the Color Map 
047:
048:            private int nextInsertIdx = 0; // Next index for insertion into Cache table
049:
050:            private int totalInserted = 0; // Number of inserted values into Cache table
051:
052:            public IndexColorModel(int bits, int size, int cmap[], int start,
053:                    int transferType, BigInteger validBits) {
054:
055:                super (bits, IndexColorModel.createBits(true), ColorSpace
056:                        .getInstance(ColorSpace.CS_sRGB), true, false,
057:                        Transparency.OPAQUE, validateTransferType(transferType));
058:
059:                if (size < 1) {
060:                    // awt.264=Size of the color map is less than 1
061:                    throw new IllegalArgumentException(Messages
062:                            .getString("awt.264")); //$NON-NLS-1$
063:                }
064:
065:                mapSize = size;
066:                colorMap = new int[mapSize];
067:                transparentIndex = -1;
068:
069:                if (validBits != null) {
070:                    for (int i = 0; i < mapSize; i++) {
071:                        if (!validBits.testBit(i)) {
072:                            this .validBits = validBits;
073:                        }
074:                        break;
075:                    }
076:                }
077:
078:                transparency = Transparency.OPAQUE;
079:                int alphaMask = 0xff000000;
080:                int alpha = 0;
081:
082:                for (int i = 0; i < mapSize; i++, start++) {
083:                    colorMap[i] = cmap[start];
084:                    alpha = cmap[start] & alphaMask;
085:
086:                    if (alpha == alphaMask) {
087:                        continue;
088:                    }
089:                    if (alpha == 0) {
090:                        if (transparentIndex < 0) {
091:                            transparentIndex = i;
092:                        }
093:                        if (transparency == Transparency.OPAQUE) {
094:                            transparency = Transparency.BITMASK;
095:                        }
096:                    } else if (alpha != alphaMask
097:                            && transparency != Transparency.TRANSLUCENT) {
098:                        transparency = Transparency.TRANSLUCENT;
099:                    }
100:
101:                }
102:                checkPalette();
103:
104:            }
105:
106:            public IndexColorModel(int bits, int size, int cmap[], int start,
107:                    boolean hasalpha, int trans, int transferType) {
108:
109:                super (bits, IndexColorModel
110:                        .createBits(hasalpha || (trans >= 0)), ColorSpace
111:                        .getInstance(ColorSpace.CS_sRGB),
112:                        (hasalpha || (trans >= 0)), false, Transparency.OPAQUE,
113:                        validateTransferType(transferType));
114:
115:                if (size < 1) {
116:                    // awt.264=Size of the color map is less than 1
117:                    throw new IllegalArgumentException(Messages
118:                            .getString("awt.264")); //$NON-NLS-1$
119:                }
120:
121:                mapSize = size;
122:                colorMap = new int[mapSize];
123:                if (trans >= 0 && trans < mapSize) {
124:                    transparentIndex = trans;
125:                    transparency = Transparency.BITMASK;
126:                } else {
127:                    transparentIndex = -1;
128:                    transparency = Transparency.OPAQUE;
129:                }
130:
131:                int alphaMask = 0xff000000;
132:                int alpha = 0;
133:
134:                for (int i = 0; i < mapSize; i++, start++) {
135:                    if (transparentIndex == i) {
136:                        colorMap[i] = cmap[start] & 0x00ffffff;
137:                        continue;
138:                    }
139:                    if (hasalpha) {
140:                        alpha = cmap[start] & alphaMask;
141:                        colorMap[i] = cmap[start];
142:
143:                        if (alpha == alphaMask) {
144:                            continue;
145:                        }
146:                        if (alpha == 0) {
147:                            if (trans < 0) {
148:                                trans = i;
149:                            }
150:                            if (transparency == Transparency.OPAQUE) {
151:                                transparency = Transparency.BITMASK;
152:                            }
153:                        } else if (alpha != 0
154:                                && transparency != Transparency.TRANSLUCENT) {
155:                            transparency = Transparency.TRANSLUCENT;
156:                        }
157:                    } else {
158:                        colorMap[i] = alphaMask | cmap[start];
159:                    }
160:                }
161:                checkPalette();
162:
163:            }
164:
165:            public IndexColorModel(int bits, int size, byte r[], byte g[],
166:                    byte b[], byte a[]) {
167:
168:                super (bits, IndexColorModel.createBits(true), ColorSpace
169:                        .getInstance(ColorSpace.CS_sRGB), true, false,
170:                        Transparency.OPAQUE, validateTransferType(ColorModel
171:                                .getTransferType(bits)));
172:
173:                createColorMap(size, r, g, b, a, -1);
174:                checkPalette();
175:            }
176:
177:            public IndexColorModel(int bits, int size, byte r[], byte g[],
178:                    byte b[], int trans) {
179:
180:                super (bits, IndexColorModel.createBits((trans >= 0)),
181:                        ColorSpace.getInstance(ColorSpace.CS_sRGB),
182:                        (trans >= 0), false, Transparency.OPAQUE,
183:                        validateTransferType(ColorModel.getTransferType(bits)));
184:
185:                createColorMap(size, r, g, b, null, trans);
186:                checkPalette();
187:            }
188:
189:            public IndexColorModel(int bits, int size, byte r[], byte g[],
190:                    byte b[]) {
191:                super (bits, IndexColorModel.createBits(false), ColorSpace
192:                        .getInstance(ColorSpace.CS_sRGB), false, false,
193:                        Transparency.OPAQUE, validateTransferType(ColorModel
194:                                .getTransferType(bits)));
195:
196:                createColorMap(size, r, g, b, null, -1);
197:                checkPalette();
198:            }
199:
200:            public IndexColorModel(int bits, int size, byte cmap[], int start,
201:                    boolean hasalpha, int trans) {
202:
203:                super (bits, IndexColorModel
204:                        .createBits(hasalpha || (trans >= 0)), ColorSpace
205:                        .getInstance(ColorSpace.CS_sRGB),
206:                        (hasalpha || (trans >= 0)), false, Transparency.OPAQUE,
207:                        validateTransferType(ColorModel.getTransferType(bits)));
208:
209:                if (size < 1) {
210:                    // awt.264=Size of the color map is less than 1
211:                    throw new IllegalArgumentException(Messages
212:                            .getString("awt.264")); //$NON-NLS-1$
213:                }
214:
215:                mapSize = size;
216:                colorMap = new int[mapSize];
217:                transparentIndex = -1;
218:
219:                transparency = Transparency.OPAQUE;
220:                int alpha = 0xff000000;
221:
222:                for (int i = 0; i < mapSize; i++) {
223:                    colorMap[i] = (cmap[start++] & 0xff) << 16
224:                            | (cmap[start++] & 0xff) << 8
225:                            | (cmap[start++] & 0xff);
226:                    if (trans == i) {
227:                        if (transparency == Transparency.OPAQUE) {
228:                            transparency = Transparency.BITMASK;
229:                        }
230:                        if (hasalpha) {
231:                            start++;
232:                        }
233:                        continue;
234:                    }
235:                    if (hasalpha) {
236:                        alpha = cmap[start++] & 0xff;
237:                        if (alpha == 0) {
238:                            if (transparency == Transparency.OPAQUE) {
239:                                transparency = Transparency.BITMASK;
240:                                if (trans < 0) {
241:                                    trans = i;
242:                                }
243:                            }
244:                        } else {
245:                            if (alpha != 0xff
246:                                    && transparency != Transparency.TRANSLUCENT) {
247:                                transparency = Transparency.TRANSLUCENT;
248:                            }
249:                        }
250:                        alpha <<= 24;
251:                    }
252:                    colorMap[i] |= alpha;
253:                }
254:
255:                if (trans >= 0 && trans < mapSize) {
256:                    transparentIndex = trans;
257:                }
258:                checkPalette();
259:
260:            }
261:
262:            public IndexColorModel(int bits, int size, byte cmap[], int start,
263:                    boolean hasalpha) {
264:
265:                this (bits, size, cmap, start, hasalpha, -1);
266:            }
267:
268:            @Override
269:            public Object getDataElements(int[] components, int offset,
270:                    Object pixel) {
271:                int rgb = (components[offset] << 16)
272:                        | (components[offset + 1]) << 8
273:                        | components[offset + 2];
274:                if (hasAlpha) {
275:                    rgb |= components[offset + 3] << 24;
276:                } else {
277:                    rgb |= 0xff000000;
278:                }
279:                return getDataElements(rgb, pixel);
280:            }
281:
282:            @Override
283:            public synchronized Object getDataElements(int rgb, Object pixel) {
284:                int red = (rgb >> 16) & 0xff;
285:                int green = (rgb >> 8) & 0xff;
286:                int blue = rgb & 0xff;
287:                int alpha = rgb >>> 24;
288:                int pixIdx = 0;
289:
290:                for (int i = 0; i < totalInserted; i++) {
291:                    int idx = i * 2;
292:                    if (rgb == cachetable[idx]) {
293:                        return createDataObject(cachetable[idx + 1], pixel);
294:                    }
295:                }
296:
297:                if (!hasAlpha && grayPalette) {
298:                    int grey = (red * 77 + green * 150 + blue * 29 + 128) >>> 8;
299:                    int minError = 255;
300:                    int error = 0;
301:
302:                    for (int i = 0; i < mapSize; i++) {
303:                        error = Math.abs((colorMap[i] & 0xff) - grey);
304:                        if (error < minError) {
305:                            pixIdx = i;
306:                            if (error == 0) {
307:                                break;
308:                            }
309:                            minError = error;
310:                        }
311:                    }
312:                } else if (alpha == 0 && transparentIndex > -1) {
313:                    pixIdx = transparentIndex;
314:                } else {
315:                    int minAlphaError = 255;
316:                    int minError = 195075; // 255^2 + 255^2 + 255^2
317:                    int alphaError;
318:                    int error = 0;
319:
320:                    for (int i = 0; i < mapSize; i++) {
321:                        int pix = colorMap[i];
322:                        if (rgb == pix) {
323:                            pixIdx = i;
324:                            break;
325:                        }
326:                        alphaError = Math.abs(alpha - (pix >>> 24));
327:                        if (alphaError <= minAlphaError) {
328:                            minAlphaError = alphaError;
329:
330:                            int buf = ((pix >> 16) & 0xff) - red;
331:                            error = buf * buf;
332:
333:                            if (error < minError) {
334:                                buf = ((pix >> 8) & 0xff) - green;
335:                                error += buf * buf;
336:
337:                                if (error < minError) {
338:                                    buf = (pix & 0xff) - blue;
339:                                    error += buf * buf;
340:
341:                                    if (error < minError) {
342:                                        pixIdx = i;
343:                                        minError = error;
344:                                    }
345:                                }
346:                            }
347:                        }
348:                    }
349:                }
350:
351:                cachetable[nextInsertIdx] = rgb;
352:                cachetable[nextInsertIdx + 1] = pixIdx;
353:
354:                nextInsertIdx = (nextInsertIdx + 2) % (CACHESIZE * 2);
355:                if (totalInserted < CACHESIZE) {
356:                    totalInserted++;
357:                }
358:
359:                return createDataObject(pixIdx, pixel);
360:            }
361:
362:            public BufferedImage convertToIntDiscrete(Raster raster,
363:                    boolean forceARGB) {
364:
365:                if (!isCompatibleRaster(raster)) {
366:                    // awt.265=The raster argument is not compatible with this IndexColorModel
367:                    throw new IllegalArgumentException(Messages
368:                            .getString("awt.265")); //$NON-NLS-1$
369:                }
370:
371:                ColorModel model;
372:                if (forceARGB || transparency == Transparency.TRANSLUCENT) {
373:                    model = ColorModel.getRGBdefault();
374:                } else if (transparency == Transparency.BITMASK) {
375:                    model = new DirectColorModel(25, 0x00ff0000, 0x0000ff00,
376:                            0x000000ff, 0x01000000);
377:                } else {
378:                    model = new DirectColorModel(24, 0x00ff0000, 0x0000ff00,
379:                            0x000000ff);
380:                }
381:
382:                int w = raster.getWidth();
383:                int h = raster.getHeight();
384:
385:                WritableRaster distRaster = model
386:                        .createCompatibleWritableRaster(w, h);
387:
388:                int minX = raster.getMinX();
389:                int minY = raster.getMinY();
390:
391:                Object obj = null;
392:                int pixels[] = null;
393:
394:                for (int i = 0; i < h; i++, minY++) {
395:                    obj = raster.getDataElements(minX, minY, w, 1, obj);
396:                    if (obj instanceof  byte[]) {
397:                        byte ba[] = (byte[]) obj;
398:                        if (pixels == null) {
399:                            pixels = new int[ba.length];
400:                        }
401:                        for (int j = 0; j < ba.length; j++) {
402:                            pixels[j] = colorMap[ba[j] & 0xff];
403:                        }
404:                    } else if (obj instanceof  short[]) {
405:                        short sa[] = (short[]) obj;
406:                        if (pixels == null) {
407:                            pixels = new int[sa.length];
408:                        }
409:                        for (int j = 0; j < sa.length; j++) {
410:                            pixels[j] = colorMap[sa[j] & 0xffff];
411:                        }
412:                    }
413:                    if (obj instanceof  int[]) {
414:                        int ia[] = (int[]) obj;
415:                        if (pixels == null) {
416:                            pixels = new int[ia.length];
417:                        }
418:                        for (int j = 0; j < ia.length; j++) {
419:                            pixels[j] = colorMap[ia[j]];
420:                        }
421:                    }
422:
423:                    distRaster.setDataElements(0, i, w, 1, pixels);
424:                }
425:
426:                return new BufferedImage(model, distRaster, false, null);
427:            }
428:
429:            public BigInteger getValidPixels() {
430:                return validBits;
431:            }
432:
433:            @Override
434:            public String toString() {
435:                // The output format based on 1.5 release behaviour. 
436:                // It could be reveled such way:
437:                // BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED);
438:                // ColorModel cm = bi.getColorModel();
439:                // System.out.println(cm.toString());
440:                String str = "IndexColorModel: #pixel_bits = " + pixel_bits + //$NON-NLS-1$
441:                        " numComponents = " + numComponents
442:                        + " color space = " + cs + //$NON-NLS-1$ //$NON-NLS-2$
443:                        " transparency = "; //$NON-NLS-1$
444:
445:                if (transparency == Transparency.OPAQUE) {
446:                    str = str + "Transparency.OPAQUE"; //$NON-NLS-1$
447:                } else if (transparency == Transparency.BITMASK) {
448:                    str = str + "Transparency.BITMASK"; //$NON-NLS-1$
449:                } else {
450:                    str = str + "Transparency.TRANSLUCENT"; //$NON-NLS-1$
451:                }
452:
453:                str = str
454:                        + " transIndex = " + transparentIndex + " has alpha = " + //$NON-NLS-1$ //$NON-NLS-2$
455:                        hasAlpha + " isAlphaPre = " + isAlphaPremultiplied; //$NON-NLS-1$
456:
457:                return str;
458:            }
459:
460:            @Override
461:            public int[] getComponents(Object pixel, int components[],
462:                    int offset) {
463:                int pixIdx = -1;
464:                if (pixel instanceof  byte[]) {
465:                    byte ba[] = (byte[]) pixel;
466:                    pixIdx = ba[0] & 0xff;
467:                } else if (pixel instanceof  short[]) {
468:                    short sa[] = (short[]) pixel;
469:                    pixIdx = sa[0] & 0xffff;
470:                } else if (pixel instanceof  int[]) {
471:                    int ia[] = (int[]) pixel;
472:                    pixIdx = ia[0];
473:                } else {
474:                    // awt.219=This transferType is not supported by this color model
475:                    throw new UnsupportedOperationException(Messages
476:                            .getString("awt.219")); //$NON-NLS-1$
477:                }
478:
479:                return getComponents(pixIdx, components, offset);
480:            }
481:
482:            @Override
483:            public WritableRaster createCompatibleWritableRaster(int w, int h) {
484:                WritableRaster raster;
485:                if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) {
486:                    raster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, w,
487:                            h, 1, pixel_bits, null);
488:                } else if (pixel_bits <= 8) {
489:                    raster = Raster.createInterleavedRaster(
490:                            DataBuffer.TYPE_BYTE, w, h, 1, null);
491:                } else if (pixel_bits <= 16) {
492:                    raster = Raster.createInterleavedRaster(
493:                            DataBuffer.TYPE_USHORT, w, h, 1, null);
494:                } else {
495:                    // awt.266=The number of bits in a pixel is greater than 16
496:                    throw new UnsupportedOperationException(Messages
497:                            .getString("awt.266")); //$NON-NLS-1$
498:                }
499:
500:                return raster;
501:            }
502:
503:            @Override
504:            public boolean isCompatibleSampleModel(SampleModel sm) {
505:                if (sm == null) {
506:                    return false;
507:                }
508:
509:                if (!(sm instanceof  MultiPixelPackedSampleModel)
510:                        && !(sm instanceof  ComponentSampleModel)) {
511:                    return false;
512:                }
513:
514:                if (sm.getTransferType() != transferType) {
515:                    return false;
516:                }
517:                if (sm.getNumBands() != 1) {
518:                    return false;
519:                }
520:
521:                return true;
522:            }
523:
524:            @Override
525:            public SampleModel createCompatibleSampleModel(int w, int h) {
526:                if (pixel_bits == 1 || pixel_bits == 2 || pixel_bits == 4) {
527:                    return new MultiPixelPackedSampleModel(
528:                            DataBuffer.TYPE_BYTE, w, h, pixel_bits);
529:                }
530:                int bandOffsets[] = new int[1];
531:                bandOffsets[0] = 0;
532:                return new ComponentSampleModel(transferType, w, h, 1, w,
533:                        bandOffsets);
534:
535:            }
536:
537:            @Override
538:            public boolean isCompatibleRaster(Raster raster) {
539:                int sampleSize = raster.getSampleModel().getSampleSize(0);
540:                return (raster.getTransferType() == transferType
541:                        && raster.getNumBands() == 1 && (1 << sampleSize) >= mapSize);
542:            }
543:
544:            @Override
545:            public int getDataElement(int components[], int offset) {
546:                int rgb = (components[offset] << 16)
547:                        | (components[offset + 1]) << 8
548:                        | components[offset + 2];
549:
550:                if (hasAlpha) {
551:                    rgb |= components[offset + 3] << 24;
552:                } else {
553:                    rgb |= 0xff000000;
554:                }
555:
556:                int pixel;
557:
558:                switch (transferType) {
559:                case DataBuffer.TYPE_BYTE:
560:                    byte ba[] = (byte[]) getDataElements(rgb, null);
561:                    pixel = ba[0] & 0xff;
562:                    break;
563:                case DataBuffer.TYPE_USHORT:
564:                    short sa[] = (short[]) getDataElements(rgb, null);
565:                    pixel = sa[0] & 0xffff;
566:                    break;
567:                default:
568:                    // awt.267=The transferType is invalid
569:                    throw new UnsupportedOperationException(Messages
570:                            .getString("awt.267")); //$NON-NLS-1$
571:                }
572:
573:                return pixel;
574:            }
575:
576:            public final void getRGBs(int rgb[]) {
577:                System.arraycopy(colorMap, 0, rgb, 0, mapSize);
578:            }
579:
580:            public final void getReds(byte r[]) {
581:                for (int i = 0; i < mapSize; i++) {
582:                    r[i] = (byte) (colorMap[i] >> 16);
583:                }
584:            }
585:
586:            public final void getGreens(byte g[]) {
587:                for (int i = 0; i < mapSize; i++) {
588:                    g[i] = (byte) (colorMap[i] >> 8);
589:                }
590:            }
591:
592:            public final void getBlues(byte b[]) {
593:                for (int i = 0; i < mapSize; i++) {
594:                    b[i] = (byte) colorMap[i];
595:                }
596:            }
597:
598:            public final void getAlphas(byte a[]) {
599:                for (int i = 0; i < mapSize; i++) {
600:                    a[i] = (byte) (colorMap[i] >> 24);
601:                }
602:            }
603:
604:            @Override
605:            public int[] getComponents(int pixel, int components[], int offset) {
606:                if (components == null) {
607:                    components = new int[offset + numComponents];
608:                }
609:
610:                components[offset + 0] = getRed(pixel);
611:                components[offset + 1] = getGreen(pixel);
612:                components[offset + 2] = getBlue(pixel);
613:                if (hasAlpha && (components.length - offset) > 3) {
614:                    components[offset + 3] = getAlpha(pixel);
615:                }
616:
617:                return components;
618:            }
619:
620:            public boolean isValid(int pixel) {
621:                if (validBits == null) {
622:                    return (pixel >= 0 && pixel < mapSize);
623:                }
624:                return (pixel < mapSize && validBits.testBit(pixel));
625:            }
626:
627:            @Override
628:            public final int getRed(int pixel) {
629:                return (colorMap[pixel] >> 16) & 0xff;
630:            }
631:
632:            @Override
633:            public final int getRGB(int pixel) {
634:                return colorMap[pixel];
635:            }
636:
637:            @Override
638:            public final int getGreen(int pixel) {
639:                return (colorMap[pixel] >> 8) & 0xff;
640:            }
641:
642:            @Override
643:            public final int getBlue(int pixel) {
644:                return colorMap[pixel] & 0xff;
645:            }
646:
647:            @Override
648:            public final int getAlpha(int pixel) {
649:                return (colorMap[pixel] >> 24) & 0xff;
650:            }
651:
652:            @Override
653:            public int[] getComponentSize() {
654:                return bits.clone();
655:            }
656:
657:            public boolean isValid() {
658:                return (validBits == null);
659:            }
660:
661:            @Override
662:            public void finalize() {
663:                // TODO: implement
664:                return;
665:            }
666:
667:            public final int getTransparentPixel() {
668:                return transparentIndex;
669:            }
670:
671:            @Override
672:            public int getTransparency() {
673:                return transparency;
674:            }
675:
676:            public final int getMapSize() {
677:                return mapSize;
678:            }
679:
680:            private void createColorMap(int size, byte r[], byte g[], byte b[],
681:                    byte a[], int trans) {
682:                if (size < 1) {
683:                    // awt.264=Size of the color map is less than 1
684:                    throw new IllegalArgumentException(Messages
685:                            .getString("awt.264")); //$NON-NLS-1$
686:                }
687:
688:                mapSize = size;
689:                colorMap = new int[mapSize];
690:                if (trans >= 0 && trans < mapSize) {
691:                    transparency = Transparency.BITMASK;
692:                    transparentIndex = trans;
693:                } else {
694:                    transparency = Transparency.OPAQUE;
695:                    transparentIndex = -1;
696:                }
697:                int alpha = 0;
698:
699:                for (int i = 0; i < mapSize; i++) {
700:                    colorMap[i] = ((r[i] & 0xff) << 16) | ((g[i] & 0xff) << 8)
701:                            | (b[i] & 0xff);
702:
703:                    if (trans == i) {
704:                        continue;
705:                    }
706:
707:                    if (a == null) {
708:                        colorMap[i] |= 0xff000000;
709:                    } else {
710:                        alpha = a[i] & 0xff;
711:                        if (alpha == 0xff) {
712:                            colorMap[i] |= 0xff000000;
713:                        } else if (alpha == 0) {
714:                            if (transparency == Transparency.OPAQUE) {
715:                                transparency = Transparency.BITMASK;
716:                            }
717:                            if (transparentIndex < 0) {
718:                                transparentIndex = i;
719:                            }
720:                        } else {
721:                            colorMap[i] |= (a[i] & 0xff) << 24;
722:                            if (transparency != Transparency.TRANSLUCENT) {
723:                                transparency = Transparency.TRANSLUCENT;
724:                            }
725:                        }
726:                    }
727:
728:                }
729:
730:            }
731:
732:            /**
733:             * This method checking, if Color Map has Gray Palette
734:             */
735:            private void checkPalette() {
736:                grayPalette = false;
737:                if (transparency > Transparency.OPAQUE) {
738:                    return;
739:                }
740:                int rgb = 0;
741:
742:                for (int i = 0; i < mapSize; i++) {
743:                    rgb = colorMap[i];
744:                    if (((rgb >> 16) & 0xff) != ((rgb >> 8) & 0xff)
745:                            || ((rgb >> 8) & 0xff) != (rgb & 0xff)) {
746:                        return;
747:                    }
748:                }
749:                grayPalette = true;
750:            }
751:
752:            /**
753:             * Construction an array pixel representation
754:             * @param colorMapIdx - index into Color Map
755:             * @param pixel - pixel
756:             * @return - an array pixel representation
757:             */
758:            private Object createDataObject(int colorMapIdx, Object pixel) {
759:                if (pixel == null) {
760:                    switch (transferType) {
761:                    case DataBuffer.TYPE_BYTE:
762:                        byte[] ba = new byte[1];
763:                        ba[0] = (byte) colorMapIdx;
764:                        pixel = ba;
765:                        break;
766:                    case DataBuffer.TYPE_USHORT:
767:                        short[] sa = new short[1];
768:                        sa[0] = (short) colorMapIdx;
769:                        pixel = sa;
770:                        break;
771:                    default:
772:                        // awt.267=The transferType is invalid
773:                        throw new UnsupportedOperationException(Messages
774:                                .getString("awt.267")); //$NON-NLS-1$
775:                    }
776:                } else if (pixel instanceof  byte[]
777:                        && transferType == DataBuffer.TYPE_BYTE) {
778:                    byte ba[] = (byte[]) pixel;
779:                    ba[0] = (byte) colorMapIdx;
780:                    pixel = ba;
781:                } else if (pixel instanceof  short[]
782:                        && transferType == DataBuffer.TYPE_USHORT) {
783:                    short[] sa = (short[]) pixel;
784:                    sa[0] = (short) colorMapIdx;
785:                    pixel = sa;
786:                } else if (pixel instanceof  int[]) {
787:                    int ia[] = (int[]) pixel;
788:                    ia[0] = colorMapIdx;
789:                    pixel = ia;
790:                } else {
791:                    // awt.268=The pixel is not a primitive array of type transferType
792:                    throw new ClassCastException(Messages.getString("awt.268")); //$NON-NLS-1$
793:                }
794:                return pixel;
795:            }
796:
797:            private static int[] createBits(boolean hasAlpha) {
798:
799:                int numChannels;
800:                if (hasAlpha) {
801:                    numChannels = 4;
802:                } else {
803:                    numChannels = 3;
804:                }
805:
806:                int bits[] = new int[numChannels];
807:                for (int i = 0; i < numChannels; i++) {
808:                    bits[i] = 8;
809:                }
810:
811:                return bits;
812:
813:            }
814:
815:            private static int validateTransferType(int transferType) {
816:                if (transferType != DataBuffer.TYPE_BYTE
817:                        && transferType != DataBuffer.TYPE_USHORT) {
818:                    // awt.269=The transferType is not one of DataBuffer.TYPE_BYTE or DataBuffer.TYPE_USHORT
819:                    throw new IllegalArgumentException(Messages
820:                            .getString("awt.269")); //$NON-NLS-1$
821:                }
822:                return transferType;
823:            }
824:
825:            boolean isGrayPallete() {
826:                return grayPalette;
827:            }
828:
829:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.