1 7 8 package com.sun.imageio.plugins.jpeg; 9 10 import javax.imageio.metadata.IIOMetadataFormatImpl ; 11 import javax.imageio.ImageTypeSpecifier ; 12 import javax.imageio.plugins.jpeg.JPEGQTable ; 13 import javax.imageio.plugins.jpeg.JPEGHuffmanTable ; 14 15 import java.awt.image.ColorModel ; 16 import java.awt.image.BufferedImage ; 17 import java.awt.image.DataBuffer ; 18 import java.awt.color.ColorSpace ; 19 import java.awt.color.ICC_ColorSpace ; 20 21 26 public class JPEG { 27 28 30 31 public static final int TEM = 0x01; 32 33 35 37 public static final int SOF0 = 0xC0; 38 39 public static final int SOF1 = 0xC1; 40 41 public static final int SOF2 = 0xC2; 42 43 public static final int SOF3 = 0xC3; 44 45 46 public static final int DHT = 0xC4; 47 48 50 public static final int SOF5 = 0xC5; 51 52 public static final int SOF6 = 0xC6; 53 54 public static final int SOF7 = 0xC7; 55 56 57 public static final int JPG = 0xC8; 58 59 61 public static final int SOF9 = 0xC9; 62 63 public static final int SOF10 = 0xCA; 64 65 public static final int SOF11 = 0xCB; 66 67 68 public static final int DAC = 0xCC; 69 70 72 public static final int SOF13 = 0xCD; 73 74 public static final int SOF14 = 0xCE; 75 76 public static final int SOF15 = 0xCF; 77 78 public static final int RST0 = 0xD0; 80 public static final int RST1 = 0xD1; 81 public static final int RST2 = 0xD2; 82 public static final int RST3 = 0xD3; 83 public static final int RST4 = 0xD4; 84 public static final int RST5 = 0xD5; 85 public static final int RST6 = 0xD6; 86 public static final int RST7 = 0xD7; 87 88 public static final int RESTART_RANGE = 8; 89 90 91 public static final int SOI = 0xD8; 92 93 public static final int EOI = 0xD9; 94 95 public static final int SOS = 0xDA; 96 97 98 public static final int DQT = 0xDB; 99 100 101 public static final int DNL = 0xDC; 102 103 104 public static final int DRI = 0xDD; 105 106 107 public static final int DHP = 0xDE; 108 109 110 public static final int EXP = 0xDF; 111 112 114 public static final int APP0 = 0xE0; 115 public static final int APP1 = 0xE1; 116 public static final int APP2 = 0xE2; 117 public static final int APP3 = 0xE3; 118 public static final int APP4 = 0xE4; 119 public static final int APP5 = 0xE5; 120 public static final int APP6 = 0xE6; 121 public static final int APP7 = 0xE7; 122 public static final int APP8 = 0xE8; 123 public static final int APP9 = 0xE9; 124 public static final int APP10 = 0xEA; 125 public static final int APP11 = 0xEB; 126 public static final int APP12 = 0xEC; 127 public static final int APP13 = 0xED; 128 129 public static final int APP14 = 0xEE; 130 public static final int APP15 = 0xEF; 131 132 134 135 public static final int COM = 0xFE; 136 137 139 public static final int DENSITY_UNIT_ASPECT_RATIO = 0; 140 141 public static final int DENSITY_UNIT_DOTS_INCH = 1; 142 143 public static final int DENSITY_UNIT_DOTS_CM = 2; 144 145 public static final int NUM_DENSITY_UNIT = 3; 146 147 public static final int ADOBE_IMPOSSIBLE = -1; 149 public static final int ADOBE_UNKNOWN = 0; 150 public static final int ADOBE_YCC = 1; 151 public static final int ADOBE_YCCK = 2; 152 153 public static final String vendor = "Sun Microsystems, Inc."; 155 public static final String version = "0.5"; 156 public static final String [] names = {"JPEG", "jpeg", "JPG", "jpg"}; 158 public static final String [] suffixes = {"jpg", "jpeg"}; 159 public static final String [] MIMETypes = {"image/jpeg"}; 160 public static final String nativeImageMetadataFormatName = 161 "javax_imageio_jpeg_image_1.0"; 162 public static final String nativeImageMetadataFormatClassName = 163 "com.sun.imageio.plugins.jpeg.JPEGImageMetadataFormat"; 164 public static final String nativeStreamMetadataFormatName = 165 "javax_imageio_jpeg_stream_1.0"; 166 public static final String nativeStreamMetadataFormatClassName = 167 "com.sun.imageio.plugins.jpeg.JPEGStreamMetadataFormat"; 168 169 public static final int JCS_UNKNOWN = 0; public static final int JCS_GRAYSCALE = 1; public static final int JCS_RGB = 2; public static final int JCS_YCbCr = 3; public static final int JCS_CMYK = 4; public static final int JCS_YCC = 5; public static final int JCS_RGBA = 6; public static final int JCS_YCbCrA = 7; public static final int JCS_YCCA = 10; public static final int JCS_YCCK = 11; 183 public static final int NUM_JCS_CODES = JCS_YCCK+1; 184 185 186 public static final int [] [] bandOffsets = {{0}, 187 {0, 1}, 188 {0, 1, 2}, 189 {0, 1, 2, 3}}; 190 191 public static final int [] bOffsRGB = { 2, 1, 0 }; 192 193 protected static final ColorSpace sRGB = 194 ColorSpace.getInstance(ColorSpace.CS_sRGB); 195 protected static ColorSpace YCC = null; 197 static { 198 try { 199 YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC); 200 } catch (IllegalArgumentException e) { 201 } 203 } 204 205 public static final float DEFAULT_QUALITY = 0.75F; 207 208 214 static boolean isNonStandardICC(ColorSpace cs) { 215 boolean retval = false; 216 if ((cs instanceof ICC_ColorSpace ) 217 && (!cs.isCS_sRGB()) 218 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ))) 219 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY))) 220 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB))) 221 && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC))) 222 ) { 223 retval = true; 224 } 225 return retval; 226 } 227 228 229 234 static boolean isJFIFcompliant(ImageTypeSpecifier imageType, 235 boolean input) { 236 ColorModel cm = imageType.getColorModel(); 237 if (cm.hasAlpha()) { 239 return false; 240 } 241 int numComponents = imageType.getNumComponents(); 243 if (numComponents == 1) { 244 return true; 245 } 246 247 if (numComponents != 3) { 249 return false; 250 } 251 252 if (input) { 253 if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) { 255 return true; 256 } 257 } else { 258 if (cm.getColorSpace().getType() == ColorSpace.TYPE_YCbCr) { 260 return true; 261 } 262 } 263 264 return false; 265 } 266 267 273 static int transformForType(ImageTypeSpecifier imageType, boolean input) { 274 int retval = ADOBE_IMPOSSIBLE; 275 ColorModel cm = imageType.getColorModel(); 276 switch (cm.getColorSpace().getType()) { 277 case ColorSpace.TYPE_GRAY: 278 retval = ADOBE_UNKNOWN; 279 break; 280 case ColorSpace.TYPE_RGB: 281 retval = input ? ADOBE_YCC : ADOBE_UNKNOWN; 282 break; 283 case ColorSpace.TYPE_YCbCr: 284 retval = ADOBE_YCC; 285 break; 286 case ColorSpace.TYPE_CMYK: 287 retval = input ? ADOBE_YCCK : ADOBE_IMPOSSIBLE; 288 } 289 return retval; 290 } 291 292 296 static float convertToLinearQuality(float quality) { 297 if (quality <= 0.0F) { 299 quality = 0.01F; 300 } 301 302 if (quality > 1.00F) { 303 quality = 1.00F; 304 } 305 306 if (quality < 0.5F) { 307 quality = 0.5F / quality; 308 } else { 309 quality = 2.0F - (quality * 2.0F); 310 } 311 312 return quality; 313 } 314 315 318 static JPEGQTable [] getDefaultQTables() { 319 JPEGQTable [] qTables = new JPEGQTable [2]; 320 qTables[0] = JPEGQTable.K1Div2Luminance; 321 qTables[1] = JPEGQTable.K2Div2Chrominance; 322 return qTables; 323 } 324 325 328 static JPEGHuffmanTable [] getDefaultHuffmanTables(boolean wantDC) { 329 JPEGHuffmanTable [] tables = new JPEGHuffmanTable [2]; 330 if (wantDC) { 331 tables[0] = JPEGHuffmanTable.StdDCLuminance; 332 tables[1] = JPEGHuffmanTable.StdDCChrominance; 333 } else { 334 tables[0] = JPEGHuffmanTable.StdACLuminance; 335 tables[1] = JPEGHuffmanTable.StdACChrominance; 336 } 337 return tables; 338 } 339 340 } 341 | Popular Tags |