001: /*
002: * $RCSfile: BMPCodec.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:55:35 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.codecimpl;
013:
014: import java.awt.image.DataBuffer;
015: import java.awt.image.PixelInterleavedSampleModel;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.SampleModel;
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.InputStream;
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import com.sun.media.jai.codec.ImageCodec;
024: import com.sun.media.jai.codec.ImageDecoder;
025: import com.sun.media.jai.codec.ImageDecodeParam;
026: import com.sun.media.jai.codec.ImageEncoder;
027: import com.sun.media.jai.codec.ImageEncodeParam;
028: import com.sun.media.jai.codec.BMPEncodeParam;
029: import com.sun.media.jai.codec.SeekableStream;
030:
031: /**
032: * @since EA2
033: */
034: public final class BMPCodec extends ImageCodec {
035:
036: public BMPCodec() {
037: }
038:
039: public String getFormatName() {
040: return "bmp";
041: }
042:
043: public Class getEncodeParamClass() {
044: return com.sun.media.jai.codec.BMPEncodeParam.class;
045: }
046:
047: public Class getDecodeParamClass() {
048: return Object.class;
049: }
050:
051: public boolean canEncodeImage(RenderedImage im,
052: ImageEncodeParam param) {
053: SampleModel sampleModel = im.getSampleModel();
054: int dataType = sampleModel.getTransferType();
055: if (dataType != DataBuffer.TYPE_BYTE
056: && !CodecUtils.isPackedByteImage(im)) {
057: return false;
058: }
059:
060: if (param != null) {
061: if (!(param instanceof BMPEncodeParam)) {
062: return false;
063: }
064: BMPEncodeParam BMPParam = (BMPEncodeParam) param;
065:
066: int version = BMPParam.getVersion();
067: if ((version == BMPEncodeParam.VERSION_2)
068: || (version == BMPEncodeParam.VERSION_4)) {
069: return false;
070: }
071: }
072:
073: return true;
074: }
075:
076: protected ImageEncoder createImageEncoder(OutputStream dst,
077: ImageEncodeParam param) {
078: BMPEncodeParam p = null;
079: if (param != null) {
080: p = (BMPEncodeParam) param;
081: }
082:
083: return new BMPImageEncoder(dst, p);
084: }
085:
086: protected ImageDecoder createImageDecoder(InputStream src,
087: ImageDecodeParam param) {
088: return new BMPImageDecoder(src, null);
089: }
090:
091: protected ImageDecoder createImageDecoder(File src,
092: ImageDecodeParam param) throws IOException {
093: return new BMPImageDecoder(new FileInputStream(src), null);
094: }
095:
096: protected ImageDecoder createImageDecoder(SeekableStream src,
097: ImageDecodeParam param) {
098: return new BMPImageDecoder(src, null);
099: }
100:
101: public int getNumHeaderBytes() {
102: return 2;
103: }
104:
105: public boolean isFormatRecognized(byte[] header) {
106: return ((header[0] == 0x42) && (header[1] == 0x4d));
107: }
108: }
|