001: /**
002: * Image.java
003: *
004: Copyright (c) 2007, Innovatics Inc.
005:
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
009:
010: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
011: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
012:
013: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
017: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
018: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
019: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
020: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
021: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
022: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
023: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: */package com.pdfjet;
025:
026: import java.lang.*;
027: import java.io.*;
028: import java.util.*;
029: import java.util.zip.*;
030:
031: import java.awt.image.PixelGrabber;
032: import javax.swing.ImageIcon;
033:
034: //>>>>pdfjet {
035: public class Image {
036:
037: protected String name = null;
038: protected int objNumber = 0;
039:
040: protected double x = 0.0; // Position of the image on the page
041: protected double y = 0.0;
042: protected double w = 0.0; // Image width
043: protected double h = 0.0; // Image height
044:
045: private double box_x = 0.0;
046: private double box_y = 0.0;
047:
048: private boolean jpgImage = false;
049: private ByteArrayOutputStream data = null;
050: private long dataSize = 0;
051:
052: public Image(PDF pdf, String fileName) throws Exception {
053: name = fileName;
054: data = new ByteArrayOutputStream(32768);
055: try {
056: if (!(new File(fileName).exists()))
057: throw new Exception();
058: if (fileName.toLowerCase().endsWith(".jpg")
059: || fileName.toLowerCase().endsWith(".jpeg")) {
060: JPGImage jpg = new JPGImage(fileName);
061: if (jpg.colors == 3) {
062: w = jpg.width;
063: h = jpg.height;
064:
065: dataSize = (new File(fileName)).length();
066: byte[] buf = new byte[(int) dataSize];
067: FileInputStream fis = new FileInputStream(fileName);
068: BufferedInputStream bis = new BufferedInputStream(
069: fis);
070: bis.read(buf, 0, (int) dataSize);
071: bis.close();
072: data.write(buf, 0, (int) dataSize);
073: /*
074: FileInputStream fis = new FileInputStream(fileName);
075: BufferedInputStream bis = new BufferedInputStream(fis);
076: int ch;
077: while ((ch = bis.read()) != -1) {
078: data.write((byte) ch);
079: }
080: bis.close();
081: */
082: jpgImage = true;
083: } else {
084: // Let the ImageIcon class deal with black and white images
085: extractAndCompressImage(fileName);
086: }
087: } else {
088: extractAndCompressImage(fileName);
089: }
090: } catch (Exception e) {
091: w = 32;
092: h = 32;
093: data.write(Missing.image, 0, Missing.image.length);
094: throw e;
095: }
096:
097: addThisImageTo(pdf, fileName, dataSize);
098: }
099:
100: public void setPosition(double x, double y) {
101: this .x = x;
102: this .y = y;
103: }
104:
105: public void scaleBy(double factor) {
106: this .w *= factor;
107: this .h *= factor;
108: }
109:
110: public void placeIn(Box box) throws Exception {
111: box_x = box.x;
112: box_y = box.y;
113: }
114:
115: public void drawOn(Page page) throws Exception {
116: x += box_x;
117: y += box_y;
118: page.pdf.append(page.buf, "q\n");
119: page.pdf.append(page.buf, w);
120: page.pdf.append(page.buf, " 0 0 ");
121: page.pdf.append(page.buf, h);
122: page.pdf.append(page.buf, ' ');
123: page.pdf.append(page.buf, x);
124: page.pdf.append(page.buf, ' ');
125: page.pdf.append(page.buf, (page.height - y) - h);
126: page.pdf.append(page.buf, " cm\n");
127: page.pdf.append(page.buf, "/Im");
128: page.pdf.append(page.buf, objNumber);
129: page.pdf.append(page.buf, " Do\n");
130: page.pdf.append(page.buf, "Q\n");
131: }
132:
133: private void extractAndCompressImage(String fileName)
134: throws Exception {
135: ImageIcon imageIcon = new ImageIcon(fileName);
136: w = imageIcon.getIconWidth();
137: h = imageIcon.getIconHeight();
138:
139: int[] pixels = new int[(int) (w * h)];
140: PixelGrabber pg = new PixelGrabber(imageIcon.getImage(), 0, 0,
141: (int) w, (int) h, pixels, 0, (int) w);
142: pg.grabPixels();
143:
144: byte[] buf = new byte[(int) (w * h * 3)];
145: int j = 0;
146: int i = 0;
147: for (int y = 0; y < h; y++) {
148: for (int x = 0; x < w; x++) {
149: int pixel = pixels[i + x];
150: buf[j++] = (byte) (pixel >> 16);
151: buf[j++] = (byte) (pixel >> 8);
152: buf[j++] = (byte) (pixel);
153: }
154: i += (int) w;
155: }
156:
157: DeflaterOutputStream dos = new DeflaterOutputStream(data,
158: new Deflater());
159: dos.write(buf, 0, buf.length);
160: dos.finish();
161: dataSize = (long) data.size();
162: }
163:
164: private void addThisImageTo(PDF pdf, String fileName, long dataSize)
165: throws Exception {
166: // Add the image
167: pdf.newobj();
168: pdf.append(pdf.buf, "<<\n");
169: pdf.append(pdf.buf, "/Type /XObject\n");
170: pdf.append(pdf.buf, "/Subtype /Image\n");
171: if (jpgImage) {
172: pdf.append(pdf.buf, "/Filter /DCTDecode\n");
173: } else {
174: pdf.append(pdf.buf, "/Filter /FlateDecode\n");
175: }
176: pdf.append(pdf.buf, "/Width ");
177: pdf.append(pdf.buf, w);
178: pdf.append(pdf.buf, '\n');
179: pdf.append(pdf.buf, "/Height ");
180: pdf.append(pdf.buf, h);
181: pdf.append(pdf.buf, '\n');
182: pdf.append(pdf.buf, "/ColorSpace /DeviceRGB\n");
183: pdf.append(pdf.buf, "/BitsPerComponent 8\n");
184: pdf.append(pdf.buf, "/Length ");
185: pdf.append(pdf.buf, (int) dataSize);
186: pdf.append(pdf.buf, '\n');
187: pdf.append(pdf.buf, ">>\n");
188: pdf.append(pdf.buf, "stream\n");
189: pdf.buf.write(data.toByteArray(), 0, (int) dataSize);
190: pdf.append(pdf.buf, "\nendstream\n");
191: pdf.endobj();
192: pdf.images.add(this );
193: objNumber = pdf.objNumber;
194: }
195:
196: } // End of Image.java
197: //>>>>}
|