001: /*
002: Copyright © 2006,2007 Stefano Chizzolini. http://clown.stefanochizzolini.it
003:
004: Contributors:
005: * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it):
006: contributed code is Copyright © 2006,2007 by Stefano Chizzolini.
007:
008: This file should be part of the source code distribution of "PDF Clown library"
009: (the Program): see the accompanying README files for more info.
010:
011: This Program is free software; you can redistribute it and/or modify it under
012: the terms of the GNU General Public License as published by the Free Software
013: Foundation; either version 2 of the License, or (at your option) any later version.
014:
015: This Program is distributed in the hope that it will be useful, but WITHOUT ANY
016: WARRANTY, either expressed or implied; without even the implied warranty of
017: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
018:
019: You should have received a copy of the GNU General Public License along with this
020: Program (see README files); if not, go to the GNU website (http://www.gnu.org/).
021:
022: Redistribution and use, with or without modification, are permitted provided that such
023: redistributions retain the above copyright notice, license and disclaimer, along with
024: this list of conditions.
025: */
026:
027: package it.stefanochizzolini.clown.documents.contents.entities;
028:
029: import it.stefanochizzolini.clown.bytes.FileInputStream;
030: import it.stefanochizzolini.clown.bytes.IInputStream;
031: import it.stefanochizzolini.clown.documents.contents.objects.ContentObject;
032:
033: /**
034: Abstract image object [PDF:1.6:4.8].
035: @version 0.0.5
036: */
037: public abstract class Image extends Entity {
038: // <class>
039: // <static>
040: // <interface>
041: // <public>
042: public static Image get(String path) {
043: try {
044: return get(new FileInputStream(
045: new java.io.RandomAccessFile(path, "r")));
046: } catch (Exception e) {
047: throw new RuntimeException(e);
048: }
049: }
050:
051: public static Image get(java.io.File file) {
052: return get(file.getPath());
053: }
054:
055: public static Image get(IInputStream stream) {
056: try {
057: // Get the format identifier!
058: byte[] formatMarkerBytes = new byte[2];
059: stream.read(formatMarkerBytes);
060:
061: // Is JPEG?
062: if (formatMarkerBytes[0] == (byte) 0xFF
063: && formatMarkerBytes[1] == (byte) 0xD8) // JPEG.
064: /*
065: NOTE: JPEG files are identified by a SOI (Start Of Image) marker [ISO 10918-1].
066: */
067: {
068: return new JpegImage(stream);
069: } else // Unknown.
070: {
071: return null;
072: }
073: } catch (Exception e) {
074: throw new RuntimeException(e);
075: }
076: }
077:
078: // </public>
079: // </interface>
080: // </static>
081:
082: // <dynamic>
083: // <fields>
084: private int bitsPerComponent;
085: private int height;
086: private int width;
087:
088: private IInputStream stream;
089:
090: // </fields>
091:
092: // <constructors>
093: protected Image(IInputStream stream) {
094: this .stream = stream;
095: }
096:
097: // </constructors>
098:
099: // <interface>
100: // <public>
101: /**
102: Gets the number of bits per color component [PDF:1.6:4.8.2].
103: */
104: public int getBitsPerComponent() {
105: return bitsPerComponent;
106: }
107:
108: /**
109: Gets the height of the image in samples [PDF:1.6:4.8.2].
110: */
111: public int getHeight() {
112: return height;
113: }
114:
115: /**
116: Gets the width of the image in samples [PDF:1.6:4.8.2].
117: */
118: public int getWidth() {
119: return width;
120: }
121:
122: /**
123: @version 0.0.5
124: */
125: /*
126: NOTE: Image entities are tipically self-contained, so we don't need to contextualize them.
127: */
128: public ContentObject toInlineObject() {
129: return toInlineObject(null);
130: }
131:
132: // </public>
133:
134: // <protected>
135: /**
136: Gets the underlying stream.
137: */
138: protected IInputStream getStream() {
139: return stream;
140: }
141:
142: /**
143: Sets the number of bits per color component [PDF:1.6:4.8.2].
144: */
145: protected void setBitsPerComponent(int value) {
146: bitsPerComponent = value;
147: }
148:
149: /**
150: Sets the height of the image in samples [PDF:1.6:4.8.2].
151: */
152: protected void setHeight(int value) {
153: height = value;
154: }
155:
156: /**
157: Sets the width of the image in samples [PDF:1.6:4.8.2].
158: */
159: protected void setWidth(int value) {
160: width = value;
161: }
162: // </protected>
163: // </interface>
164: // </dynamic>
165: // </class>
166: }
|