KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > jpeg > JPEGImageWriterSpi


1 /*
2  * @(#)JPEGImageWriterSpi.java 1.10 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.jpeg;
9
10 import javax.imageio.spi.ImageWriterSpi JavaDoc;
11 import javax.imageio.spi.ServiceRegistry JavaDoc;
12 import javax.imageio.spi.IIORegistry JavaDoc;
13 import javax.imageio.ImageWriter JavaDoc;
14 import javax.imageio.ImageTypeSpecifier JavaDoc;
15 import javax.imageio.IIOException JavaDoc;
16
17 import java.awt.image.ColorModel JavaDoc;
18 import java.awt.image.IndexColorModel JavaDoc;
19 import java.awt.image.SampleModel JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 public class JPEGImageWriterSpi extends ImageWriterSpi JavaDoc {
23
24     private static String JavaDoc [] readerSpiNames =
25         {"com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi"};
26
27     private boolean registered = false;
28     
29     public JPEGImageWriterSpi() {
30         super(JPEG.vendor,
31               JPEG.version,
32               JPEG.names,
33               JPEG.suffixes,
34               JPEG.MIMETypes,
35               "com.sun.imageio.plugins.jpeg.JPEGImageWriter",
36               STANDARD_OUTPUT_TYPE,
37               readerSpiNames,
38               true,
39               JPEG.nativeStreamMetadataFormatName,
40               JPEG.nativeStreamMetadataFormatClassName,
41               null, null,
42               true,
43               JPEG.nativeImageMetadataFormatName,
44               JPEG.nativeImageMetadataFormatClassName,
45               null, null
46               );
47     }
48
49     public String JavaDoc getDescription(Locale JavaDoc locale) {
50         return "Standard JPEG Image Writer";
51     }
52
53     public void onRegistration(ServiceRegistry JavaDoc registry,
54                                Class JavaDoc<?> category) {
55         if (registered) {
56             return;
57         }
58         try {
59             java.security.AccessController.doPrivileged(
60                 new sun.security.action.LoadLibraryAction("jpeg"));
61         } catch (Throwable JavaDoc e) { // Fail on any Throwable
62
// if it can't be loaded, deregister and return
63
registry.deregisterServiceProvider(this);
64             return;
65         }
66
67         registered = true;
68     }
69
70     public boolean isFormatLossless() {
71         return false;
72     }
73
74     public boolean canEncodeImage(ImageTypeSpecifier JavaDoc type) {
75         SampleModel JavaDoc sampleModel = type.getSampleModel();
76
77         // Find the maximum bit depth across all channels
78
int[] sampleSize = sampleModel.getSampleSize();
79         int bitDepth = sampleSize[0];
80         for (int i = 1; i < sampleSize.length; i++) {
81             if (sampleSize[i] > bitDepth) {
82                 bitDepth = sampleSize[i];
83             }
84         }
85
86         // 4450894: Ensure bitDepth is between 1 and 8
87
if (bitDepth < 1 || bitDepth > 8) {
88             return false;
89         }
90
91         return true;
92     }
93
94     public ImageWriter JavaDoc createWriterInstance(Object JavaDoc extension)
95         throws IIOException JavaDoc {
96         return new JPEGImageWriter(this);
97     }
98 }
99
Popular Tags