Initial Commit

This commit is contained in:
2023-06-21 12:46:23 -04:00
commit c70248a520
1352 changed files with 336780 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,601 @@
using System;
using iTextSharp.text.pdf;
/*
* Copyright 2005 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*
* This code is base in the libtiff encoder
*/
namespace iTextSharp.text.pdf.codec {
/**
* Encodes data in the CCITT G4 FAX format.
*/
public class CCITTG4Encoder {
private int rowbytes;
private int rowpixels;
private int bit = 8;
private int data;
private byte[] refline;
private ByteBuffer outBuf = new ByteBuffer(1024);
private byte[] dataBp;
private int offsetData;
private int sizeData;
/**
* Creates a new encoder.
* @param width the line width
*/
public CCITTG4Encoder(int width) {
rowpixels = width;
rowbytes = (rowpixels + 7) / 8;
refline = new byte[rowbytes];
}
/**
* Encodes a number of lines.
* @param data the data to be encoded
* @param offset the offset into the data
* @param size the size of the data to be encoded
*/
public void Fax4Encode(byte[] data, int offset, int size) {
dataBp = data;
offsetData = offset;
sizeData = size;
while (sizeData > 0) {
Fax3Encode2DRow();
Array.Copy(dataBp, offsetData, refline, 0, rowbytes);
offsetData += rowbytes;
sizeData -= rowbytes;
}
}
/**
* Encodes a full image.
* @param data the data to encode
* @param width the image width
* @param height the image height
* @return the encoded image
*/
public static byte[] Compress(byte[] data, int width, int height) {
CCITTG4Encoder g4 = new CCITTG4Encoder(width);
g4.Fax4Encode(data, 0, g4.rowbytes * height);
return g4.Close();
}
/**
* Encodes a number of lines.
* @param data the data to be encoded
* @param height the number of lines to encode
*/
public void Fax4Encode(byte[] data, int height) {
Fax4Encode(data, 0, rowbytes * height);
}
private void Putcode(int[] table) {
PutBits(table[CODE], table[LENGTH]);
}
private void Putspan(int span, int[][] tab) {
int code, length;
while (span >= 2624) {
int[] te = tab[63 + (2560>>6)];
code = te[CODE];
length = te[LENGTH];
PutBits(code, length);
span -= te[RUNLEN];
}
if (span >= 64) {
int[] te = tab[63 + (span>>6)];
code = te[CODE];
length = te[LENGTH];
PutBits(code, length);
span -= te[RUNLEN];
}
code = tab[span][CODE];
length = tab[span][LENGTH];
PutBits(code, length);
}
private void PutBits(int bits, int length) {
while (length > bit) {
data |= bits >> (length - bit);
length -= bit;
outBuf.Append((byte)data);
data = 0;
bit = 8;
}
data |= (bits & msbmask[length]) << (bit - length);
bit -= length;
if (bit == 0) {
outBuf.Append((byte)data);
data = 0;
bit = 8;
}
}
private void Fax3Encode2DRow() {
int a0 = 0;
int a1 = (Pixel(dataBp, offsetData, 0) != 0 ? 0 : Finddiff(dataBp, offsetData, 0, rowpixels, 0));
int b1 = (Pixel(refline, 0, 0) != 0 ? 0 : Finddiff(refline, 0, 0, rowpixels, 0));
int a2, b2;
for (;;) {
b2 = Finddiff2(refline, 0, b1, rowpixels, Pixel(refline, 0,b1));
if (b2 >= a1) {
int d = b1 - a1;
if (!(-3 <= d && d <= 3)) { /* horizontal mode */
a2 = Finddiff2(dataBp, offsetData, a1, rowpixels, Pixel(dataBp, offsetData,a1));
Putcode(horizcode);
if (a0+a1 == 0 || Pixel(dataBp, offsetData, a0) == 0) {
Putspan(a1-a0, TIFFFaxWhiteCodes);
Putspan(a2-a1, TIFFFaxBlackCodes);
} else {
Putspan(a1-a0, TIFFFaxBlackCodes);
Putspan(a2-a1, TIFFFaxWhiteCodes);
}
a0 = a2;
} else { /* vertical mode */
Putcode(vcodes[d+3]);
a0 = a1;
}
} else { /* pass mode */
Putcode(passcode);
a0 = b2;
}
if (a0 >= rowpixels)
break;
a1 = Finddiff(dataBp, offsetData, a0, rowpixels, Pixel(dataBp, offsetData,a0));
b1 = Finddiff(refline, 0, a0, rowpixels, Pixel(dataBp, offsetData,a0) ^ 1);
b1 = Finddiff(refline, 0, b1, rowpixels, Pixel(dataBp, offsetData,a0));
}
}
private void Fax4PostEncode() {
PutBits(EOL, 12);
PutBits(EOL, 12);
if (bit != 8) {
outBuf.Append((byte)data);
data = 0;
bit = 8;
}
}
/**
* Closes the encoder and returns the encoded data.
* @return the encoded data
*/
public byte[] Close() {
Fax4PostEncode();
return outBuf.ToByteArray();
}
private int Pixel(byte[] data, int offset, int bit) {
if (bit >= rowpixels)
return 0;
return ((data[offset + (bit >> 3)] & 0xff) >> (7-((bit)&7))) & 1;
}
private static int Find1span(byte[] bp, int offset, int bs, int be) {
int bits = be - bs;
int n, span;
int pos = offset + (bs >> 3);
/*
* Check partial byte on lhs.
*/
if (bits > 0 && (n = (bs & 7)) != 0) {
span = oneruns[((int)bp[pos] << n) & 0xff];
if (span > 8-n) /* table value too generous */
span = 8-n;
if (span > bits) /* constrain span to bit range */
span = bits;
if (n+span < 8) /* doesn't extend to edge of byte */
return (span);
bits -= span;
pos++;
} else
span = 0;
/*
* Scan full bytes for all 1's.
*/
while (bits >= 8) {
if (bp[pos] != 0xff) /* end of run */
return (span + oneruns[bp[pos] & 0xff]);
span += 8;
bits -= 8;
pos++;
}
/*
* Check partial byte on rhs.
*/
if (bits > 0) {
n = oneruns[bp[pos] & 0xff];
span += (n > bits ? bits : n);
}
return (span);
}
private static int Find0span(byte[] bp, int offset, int bs, int be) {
int bits = be - bs;
int n, span;
int pos = offset + (bs >> 3);
/*
* Check partial byte on lhs.
*/
if (bits > 0 && (n = (bs & 7)) != 0) {
span = zeroruns[((int)bp[pos] << n) & 0xff];
if (span > 8-n) /* table value too generous */
span = 8-n;
if (span > bits) /* constrain span to bit range */
span = bits;
if (n+span < 8) /* doesn't extend to edge of byte */
return (span);
bits -= span;
pos++;
} else
span = 0;
/*
* Scan full bytes for all 1's.
*/
while (bits >= 8) {
if (bp[pos] != 0) /* end of run */
return (span + zeroruns[bp[pos] & 0xff]);
span += 8;
bits -= 8;
pos++;
}
/*
* Check partial byte on rhs.
*/
if (bits > 0) {
n = zeroruns[bp[pos] & 0xff];
span += (n > bits ? bits : n);
}
return (span);
}
private static int Finddiff(byte[] bp, int offset, int bs, int be, int color) {
return bs + (color != 0 ? Find1span(bp, offset, bs, be) : Find0span(bp, offset, bs, be));
}
private static int Finddiff2(byte[] bp, int offset, int bs, int be, int color) {
return bs < be ? Finddiff(bp, offset, bs, be, color) : be;
}
private static byte[] zeroruns = {
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 - 0xff */
};
private static byte[] oneruns = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8 /* 0xf0 - 0xff */
};
private const int LENGTH = 0; /* bit length of g3 code */
private const int CODE = 1; /* g3 code */
private const int RUNLEN = 2; /* run length in bits */
private const int EOL = 0x001; /* EOL code value - 0000 0000 0000 1 */
/* status values returned instead of a run length */
private const int G3CODE_EOL = -1; /* NB: ACT_EOL - ACT_WRUNT */
private const int G3CODE_INVALID = -2; /* NB: ACT_INVALID - ACT_WRUNT */
private const int G3CODE_EOF = -3; /* end of input data */
private const int G3CODE_INCOMP = -4; /* incomplete run code */
private int[][] TIFFFaxWhiteCodes = {
new int[]{ 8, 0x35, 0 }, /* 0011 0101 */
new int[]{ 6, 0x7, 1 }, /* 0001 11 */
new int[]{ 4, 0x7, 2 }, /* 0111 */
new int[]{ 4, 0x8, 3 }, /* 1000 */
new int[]{ 4, 0xB, 4 }, /* 1011 */
new int[]{ 4, 0xC, 5 }, /* 1100 */
new int[]{ 4, 0xE, 6 }, /* 1110 */
new int[]{ 4, 0xF, 7 }, /* 1111 */
new int[]{ 5, 0x13, 8 }, /* 1001 1 */
new int[]{ 5, 0x14, 9 }, /* 1010 0 */
new int[]{ 5, 0x7, 10 }, /* 0011 1 */
new int[]{ 5, 0x8, 11 }, /* 0100 0 */
new int[]{ 6, 0x8, 12 }, /* 0010 00 */
new int[]{ 6, 0x3, 13 }, /* 0000 11 */
new int[]{ 6, 0x34, 14 }, /* 1101 00 */
new int[]{ 6, 0x35, 15 }, /* 1101 01 */
new int[]{ 6, 0x2A, 16 }, /* 1010 10 */
new int[]{ 6, 0x2B, 17 }, /* 1010 11 */
new int[]{ 7, 0x27, 18 }, /* 0100 111 */
new int[]{ 7, 0xC, 19 }, /* 0001 100 */
new int[]{ 7, 0x8, 20 }, /* 0001 000 */
new int[]{ 7, 0x17, 21 }, /* 0010 111 */
new int[]{ 7, 0x3, 22 }, /* 0000 011 */
new int[]{ 7, 0x4, 23 }, /* 0000 100 */
new int[]{ 7, 0x28, 24 }, /* 0101 000 */
new int[]{ 7, 0x2B, 25 }, /* 0101 011 */
new int[]{ 7, 0x13, 26 }, /* 0010 011 */
new int[]{ 7, 0x24, 27 }, /* 0100 100 */
new int[]{ 7, 0x18, 28 }, /* 0011 000 */
new int[]{ 8, 0x2, 29 }, /* 0000 0010 */
new int[]{ 8, 0x3, 30 }, /* 0000 0011 */
new int[]{ 8, 0x1A, 31 }, /* 0001 1010 */
new int[]{ 8, 0x1B, 32 }, /* 0001 1011 */
new int[]{ 8, 0x12, 33 }, /* 0001 0010 */
new int[]{ 8, 0x13, 34 }, /* 0001 0011 */
new int[]{ 8, 0x14, 35 }, /* 0001 0100 */
new int[]{ 8, 0x15, 36 }, /* 0001 0101 */
new int[]{ 8, 0x16, 37 }, /* 0001 0110 */
new int[]{ 8, 0x17, 38 }, /* 0001 0111 */
new int[]{ 8, 0x28, 39 }, /* 0010 1000 */
new int[]{ 8, 0x29, 40 }, /* 0010 1001 */
new int[]{ 8, 0x2A, 41 }, /* 0010 1010 */
new int[]{ 8, 0x2B, 42 }, /* 0010 1011 */
new int[]{ 8, 0x2C, 43 }, /* 0010 1100 */
new int[]{ 8, 0x2D, 44 }, /* 0010 1101 */
new int[]{ 8, 0x4, 45 }, /* 0000 0100 */
new int[]{ 8, 0x5, 46 }, /* 0000 0101 */
new int[]{ 8, 0xA, 47 }, /* 0000 1010 */
new int[]{ 8, 0xB, 48 }, /* 0000 1011 */
new int[]{ 8, 0x52, 49 }, /* 0101 0010 */
new int[]{ 8, 0x53, 50 }, /* 0101 0011 */
new int[]{ 8, 0x54, 51 }, /* 0101 0100 */
new int[]{ 8, 0x55, 52 }, /* 0101 0101 */
new int[]{ 8, 0x24, 53 }, /* 0010 0100 */
new int[]{ 8, 0x25, 54 }, /* 0010 0101 */
new int[]{ 8, 0x58, 55 }, /* 0101 1000 */
new int[]{ 8, 0x59, 56 }, /* 0101 1001 */
new int[]{ 8, 0x5A, 57 }, /* 0101 1010 */
new int[]{ 8, 0x5B, 58 }, /* 0101 1011 */
new int[]{ 8, 0x4A, 59 }, /* 0100 1010 */
new int[]{ 8, 0x4B, 60 }, /* 0100 1011 */
new int[]{ 8, 0x32, 61 }, /* 0011 0010 */
new int[]{ 8, 0x33, 62 }, /* 0011 0011 */
new int[]{ 8, 0x34, 63 }, /* 0011 0100 */
new int[]{ 5, 0x1B, 64 }, /* 1101 1 */
new int[]{ 5, 0x12, 128 }, /* 1001 0 */
new int[]{ 6, 0x17, 192 }, /* 0101 11 */
new int[]{ 7, 0x37, 256 }, /* 0110 111 */
new int[]{ 8, 0x36, 320 }, /* 0011 0110 */
new int[]{ 8, 0x37, 384 }, /* 0011 0111 */
new int[]{ 8, 0x64, 448 }, /* 0110 0100 */
new int[]{ 8, 0x65, 512 }, /* 0110 0101 */
new int[]{ 8, 0x68, 576 }, /* 0110 1000 */
new int[]{ 8, 0x67, 640 }, /* 0110 0111 */
new int[]{ 9, 0xCC, 704 }, /* 0110 0110 0 */
new int[]{ 9, 0xCD, 768 }, /* 0110 0110 1 */
new int[]{ 9, 0xD2, 832 }, /* 0110 1001 0 */
new int[]{ 9, 0xD3, 896 }, /* 0110 1001 1 */
new int[]{ 9, 0xD4, 960 }, /* 0110 1010 0 */
new int[]{ 9, 0xD5, 1024 }, /* 0110 1010 1 */
new int[]{ 9, 0xD6, 1088 }, /* 0110 1011 0 */
new int[]{ 9, 0xD7, 1152 }, /* 0110 1011 1 */
new int[]{ 9, 0xD8, 1216 }, /* 0110 1100 0 */
new int[]{ 9, 0xD9, 1280 }, /* 0110 1100 1 */
new int[]{ 9, 0xDA, 1344 }, /* 0110 1101 0 */
new int[]{ 9, 0xDB, 1408 }, /* 0110 1101 1 */
new int[]{ 9, 0x98, 1472 }, /* 0100 1100 0 */
new int[]{ 9, 0x99, 1536 }, /* 0100 1100 1 */
new int[]{ 9, 0x9A, 1600 }, /* 0100 1101 0 */
new int[]{ 6, 0x18, 1664 }, /* 0110 00 */
new int[]{ 9, 0x9B, 1728 }, /* 0100 1101 1 */
new int[]{ 11, 0x8, 1792 }, /* 0000 0001 000 */
new int[]{ 11, 0xC, 1856 }, /* 0000 0001 100 */
new int[]{ 11, 0xD, 1920 }, /* 0000 0001 101 */
new int[]{ 12, 0x12, 1984 }, /* 0000 0001 0010 */
new int[]{ 12, 0x13, 2048 }, /* 0000 0001 0011 */
new int[]{ 12, 0x14, 2112 }, /* 0000 0001 0100 */
new int[]{ 12, 0x15, 2176 }, /* 0000 0001 0101 */
new int[]{ 12, 0x16, 2240 }, /* 0000 0001 0110 */
new int[]{ 12, 0x17, 2304 }, /* 0000 0001 0111 */
new int[]{ 12, 0x1C, 2368 }, /* 0000 0001 1100 */
new int[]{ 12, 0x1D, 2432 }, /* 0000 0001 1101 */
new int[]{ 12, 0x1E, 2496 }, /* 0000 0001 1110 */
new int[]{ 12, 0x1F, 2560 }, /* 0000 0001 1111 */
new int[]{ 12, 0x1, G3CODE_EOL }, /* 0000 0000 0001 */
new int[]{ 9, 0x1, G3CODE_INVALID }, /* 0000 0000 1 */
new int[]{ 10, 0x1, G3CODE_INVALID }, /* 0000 0000 01 */
new int[]{ 11, 0x1, G3CODE_INVALID }, /* 0000 0000 001 */
new int[]{ 12, 0x0, G3CODE_INVALID } /* 0000 0000 0000 */
};
private int[][] TIFFFaxBlackCodes = {
new int[]{ 10, 0x37, 0 }, /* 0000 1101 11 */
new int[]{ 3, 0x2, 1 }, /* 010 */
new int[]{ 2, 0x3, 2 }, /* 11 */
new int[]{ 2, 0x2, 3 }, /* 10 */
new int[]{ 3, 0x3, 4 }, /* 011 */
new int[]{ 4, 0x3, 5 }, /* 0011 */
new int[]{ 4, 0x2, 6 }, /* 0010 */
new int[]{ 5, 0x3, 7 }, /* 0001 1 */
new int[]{ 6, 0x5, 8 }, /* 0001 01 */
new int[]{ 6, 0x4, 9 }, /* 0001 00 */
new int[]{ 7, 0x4, 10 }, /* 0000 100 */
new int[]{ 7, 0x5, 11 }, /* 0000 101 */
new int[]{ 7, 0x7, 12 }, /* 0000 111 */
new int[]{ 8, 0x4, 13 }, /* 0000 0100 */
new int[]{ 8, 0x7, 14 }, /* 0000 0111 */
new int[]{ 9, 0x18, 15 }, /* 0000 1100 0 */
new int[]{ 10, 0x17, 16 }, /* 0000 0101 11 */
new int[]{ 10, 0x18, 17 }, /* 0000 0110 00 */
new int[]{ 10, 0x8, 18 }, /* 0000 0010 00 */
new int[]{ 11, 0x67, 19 }, /* 0000 1100 111 */
new int[]{ 11, 0x68, 20 }, /* 0000 1101 000 */
new int[]{ 11, 0x6C, 21 }, /* 0000 1101 100 */
new int[]{ 11, 0x37, 22 }, /* 0000 0110 111 */
new int[]{ 11, 0x28, 23 }, /* 0000 0101 000 */
new int[]{ 11, 0x17, 24 }, /* 0000 0010 111 */
new int[]{ 11, 0x18, 25 }, /* 0000 0011 000 */
new int[]{ 12, 0xCA, 26 }, /* 0000 1100 1010 */
new int[]{ 12, 0xCB, 27 }, /* 0000 1100 1011 */
new int[]{ 12, 0xCC, 28 }, /* 0000 1100 1100 */
new int[]{ 12, 0xCD, 29 }, /* 0000 1100 1101 */
new int[]{ 12, 0x68, 30 }, /* 0000 0110 1000 */
new int[]{ 12, 0x69, 31 }, /* 0000 0110 1001 */
new int[]{ 12, 0x6A, 32 }, /* 0000 0110 1010 */
new int[]{ 12, 0x6B, 33 }, /* 0000 0110 1011 */
new int[]{ 12, 0xD2, 34 }, /* 0000 1101 0010 */
new int[]{ 12, 0xD3, 35 }, /* 0000 1101 0011 */
new int[]{ 12, 0xD4, 36 }, /* 0000 1101 0100 */
new int[]{ 12, 0xD5, 37 }, /* 0000 1101 0101 */
new int[]{ 12, 0xD6, 38 }, /* 0000 1101 0110 */
new int[]{ 12, 0xD7, 39 }, /* 0000 1101 0111 */
new int[]{ 12, 0x6C, 40 }, /* 0000 0110 1100 */
new int[]{ 12, 0x6D, 41 }, /* 0000 0110 1101 */
new int[]{ 12, 0xDA, 42 }, /* 0000 1101 1010 */
new int[]{ 12, 0xDB, 43 }, /* 0000 1101 1011 */
new int[]{ 12, 0x54, 44 }, /* 0000 0101 0100 */
new int[]{ 12, 0x55, 45 }, /* 0000 0101 0101 */
new int[]{ 12, 0x56, 46 }, /* 0000 0101 0110 */
new int[]{ 12, 0x57, 47 }, /* 0000 0101 0111 */
new int[]{ 12, 0x64, 48 }, /* 0000 0110 0100 */
new int[]{ 12, 0x65, 49 }, /* 0000 0110 0101 */
new int[]{ 12, 0x52, 50 }, /* 0000 0101 0010 */
new int[]{ 12, 0x53, 51 }, /* 0000 0101 0011 */
new int[]{ 12, 0x24, 52 }, /* 0000 0010 0100 */
new int[]{ 12, 0x37, 53 }, /* 0000 0011 0111 */
new int[]{ 12, 0x38, 54 }, /* 0000 0011 1000 */
new int[]{ 12, 0x27, 55 }, /* 0000 0010 0111 */
new int[]{ 12, 0x28, 56 }, /* 0000 0010 1000 */
new int[]{ 12, 0x58, 57 }, /* 0000 0101 1000 */
new int[]{ 12, 0x59, 58 }, /* 0000 0101 1001 */
new int[]{ 12, 0x2B, 59 }, /* 0000 0010 1011 */
new int[]{ 12, 0x2C, 60 }, /* 0000 0010 1100 */
new int[]{ 12, 0x5A, 61 }, /* 0000 0101 1010 */
new int[]{ 12, 0x66, 62 }, /* 0000 0110 0110 */
new int[]{ 12, 0x67, 63 }, /* 0000 0110 0111 */
new int[]{ 10, 0xF, 64 }, /* 0000 0011 11 */
new int[]{ 12, 0xC8, 128 }, /* 0000 1100 1000 */
new int[]{ 12, 0xC9, 192 }, /* 0000 1100 1001 */
new int[]{ 12, 0x5B, 256 }, /* 0000 0101 1011 */
new int[]{ 12, 0x33, 320 }, /* 0000 0011 0011 */
new int[]{ 12, 0x34, 384 }, /* 0000 0011 0100 */
new int[]{ 12, 0x35, 448 }, /* 0000 0011 0101 */
new int[]{ 13, 0x6C, 512 }, /* 0000 0011 0110 0 */
new int[]{ 13, 0x6D, 576 }, /* 0000 0011 0110 1 */
new int[]{ 13, 0x4A, 640 }, /* 0000 0010 0101 0 */
new int[]{ 13, 0x4B, 704 }, /* 0000 0010 0101 1 */
new int[]{ 13, 0x4C, 768 }, /* 0000 0010 0110 0 */
new int[]{ 13, 0x4D, 832 }, /* 0000 0010 0110 1 */
new int[]{ 13, 0x72, 896 }, /* 0000 0011 1001 0 */
new int[]{ 13, 0x73, 960 }, /* 0000 0011 1001 1 */
new int[]{ 13, 0x74, 1024 }, /* 0000 0011 1010 0 */
new int[]{ 13, 0x75, 1088 }, /* 0000 0011 1010 1 */
new int[]{ 13, 0x76, 1152 }, /* 0000 0011 1011 0 */
new int[]{ 13, 0x77, 1216 }, /* 0000 0011 1011 1 */
new int[]{ 13, 0x52, 1280 }, /* 0000 0010 1001 0 */
new int[]{ 13, 0x53, 1344 }, /* 0000 0010 1001 1 */
new int[]{ 13, 0x54, 1408 }, /* 0000 0010 1010 0 */
new int[]{ 13, 0x55, 1472 }, /* 0000 0010 1010 1 */
new int[]{ 13, 0x5A, 1536 }, /* 0000 0010 1101 0 */
new int[]{ 13, 0x5B, 1600 }, /* 0000 0010 1101 1 */
new int[]{ 13, 0x64, 1664 }, /* 0000 0011 0010 0 */
new int[]{ 13, 0x65, 1728 }, /* 0000 0011 0010 1 */
new int[]{ 11, 0x8, 1792 }, /* 0000 0001 000 */
new int[]{ 11, 0xC, 1856 }, /* 0000 0001 100 */
new int[]{ 11, 0xD, 1920 }, /* 0000 0001 101 */
new int[]{ 12, 0x12, 1984 }, /* 0000 0001 0010 */
new int[]{ 12, 0x13, 2048 }, /* 0000 0001 0011 */
new int[]{ 12, 0x14, 2112 }, /* 0000 0001 0100 */
new int[]{ 12, 0x15, 2176 }, /* 0000 0001 0101 */
new int[]{ 12, 0x16, 2240 }, /* 0000 0001 0110 */
new int[]{ 12, 0x17, 2304 }, /* 0000 0001 0111 */
new int[]{ 12, 0x1C, 2368 }, /* 0000 0001 1100 */
new int[]{ 12, 0x1D, 2432 }, /* 0000 0001 1101 */
new int[]{ 12, 0x1E, 2496 }, /* 0000 0001 1110 */
new int[]{ 12, 0x1F, 2560 }, /* 0000 0001 1111 */
new int[]{ 12, 0x1, G3CODE_EOL }, /* 0000 0000 0001 */
new int[]{ 9, 0x1, G3CODE_INVALID }, /* 0000 0000 1 */
new int[]{ 10, 0x1, G3CODE_INVALID }, /* 0000 0000 01 */
new int[]{ 11, 0x1, G3CODE_INVALID }, /* 0000 0000 001 */
new int[]{ 12, 0x0, G3CODE_INVALID } /* 0000 0000 0000 */
};
private int[] horizcode =
{ 3, 0x1, 0 }; /* 001 */
private int[] passcode =
{ 4, 0x1, 0 }; /* 0001 */
private int[][] vcodes = {
new int[]{ 7, 0x03, 0 }, /* 0000 011 */
new int[]{ 6, 0x03, 0 }, /* 0000 11 */
new int[]{ 3, 0x03, 0 }, /* 011 */
new int[]{ 1, 0x1, 0 }, /* 1 */
new int[]{ 3, 0x2, 0 }, /* 010 */
new int[]{ 6, 0x02, 0 }, /* 0000 10 */
new int[]{ 7, 0x02, 0 } /* 0000 010 */
};
private int[] msbmask =
{ 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
}
}

View File

@@ -0,0 +1,597 @@
using System;
using System.Net;
using System.IO;
using System.Collections;
using iTextSharp.text;
using iTextSharp.text.pdf;
/*
* Copyright 2003 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License isp distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code isp 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code isp Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code isp Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library isp free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library isp distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec {
/** Reads gif images of all types. All the images in a gif are read in the constructors
* and can be retrieved with other methods.
* @author Paulo Soares (psoares@consiste.pt)
*/
public class GifImage {
protected Stream inp;
protected int width; // full image width
protected int height; // full image height
protected bool gctFlag; // global color table used
protected int bgIndex; // background color index
protected int bgColor; // background color
protected int pixelAspect; // pixel aspect ratio
protected bool lctFlag; // local color table flag
protected bool interlace; // interlace flag
protected int lctSize; // local color table size
protected int ix, iy, iw, ih; // current image rectangle
protected byte[] block = new byte[256]; // current data block
protected int blockSize = 0; // block size
// last graphic control extension info
protected int dispose = 0; // 0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev
protected bool transparency = false; // use transparent color
protected int delay = 0; // delay in milliseconds
protected int transIndex; // transparent color index
protected const int MaxStackSize = 4096; // max decoder pixel stack size
// LZW decoder working arrays
protected short[] prefix;
protected byte[] suffix;
protected byte[] pixelStack;
protected byte[] pixels;
protected byte[] m_out;
protected int m_bpc;
protected int m_gbpc;
protected byte[] m_global_table;
protected byte[] m_local_table;
protected byte[] m_curr_table;
protected int m_line_stride;
protected byte[] fromData;
protected Uri fromUrl;
protected ArrayList frames = new ArrayList(); // frames read from current file
/** Reads gif images from an URL.
* @param url the URL
* @throws IOException on error
*/
public GifImage(Uri url) {
fromUrl = url;
Stream isp = null;
try {
isp = WebRequest.Create(url).GetResponse().GetResponseStream();
Process(isp);
}
finally {
if (isp != null) {
isp.Close();
}
}
}
/** Reads gif images from a file.
* @param file the file
* @throws IOException on error
*/
public GifImage(String file) : this(Utilities.ToURL(file)) {
}
/** Reads gif images from a byte array.
* @param data the byte array
* @throws IOException on error
*/
public GifImage(byte[] data) {
fromData = data;
Stream isp = null;
try {
isp = new MemoryStream(data);
Process(isp);
}
finally {
if (isp != null) {
isp.Close();
}
}
}
/** Reads gif images from a stream. The stream isp not closed.
* @param isp the stream
* @throws IOException on error
*/
public GifImage(Stream isp) {
Process(isp);
}
/** Gets the number of frames the gif has.
* @return the number of frames the gif has
*/
public int GetFrameCount() {
return frames.Count;
}
/** Gets the image from a frame. The first frame isp 1.
* @param frame the frame to get the image from
* @return the image
*/
public Image GetImage(int frame) {
GifFrame gf = (GifFrame)frames[frame - 1];
return gf.image;
}
/** Gets the [x,y] position of the frame in reference to the
* logical screen.
* @param frame the frame
* @return the [x,y] position of the frame
*/
public int[] GetFramePosition(int frame) {
GifFrame gf = (GifFrame)frames[frame - 1];
return new int[]{gf.ix, gf.iy};
}
/** Gets the logical screen. The images may be smaller and placed
* in some position in this screen to playback some animation.
* No image will be be bigger that this.
* @return the logical screen dimensions as [x,y]
*/
public int[] GetLogicalScreen() {
return new int[]{width, height};
}
internal void Process(Stream isp) {
inp = new BufferedStream(isp);
ReadHeader();
ReadContents();
if (frames.Count == 0)
throw new IOException("The file does not contain any valid image.");
}
/**
* Reads GIF file header information.
*/
protected void ReadHeader() {
String id = "";
for (int i = 0; i < 6; i++)
id += (char)inp.ReadByte();
if (!id.StartsWith("GIF8")) {
throw new IOException("Gif signature nor found.");
}
ReadLSD();
if (gctFlag) {
m_global_table = ReadColorTable(m_gbpc);
}
}
/**
* Reads Logical Screen Descriptor
*/
protected void ReadLSD() {
// logical screen size
width = ReadShort();
height = ReadShort();
// packed fields
int packed = inp.ReadByte();
gctFlag = (packed & 0x80) != 0; // 1 : global color table flag
m_gbpc = (packed & 7) + 1;
bgIndex = inp.ReadByte(); // background color index
pixelAspect = inp.ReadByte(); // pixel aspect ratio
}
/**
* Reads next 16-bit value, LSB first
*/
protected int ReadShort() {
// read 16-bit value, LSB first
return inp.ReadByte() | (inp.ReadByte() << 8);
}
/**
* Reads next variable length block from input.
*
* @return number of bytes stored in "buffer"
*/
protected int ReadBlock() {
blockSize = inp.ReadByte();
if (blockSize <= 0)
return blockSize = 0;
for (int k = 0; k < blockSize; ++k) {
int v = inp.ReadByte();
if (v < 0) {
return blockSize = k;
}
block[k] = (byte)v;
}
return blockSize;
}
protected byte[] ReadColorTable(int bpc) {
int ncolors = 1 << bpc;
int nbytes = 3*ncolors;
bpc = NewBpc(bpc);
byte[] table = new byte[(1 << bpc) * 3];
ReadFully(table, 0, nbytes);
return table;
}
static protected int NewBpc(int bpc) {
switch (bpc) {
case 1:
case 2:
case 4:
break;
case 3:
return 4;
default:
return 8;
}
return bpc;
}
protected void ReadContents() {
// read GIF file content blocks
bool done = false;
while (!done) {
int code = inp.ReadByte();
switch (code) {
case 0x2C: // image separator
ReadImage();
break;
case 0x21: // extension
code = inp.ReadByte();
switch (code) {
case 0xf9: // graphics control extension
ReadGraphicControlExt();
break;
case 0xff: // application extension
ReadBlock();
Skip(); // don't care
break;
default: // uninteresting extension
Skip();
break;
}
break;
default:
done = true;
break;
}
}
}
/**
* Reads next frame image
*/
protected void ReadImage() {
ix = ReadShort(); // (sub)image position & size
iy = ReadShort();
iw = ReadShort();
ih = ReadShort();
int packed = inp.ReadByte();
lctFlag = (packed & 0x80) != 0; // 1 - local color table flag
interlace = (packed & 0x40) != 0; // 2 - interlace flag
// 3 - sort flag
// 4-5 - reserved
lctSize = 2 << (packed & 7); // 6-8 - local color table size
m_bpc = NewBpc(m_gbpc);
if (lctFlag) {
m_curr_table = ReadColorTable((packed & 7) + 1); // read table
m_bpc = NewBpc((packed & 7) + 1);
}
else {
m_curr_table = m_global_table;
}
if (transparency && transIndex >= m_curr_table.Length / 3)
transparency = false;
if (transparency && m_bpc == 1) { // Acrobat 5.05 doesn't like this combination
byte[] tp = new byte[12];
Array.Copy(m_curr_table, 0, tp, 0, 6);
m_curr_table = tp;
m_bpc = 2;
}
bool skipZero = DecodeImageData(); // decode pixel data
if (!skipZero)
Skip();
Image img = null;
img = new ImgRaw(iw, ih, 1, m_bpc, m_out);
PdfArray colorspace = new PdfArray();
colorspace.Add(PdfName.INDEXED);
colorspace.Add(PdfName.DEVICERGB);
int len = m_curr_table.Length;
colorspace.Add(new PdfNumber(len / 3 - 1));
colorspace.Add(new PdfString(m_curr_table));
PdfDictionary ad = new PdfDictionary();
ad.Put(PdfName.COLORSPACE, colorspace);
img.Additional = ad;
if (transparency) {
img.Transparency = new int[]{transIndex, transIndex};
}
img.OriginalType = Image.ORIGINAL_GIF;
img.OriginalData = fromData;
img.Url = fromUrl;
GifFrame gf = new GifFrame();
gf.image = img;
gf.ix = ix;
gf.iy = iy;
frames.Add(gf); // add image to frame list
//ResetFrame();
}
protected bool DecodeImageData() {
int NullCode = -1;
int npix = iw * ih;
int available, clear, code_mask, code_size, end_of_information, in_code, old_code,
bits, code, count, i, datum, data_size, first, top, bi;
bool skipZero = false;
if (prefix == null)
prefix = new short[MaxStackSize];
if (suffix == null)
suffix = new byte[MaxStackSize];
if (pixelStack == null)
pixelStack = new byte[MaxStackSize+1];
m_line_stride = (iw * m_bpc + 7) / 8;
m_out = new byte[m_line_stride * ih];
int pass = 1;
int inc = interlace ? 8 : 1;
int line = 0;
int xpos = 0;
// Initialize GIF data stream decoder.
data_size = inp.ReadByte();
clear = 1 << data_size;
end_of_information = clear + 1;
available = clear + 2;
old_code = NullCode;
code_size = data_size + 1;
code_mask = (1 << code_size) - 1;
for (code = 0; code < clear; code++) {
prefix[code] = 0;
suffix[code] = (byte) code;
}
// Decode GIF pixel stream.
datum = bits = count = first = top = bi = 0;
for (i = 0; i < npix; ) {
if (top == 0) {
if (bits < code_size) {
// Load bytes until there are enough bits for a code.
if (count == 0) {
// Read a new data block.
count = ReadBlock();
if (count <= 0) {
skipZero = true;
break;
}
bi = 0;
}
datum += (((int) block[bi]) & 0xff) << bits;
bits += 8;
bi++;
count--;
continue;
}
// Get the next code.
code = datum & code_mask;
datum >>= code_size;
bits -= code_size;
// Interpret the code
if ((code > available) || (code == end_of_information))
break;
if (code == clear) {
// Reset decoder.
code_size = data_size + 1;
code_mask = (1 << code_size) - 1;
available = clear + 2;
old_code = NullCode;
continue;
}
if (old_code == NullCode) {
pixelStack[top++] = suffix[code];
old_code = code;
first = code;
continue;
}
in_code = code;
if (code == available) {
pixelStack[top++] = (byte) first;
code = old_code;
}
while (code > clear) {
pixelStack[top++] = suffix[code];
code = prefix[code];
}
first = ((int) suffix[code]) & 0xff;
// Add a new string to the string table,
if (available >= MaxStackSize)
break;
pixelStack[top++] = (byte) first;
prefix[available] = (short) old_code;
suffix[available] = (byte) first;
available++;
if (((available & code_mask) == 0) && (available < MaxStackSize)) {
code_size++;
code_mask += available;
}
old_code = in_code;
}
// Pop a pixel off the pixel stack.
top--;
i++;
SetPixel(xpos, line, pixelStack[top]);
++xpos;
if (xpos >= iw) {
xpos = 0;
line += inc;
if (line >= ih) {
if (interlace) {
do {
pass++;
switch (pass) {
case 2:
line = 4;
break;
case 3:
line = 2;
inc = 4;
break;
case 4:
line = 1;
inc = 2;
break;
default: // this shouldn't happen
line = ih - 1;
inc = 0;
break;
}
} while (line >= ih);
}
else {
line = ih - 1; // this shouldn't happen
inc = 0;
}
}
}
}
return skipZero;
}
protected void SetPixel(int x, int y, int v) {
if (m_bpc == 8) {
int pos = x + iw * y;
m_out[pos] = (byte)v;
}
else {
int pos = m_line_stride * y + x / (8 / m_bpc);
int vout = v << (8 - m_bpc * (x % (8 / m_bpc))- m_bpc);
m_out[pos] |= (byte)vout;
}
}
/**
* Resets frame state for reading next image.
*/
protected void ResetFrame() {
}
/**
* Reads Graphics Control Extension values
*/
protected void ReadGraphicControlExt() {
inp.ReadByte(); // block size
int packed = inp.ReadByte(); // packed fields
dispose = (packed & 0x1c) >> 2; // disposal method
if (dispose == 0)
dispose = 1; // elect to keep old image if discretionary
transparency = (packed & 1) != 0;
delay = ReadShort() * 10; // delay inp milliseconds
transIndex = inp.ReadByte(); // transparent color index
inp.ReadByte(); // block terminator
}
/**
* Skips variable length blocks up to and including
* next zero length block.
*/
protected void Skip() {
do {
ReadBlock();
} while (blockSize > 0);
}
private void ReadFully(byte[] b, int offset, int count) {
while (count > 0) {
int n = inp.Read(b, offset, count);
if (n <= 0)
throw new IOException("Insufficient data.");
count -= n;
offset += n;
}
}
internal class GifFrame {
internal Image image;
internal int ix;
internal int iy;
}
}
}

View File

@@ -0,0 +1,971 @@
using System;
using System.IO;
using System.Net;
using System.Text;
using System.util.zlib;
using System.util;
using iTextSharp.text;
using iTextSharp.text.pdf;
/*
* Copyright 2003-2008 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*
* This code is based on a series of source files originally released
* by SUN in the context of the JAI project. The original code was released
* under the BSD license in a specific wording. In a mail dating from
* January 23, 2008, Brian Burkhalter (@sun.com) gave us permission
* to use the code under the following version of the BSD license:
*
* Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution 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.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for
* use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
namespace iTextSharp.text.pdf.codec {
/** Reads a PNG image. All types of PNG can be read.
* <p>
* It is based in part in the JAI codec.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
public class PngImage {
/** Some PNG specific values. */
public static int[] PNGID = {137, 80, 78, 71, 13, 10, 26, 10};
/** A PNG marker. */
public const String IHDR = "IHDR";
/** A PNG marker. */
public const String PLTE = "PLTE";
/** A PNG marker. */
public const String IDAT = "IDAT";
/** A PNG marker. */
public const String IEND = "IEND";
/** A PNG marker. */
public const String tRNS = "tRNS";
/** A PNG marker. */
public const String pHYs = "pHYs";
/** A PNG marker. */
public const String gAMA = "gAMA";
/** A PNG marker. */
public const String cHRM = "cHRM";
/** A PNG marker. */
public const String sRGB = "sRGB";
/** A PNG marker. */
public const String iCCP = "iCCP";
private const int TRANSFERSIZE = 4096;
private const int PNG_FILTER_NONE = 0;
private const int PNG_FILTER_SUB = 1;
private const int PNG_FILTER_UP = 2;
private const int PNG_FILTER_AVERAGE = 3;
private const int PNG_FILTER_PAETH = 4;
private static PdfName[] intents = {PdfName.PERCEPTUAL,
PdfName.RELATIVECALORIMETRIC,PdfName.SATURATION,PdfName.ABSOLUTECALORIMETRIC};
Stream isp;
Stream dataStream;
int width;
int height;
int bitDepth;
int colorType;
int compressionMethod;
int filterMethod;
int interlaceMethod;
PdfDictionary additional = new PdfDictionary();
byte[] image;
byte[] smask;
byte[] trans;
MemoryStream idat = new MemoryStream();
int dpiX;
int dpiY;
float XYRatio;
bool genBWMask;
bool palShades;
int transRedGray = -1;
int transGreen = -1;
int transBlue = -1;
int inputBands;
int bytesPerPixel; // number of bytes per input pixel
byte[] colorTable;
float gamma = 1f;
bool hasCHRM = false;
float xW, yW, xR, yR, xG, yG, xB, yB;
PdfName intent;
ICC_Profile icc_profile;
/** Creates a new instance of PngImage */
PngImage(Stream isp) {
this.isp = isp;
}
/** Reads a PNG from an url.
* @param url the url
* @throws IOException on error
* @return the image
*/
public static Image GetImage(Uri url) {
Stream isp = null;
try {
isp = WebRequest.Create(url).GetResponse().GetResponseStream();
Image img = GetImage(isp);
img.Url = url;
return img;
}
finally {
if (isp != null) {
isp.Close();
}
}
}
/** Reads a PNG from a stream.
* @param is the stream
* @throws IOException on error
* @return the image
*/
public static Image GetImage(Stream isp) {
PngImage png = new PngImage(isp);
return png.GetImage();
}
/** Reads a PNG from a file.
* @param file the file
* @throws IOException on error
* @return the image
*/
public static Image GetImage(String file) {
return GetImage(Utilities.ToURL(file));
}
/** Reads a PNG from a byte array.
* @param data the byte array
* @throws IOException on error
* @return the image
*/
public static Image GetImage(byte[] data) {
Stream isp = new MemoryStream(data);
Image img = GetImage(isp);
img.OriginalData = data;
return img;
}
static bool CheckMarker(String s) {
if (s.Length != 4)
return false;
for (int k = 0; k < 4; ++k) {
char c = s[k];
if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z'))
return false;
}
return true;
}
void ReadPng() {
for (int i = 0; i < PNGID.Length; i++) {
if (PNGID[i] != isp.ReadByte()) {
throw new IOException("File is not a valid PNG.");
}
}
byte[] buffer = new byte[TRANSFERSIZE];
while (true) {
int len = GetInt(isp);
String marker = GetString(isp);
if (len < 0 || !CheckMarker(marker))
throw new IOException("Corrupted PNG file.");
if (IDAT.Equals(marker)) {
int size;
while (len != 0) {
size = isp.Read(buffer, 0, Math.Min(len, TRANSFERSIZE));
if (size <= 0)
return;
idat.Write(buffer, 0, size);
len -= size;
}
}
else if (tRNS.Equals(marker)) {
switch (colorType) {
case 0:
if (len >= 2) {
len -= 2;
int gray = GetWord(isp);
if (bitDepth == 16)
transRedGray = gray;
else
additional.Put(PdfName.MASK, new PdfLiteral("["+gray+" "+gray+"]"));
}
break;
case 2:
if (len >= 6) {
len -= 6;
int red = GetWord(isp);
int green = GetWord(isp);
int blue = GetWord(isp);
if (bitDepth == 16) {
transRedGray = red;
transGreen = green;
transBlue = blue;
}
else
additional.Put(PdfName.MASK, new PdfLiteral("["+red+" "+red+" "+green+" "+green+" "+blue+" "+blue+"]"));
}
break;
case 3:
if (len > 0) {
trans = new byte[len];
for (int k = 0; k < len; ++k)
trans[k] = (byte)isp.ReadByte();
len = 0;
}
break;
}
Utilities.Skip(isp, len);
}
else if (IHDR.Equals(marker)) {
width = GetInt(isp);
height = GetInt(isp);
bitDepth = isp.ReadByte();
colorType = isp.ReadByte();
compressionMethod = isp.ReadByte();
filterMethod = isp.ReadByte();
interlaceMethod = isp.ReadByte();
}
else if (PLTE.Equals(marker)) {
if (colorType == 3) {
PdfArray colorspace = new PdfArray();
colorspace.Add(PdfName.INDEXED);
colorspace.Add(GetColorspace());
colorspace.Add(new PdfNumber(len / 3 - 1));
ByteBuffer colortable = new ByteBuffer();
while ((len--) > 0) {
colortable.Append_i(isp.ReadByte());
}
colorspace.Add(new PdfString(colorTable = colortable.ToByteArray()));
additional.Put(PdfName.COLORSPACE, colorspace);
}
else {
Utilities.Skip(isp, len);
}
}
else if (pHYs.Equals(marker)) {
int dx = GetInt(isp);
int dy = GetInt(isp);
int unit = isp.ReadByte();
if (unit == 1) {
dpiX = (int)((float)dx * 0.0254f + 0.5f);
dpiY = (int)((float)dy * 0.0254f + 0.5f);
}
else {
if (dy != 0)
XYRatio = (float)dx / (float)dy;
}
}
else if (cHRM.Equals(marker)) {
xW = (float)GetInt(isp) / 100000f;
yW = (float)GetInt(isp) / 100000f;
xR = (float)GetInt(isp) / 100000f;
yR = (float)GetInt(isp) / 100000f;
xG = (float)GetInt(isp) / 100000f;
yG = (float)GetInt(isp) / 100000f;
xB = (float)GetInt(isp) / 100000f;
yB = (float)GetInt(isp) / 100000f;
hasCHRM = !(Math.Abs(xW)<0.0001f||Math.Abs(yW)<0.0001f||Math.Abs(xR)<0.0001f||Math.Abs(yR)<0.0001f||Math.Abs(xG)<0.0001f||Math.Abs(yG)<0.0001f||Math.Abs(xB)<0.0001f||Math.Abs(yB)<0.0001f);
}
else if (sRGB.Equals(marker)) {
int ri = isp.ReadByte();
intent = intents[ri];
gamma = 2.2f;
xW = 0.3127f;
yW = 0.329f;
xR = 0.64f;
yR = 0.33f;
xG = 0.3f;
yG = 0.6f;
xB = 0.15f;
yB = 0.06f;
hasCHRM = true;
}
else if (gAMA.Equals(marker)) {
int gm = GetInt(isp);
if (gm != 0) {
gamma = 100000f / (float)gm;
if (!hasCHRM) {
xW = 0.3127f;
yW = 0.329f;
xR = 0.64f;
yR = 0.33f;
xG = 0.3f;
yG = 0.6f;
xB = 0.15f;
yB = 0.06f;
hasCHRM = true;
}
}
}
else if (iCCP.Equals(marker)) {
do {
--len;
} while (isp.ReadByte() != 0);
isp.ReadByte();
--len;
byte[] icccom = new byte[len];
int p = 0;
while (len > 0) {
int r = isp.Read(icccom, p, len);
if (r < 0)
throw new IOException("Premature end of file.");
p += r;
len -= r;
}
byte[] iccp = PdfReader.FlateDecode(icccom, true);
icccom = null;
try {
icc_profile = ICC_Profile.GetInstance(iccp);
}
catch {
icc_profile = null;
}
}
else if (IEND.Equals(marker)) {
break;
}
else {
Utilities.Skip(isp, len);
}
Utilities.Skip(isp, 4);
}
}
PdfObject GetColorspace() {
if (icc_profile != null) {
if ((colorType & 2) == 0)
return PdfName.DEVICEGRAY;
else
return PdfName.DEVICERGB;
}
if (gamma == 1f && !hasCHRM) {
if ((colorType & 2) == 0)
return PdfName.DEVICEGRAY;
else
return PdfName.DEVICERGB;
}
else {
PdfArray array = new PdfArray();
PdfDictionary dic = new PdfDictionary();
if ((colorType & 2) == 0) {
if (gamma == 1f)
return PdfName.DEVICEGRAY;
array.Add(PdfName.CALGRAY);
dic.Put(PdfName.GAMMA, new PdfNumber(gamma));
dic.Put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]"));
array.Add(dic);
}
else {
PdfObject wp = new PdfLiteral("[1 1 1]");
array.Add(PdfName.CALRGB);
if (gamma != 1f) {
PdfArray gm = new PdfArray();
PdfNumber n = new PdfNumber(gamma);
gm.Add(n);
gm.Add(n);
gm.Add(n);
dic.Put(PdfName.GAMMA, gm);
}
if (hasCHRM) {
float z = yW*((xG-xB)*yR-(xR-xB)*yG+(xR-xG)*yB);
float YA = yR*((xG-xB)*yW-(xW-xB)*yG+(xW-xG)*yB)/z;
float XA = YA*xR/yR;
float ZA = YA*((1-xR)/yR-1);
float YB = -yG*((xR-xB)*yW-(xW-xB)*yR+(xW-xR)*yB)/z;
float XB = YB*xG/yG;
float ZB = YB*((1-xG)/yG-1);
float YC = yB*((xR-xG)*yW-(xW-xG)*yW+(xW-xR)*yG)/z;
float XC = YC*xB/yB;
float ZC = YC*((1-xB)/yB-1);
float XW = XA+XB+XC;
float YW = 1;//YA+YB+YC;
float ZW = ZA+ZB+ZC;
PdfArray wpa = new PdfArray();
wpa.Add(new PdfNumber(XW));
wpa.Add(new PdfNumber(YW));
wpa.Add(new PdfNumber(ZW));
wp = wpa;
PdfArray matrix = new PdfArray();
matrix.Add(new PdfNumber(XA));
matrix.Add(new PdfNumber(YA));
matrix.Add(new PdfNumber(ZA));
matrix.Add(new PdfNumber(XB));
matrix.Add(new PdfNumber(YB));
matrix.Add(new PdfNumber(ZB));
matrix.Add(new PdfNumber(XC));
matrix.Add(new PdfNumber(YC));
matrix.Add(new PdfNumber(ZC));
dic.Put(PdfName.MATRIX, matrix);
}
dic.Put(PdfName.WHITEPOINT, wp);
array.Add(dic);
}
return array;
}
}
Image GetImage() {
ReadPng();
int pal0 = 0;
int palIdx = 0;
palShades = false;
if (trans != null) {
for (int k = 0; k < trans.Length; ++k) {
int n = trans[k] & 0xff;
if (n == 0) {
++pal0;
palIdx = k;
}
if (n != 0 && n != 255) {
palShades = true;
break;
}
}
}
if ((colorType & 4) != 0)
palShades = true;
genBWMask = (!palShades && (pal0 > 1 || transRedGray >= 0));
if (!palShades && !genBWMask && pal0 == 1) {
additional.Put(PdfName.MASK, new PdfLiteral("["+palIdx+" "+palIdx+"]"));
}
bool needDecode = (interlaceMethod == 1) || (bitDepth == 16) || ((colorType & 4) != 0) || palShades || genBWMask;
switch (colorType) {
case 0:
inputBands = 1;
break;
case 2:
inputBands = 3;
break;
case 3:
inputBands = 1;
break;
case 4:
inputBands = 2;
break;
case 6:
inputBands = 4;
break;
}
if (needDecode)
DecodeIdat();
int components = inputBands;
if ((colorType & 4) != 0)
--components;
int bpc = bitDepth;
if (bpc == 16)
bpc = 8;
Image img;
if (image != null)
img = Image.GetInstance(width, height, components, bpc, image);
else {
img = new ImgRaw(width, height, components, bpc, idat.ToArray());
img.Deflated = true;
PdfDictionary decodeparms = new PdfDictionary();
decodeparms.Put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth));
decodeparms.Put(PdfName.PREDICTOR, new PdfNumber(15));
decodeparms.Put(PdfName.COLUMNS, new PdfNumber(width));
decodeparms.Put(PdfName.COLORS, new PdfNumber((colorType == 3 || (colorType & 2) == 0) ? 1 : 3));
additional.Put(PdfName.DECODEPARMS, decodeparms);
}
if (additional.Get(PdfName.COLORSPACE) == null)
additional.Put(PdfName.COLORSPACE, GetColorspace());
if (intent != null)
additional.Put(PdfName.INTENT, intent);
if (additional.Size > 0)
img.Additional = additional;
if (icc_profile != null)
img.TagICC = icc_profile;
if (palShades) {
Image im2 = Image.GetInstance(width, height, 1, 8, smask);
im2.MakeMask();
img.ImageMask = im2;
}
if (genBWMask) {
Image im2 = Image.GetInstance(width, height, 1, 1, smask);
im2.MakeMask();
img.ImageMask = im2;
}
img.SetDpi(dpiX, dpiY);
img.XYRatio = XYRatio;
img.OriginalType = Image.ORIGINAL_PNG;
return img;
}
void DecodeIdat() {
int nbitDepth = bitDepth;
if (nbitDepth == 16)
nbitDepth = 8;
int size = -1;
bytesPerPixel = (bitDepth == 16) ? 2 : 1;
switch (colorType) {
case 0:
size = (nbitDepth * width + 7) / 8 * height;
break;
case 2:
size = width * 3 * height;
bytesPerPixel *= 3;
break;
case 3:
if (interlaceMethod == 1)
size = (nbitDepth * width + 7) / 8 * height;
bytesPerPixel = 1;
break;
case 4:
size = width * height;
bytesPerPixel *= 2;
break;
case 6:
size = width * 3 * height;
bytesPerPixel *= 4;
break;
}
if (size >= 0)
image = new byte[size];
if (palShades)
smask = new byte[width * height];
else if (genBWMask)
smask = new byte[(width + 7) / 8 * height];
idat.Position = 0;
dataStream = new ZInflaterInputStream(idat);
if (interlaceMethod != 1) {
DecodePass(0, 0, 1, 1, width, height);
}
else {
DecodePass(0, 0, 8, 8, (width + 7)/8, (height + 7)/8);
DecodePass(4, 0, 8, 8, (width + 3)/8, (height + 7)/8);
DecodePass(0, 4, 4, 8, (width + 3)/4, (height + 3)/8);
DecodePass(2, 0, 4, 4, (width + 1)/4, (height + 3)/4);
DecodePass(0, 2, 2, 4, (width + 1)/2, (height + 1)/4);
DecodePass(1, 0, 2, 2, width/2, (height + 1)/2);
DecodePass(0, 1, 1, 2, width, height/2);
}
}
void DecodePass( int xOffset, int yOffset,
int xStep, int yStep,
int passWidth, int passHeight) {
if ((passWidth == 0) || (passHeight == 0)) {
return;
}
int bytesPerRow = (inputBands*passWidth*bitDepth + 7)/8;
byte[] curr = new byte[bytesPerRow];
byte[] prior = new byte[bytesPerRow];
// Decode the (sub)image row-by-row
int srcY, dstY;
for (srcY = 0, dstY = yOffset;
srcY < passHeight;
srcY++, dstY += yStep) {
// Read the filter type byte and a row of data
int filter = 0;
try {
filter = dataStream.ReadByte();
ReadFully(dataStream,curr, 0, bytesPerRow);
} catch {
// empty on purpose
}
switch (filter) {
case PNG_FILTER_NONE:
break;
case PNG_FILTER_SUB:
DecodeSubFilter(curr, bytesPerRow, bytesPerPixel);
break;
case PNG_FILTER_UP:
DecodeUpFilter(curr, prior, bytesPerRow);
break;
case PNG_FILTER_AVERAGE:
DecodeAverageFilter(curr, prior, bytesPerRow, bytesPerPixel);
break;
case PNG_FILTER_PAETH:
DecodePaethFilter(curr, prior, bytesPerRow, bytesPerPixel);
break;
default:
// Error -- uknown filter type
throw new Exception("PNG filter unknown.");
}
ProcessPixels(curr, xOffset, xStep, dstY, passWidth);
// Swap curr and prior
byte[] tmp = prior;
prior = curr;
curr = tmp;
}
}
void ProcessPixels(byte[] curr, int xOffset, int step, int y, int width) {
int srcX, dstX;
int[] outp = GetPixel(curr);
int sizes = 0;
switch (colorType) {
case 0:
case 3:
case 4:
sizes = 1;
break;
case 2:
case 6:
sizes = 3;
break;
}
if (image != null) {
dstX = xOffset;
int yStride = (sizes*this.width*(bitDepth == 16 ? 8 : bitDepth)+ 7)/8;
for (srcX = 0; srcX < width; srcX++) {
SetPixel(image, outp, inputBands * srcX, sizes, dstX, y, bitDepth, yStride);
dstX += step;
}
}
if (palShades) {
if ((colorType & 4) != 0) {
if (bitDepth == 16) {
for (int k = 0; k < width; ++k) {
int t = k * inputBands + sizes;
outp[t] = Util.USR(outp[t], 8);
}
}
int yStride = this.width;
dstX = xOffset;
for (srcX = 0; srcX < width; srcX++) {
SetPixel(smask, outp, inputBands * srcX + sizes, 1, dstX, y, 8, yStride);
dstX += step;
}
}
else { //colorType 3
int yStride = this.width;
int[] v = new int[1];
dstX = xOffset;
for (srcX = 0; srcX < width; srcX++) {
int idx = outp[srcX];
if (idx < trans.Length)
v[0] = trans[idx];
SetPixel(smask, v, 0, 1, dstX, y, 8, yStride);
dstX += step;
}
}
}
else if (genBWMask) {
switch (colorType) {
case 3: {
int yStride = (this.width + 7) / 8;
int[] v = new int[1];
dstX = xOffset;
for (srcX = 0; srcX < width; srcX++) {
int idx = outp[srcX];
if (idx < trans.Length)
v[0] = (trans[idx] == 0 ? 1 : 0);
SetPixel(smask, v, 0, 1, dstX, y, 1, yStride);
dstX += step;
}
break;
}
case 0: {
int yStride = (this.width + 7) / 8;
int[] v = new int[1];
dstX = xOffset;
for (srcX = 0; srcX < width; srcX++) {
int g = outp[srcX];
v[0] = (g == transRedGray ? 1 : 0);
SetPixel(smask, v, 0, 1, dstX, y, 1, yStride);
dstX += step;
}
break;
}
case 2: {
int yStride = (this.width + 7) / 8;
int[] v = new int[1];
dstX = xOffset;
for (srcX = 0; srcX < width; srcX++) {
int markRed = inputBands * srcX;
v[0] = (outp[markRed] == transRedGray && outp[markRed + 1] == transGreen
&& outp[markRed + 2] == transBlue ? 1 : 0);
SetPixel(smask, v, 0, 1, dstX, y, 1, yStride);
dstX += step;
}
break;
}
}
}
}
static int GetPixel(byte[] image, int x, int y, int bitDepth, int bytesPerRow) {
if (bitDepth == 8) {
int pos = bytesPerRow * y + x;
return image[pos] & 0xff;
}
else {
int pos = bytesPerRow * y + x / (8 / bitDepth);
int v = image[pos] >> (8 - bitDepth * (x % (8 / bitDepth))- bitDepth);
return v & ((1 << bitDepth) - 1);
}
}
static void SetPixel(byte[] image, int[] data, int offset, int size, int x, int y, int bitDepth, int bytesPerRow) {
if (bitDepth == 8) {
int pos = bytesPerRow * y + size * x;
for (int k = 0; k < size; ++k)
image[pos + k] = (byte)data[k + offset];
}
else if (bitDepth == 16) {
int pos = bytesPerRow * y + size * x;
for (int k = 0; k < size; ++k)
image[pos + k] = (byte)(data[k + offset] >> 8);
}
else {
int pos = bytesPerRow * y + x / (8 / bitDepth);
int v = data[offset] << (8 - bitDepth * (x % (8 / bitDepth))- bitDepth);
image[pos] |= (byte)v;
}
}
int[] GetPixel(byte[] curr) {
switch (bitDepth) {
case 8: {
int[] outp = new int[curr.Length];
for (int k = 0; k < outp.Length; ++k)
outp[k] = curr[k] & 0xff;
return outp;
}
case 16: {
int[] outp = new int[curr.Length / 2];
for (int k = 0; k < outp.Length; ++k)
outp[k] = ((curr[k * 2] & 0xff) << 8) + (curr[k * 2 + 1] & 0xff);
return outp;
}
default: {
int[] outp = new int[curr.Length * 8 / bitDepth];
int idx = 0;
int passes = 8 / bitDepth;
int mask = (1 << bitDepth) - 1;
for (int k = 0; k < curr.Length; ++k) {
for (int j = passes - 1; j >= 0; --j) {
outp[idx++] = Util.USR(curr[k], bitDepth * j) & mask;
}
}
return outp;
}
}
}
private static void DecodeSubFilter(byte[] curr, int count, int bpp) {
for (int i = bpp; i < count; i++) {
int val;
val = curr[i] & 0xff;
val += curr[i - bpp] & 0xff;
curr[i] = (byte)val;
}
}
private static void DecodeUpFilter(byte[] curr, byte[] prev,
int count) {
for (int i = 0; i < count; i++) {
int raw = curr[i] & 0xff;
int prior = prev[i] & 0xff;
curr[i] = (byte)(raw + prior);
}
}
private static void DecodeAverageFilter(byte[] curr, byte[] prev,
int count, int bpp) {
int raw, priorPixel, priorRow;
for (int i = 0; i < bpp; i++) {
raw = curr[i] & 0xff;
priorRow = prev[i] & 0xff;
curr[i] = (byte)(raw + priorRow/2);
}
for (int i = bpp; i < count; i++) {
raw = curr[i] & 0xff;
priorPixel = curr[i - bpp] & 0xff;
priorRow = prev[i] & 0xff;
curr[i] = (byte)(raw + (priorPixel + priorRow)/2);
}
}
private static int PaethPredictor(int a, int b, int c) {
int p = a + b - c;
int pa = Math.Abs(p - a);
int pb = Math.Abs(p - b);
int pc = Math.Abs(p - c);
if ((pa <= pb) && (pa <= pc)) {
return a;
} else if (pb <= pc) {
return b;
} else {
return c;
}
}
private static void DecodePaethFilter(byte[] curr, byte[] prev,
int count, int bpp) {
int raw, priorPixel, priorRow, priorRowPixel;
for (int i = 0; i < bpp; i++) {
raw = curr[i] & 0xff;
priorRow = prev[i] & 0xff;
curr[i] = (byte)(raw + priorRow);
}
for (int i = bpp; i < count; i++) {
raw = curr[i] & 0xff;
priorPixel = curr[i - bpp] & 0xff;
priorRow = prev[i] & 0xff;
priorRowPixel = prev[i - bpp] & 0xff;
curr[i] = (byte)(raw + PaethPredictor(priorPixel,
priorRow,
priorRowPixel));
}
}
/**
* Gets an <CODE>int</CODE> from an <CODE>Stream</CODE>.
*
* @param is an <CODE>Stream</CODE>
* @return the value of an <CODE>int</CODE>
*/
public static int GetInt(Stream isp) {
return (isp.ReadByte() << 24) + (isp.ReadByte() << 16) + (isp.ReadByte() << 8) + isp.ReadByte();
}
/**
* Gets a <CODE>word</CODE> from an <CODE>Stream</CODE>.
*
* @param is an <CODE>Stream</CODE>
* @return the value of an <CODE>int</CODE>
*/
public static int GetWord(Stream isp) {
return (isp.ReadByte() << 8) + isp.ReadByte();
}
/**
* Gets a <CODE>String</CODE> from an <CODE>Stream</CODE>.
*
* @param is an <CODE>Stream</CODE>
* @return the value of an <CODE>int</CODE>
*/
public static String GetString(Stream isp) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < 4; i++) {
buf.Append((char)isp.ReadByte());
}
return buf.ToString();
}
private static void ReadFully(Stream inp, byte[] b, int offset, int count) {
while (count > 0) {
int n = inp.Read(b, offset, count);
if (n <= 0)
throw new IOException("Insufficient data.");
count -= n;
offset += n;
}
}
}
}

View File

@@ -0,0 +1,282 @@
using System;
/*
* Copyright 2003-2005 by Paulo Soares.
*
* This list of constants was originally released with libtiff
* under the following license:
*
* Copyright (c) 1988-1997 Sam Leffler
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
namespace iTextSharp.text.pdf.codec {
/**
* A list of constants used in class TIFFImage.
*/
public class TIFFConstants {
/*
* TIFF Tag Definitions (from tifflib).
*/
public const int TIFFTAG_SUBFILETYPE = 254; /* subfile data descriptor */
public const int FILETYPE_REDUCEDIMAGE = 0x1; /* reduced resolution version */
public const int FILETYPE_PAGE = 0x2; /* one page of many */
public const int FILETYPE_MASK = 0x4; /* transparency mask */
public const int TIFFTAG_OSUBFILETYPE = 255; /* +kind of data in subfile */
public const int OFILETYPE_IMAGE = 1; /* full resolution image data */
public const int OFILETYPE_REDUCEDIMAGE = 2; /* reduced size image data */
public const int OFILETYPE_PAGE = 3; /* one page of many */
public const int TIFFTAG_IMAGEWIDTH = 256; /* image width in pixels */
public const int TIFFTAG_IMAGELENGTH = 257; /* image height in pixels */
public const int TIFFTAG_BITSPERSAMPLE = 258; /* bits per channel (sample) */
public const int TIFFTAG_COMPRESSION = 259; /* data compression technique */
public const int COMPRESSION_NONE = 1; /* dump mode */
public const int COMPRESSION_CCITTRLE = 2; /* CCITT modified Huffman RLE */
public const int COMPRESSION_CCITTFAX3 = 3; /* CCITT Group 3 fax encoding */
public const int COMPRESSION_CCITTFAX4 = 4; /* CCITT Group 4 fax encoding */
public const int COMPRESSION_LZW = 5; /* Lempel-Ziv & Welch */
public const int COMPRESSION_OJPEG = 6; /* !6.0 JPEG */
public const int COMPRESSION_JPEG = 7; /* %JPEG DCT compression */
public const int COMPRESSION_NEXT = 32766; /* NeXT 2-bit RLE */
public const int COMPRESSION_CCITTRLEW = 32771; /* #1 w/ word alignment */
public const int COMPRESSION_PACKBITS = 32773; /* Macintosh RLE */
public const int COMPRESSION_THUNDERSCAN = 32809; /* ThunderScan RLE */
/* codes 32895-32898 are reserved for ANSI IT8 TIFF/IT <dkelly@etsinc.com) */
public const int COMPRESSION_IT8CTPAD = 32895; /* IT8 CT w/padding */
public const int COMPRESSION_IT8LW = 32896; /* IT8 Linework RLE */
public const int COMPRESSION_IT8MP = 32897; /* IT8 Monochrome picture */
public const int COMPRESSION_IT8BL = 32898; /* IT8 Binary line art */
/* compression codes 32908-32911 are reserved for Pixar */
public const int COMPRESSION_PIXARFILM = 32908; /* Pixar companded 10bit LZW */
public const int COMPRESSION_PIXARLOG = 32909; /* Pixar companded 11bit ZIP */
public const int COMPRESSION_DEFLATE = 32946; /* Deflate compression */
public const int COMPRESSION_ADOBE_DEFLATE = 8; /* Deflate compression, as recognized by Adobe */
/* compression code 32947 is reserved for Oceana Matrix <dev@oceana.com> */
public const int COMPRESSION_DCS = 32947; /* Kodak DCS encoding */
public const int COMPRESSION_JBIG = 34661; /* ISO JBIG */
public const int COMPRESSION_SGILOG = 34676; /* SGI Log Luminance RLE */
public const int COMPRESSION_SGILOG24 = 34677; /* SGI Log 24-bit packed */
public const int TIFFTAG_PHOTOMETRIC = 262; /* photometric interpretation */
public const int PHOTOMETRIC_MINISWHITE = 0; /* min value is white */
public const int PHOTOMETRIC_MINISBLACK = 1; /* min value is black */
public const int PHOTOMETRIC_RGB = 2; /* RGB color model */
public const int PHOTOMETRIC_PALETTE = 3; /* color map indexed */
public const int PHOTOMETRIC_MASK = 4; /* $holdout mask */
public const int PHOTOMETRIC_SEPARATED = 5; /* !color separations */
public const int PHOTOMETRIC_YCBCR = 6; /* !CCIR 601 */
public const int PHOTOMETRIC_CIELAB = 8; /* !1976 CIE L*a*b* */
public const int PHOTOMETRIC_LOGL = 32844; /* CIE Log2(L) */
public const int PHOTOMETRIC_LOGLUV = 32845; /* CIE Log2(L) (u',v') */
public const int TIFFTAG_THRESHHOLDING = 263; /* +thresholding used on data */
public const int THRESHHOLD_BILEVEL = 1; /* b&w art scan */
public const int THRESHHOLD_HALFTONE = 2; /* or dithered scan */
public const int THRESHHOLD_ERRORDIFFUSE = 3; /* usually floyd-steinberg */
public const int TIFFTAG_CELLWIDTH = 264; /* +dithering matrix width */
public const int TIFFTAG_CELLLENGTH = 265; /* +dithering matrix height */
public const int TIFFTAG_FILLORDER = 266; /* data order within a byte */
public const int FILLORDER_MSB2LSB = 1; /* most significant -> least */
public const int FILLORDER_LSB2MSB = 2; /* least significant -> most */
public const int TIFFTAG_DOCUMENTNAME = 269; /* name of doc. image is from */
public const int TIFFTAG_IMAGEDESCRIPTION = 270; /* info about image */
public const int TIFFTAG_MAKE = 271; /* scanner manufacturer name */
public const int TIFFTAG_MODEL = 272; /* scanner model name/number */
public const int TIFFTAG_STRIPOFFSETS = 273; /* offsets to data strips */
public const int TIFFTAG_ORIENTATION = 274; /* +image orientation */
public const int ORIENTATION_TOPLEFT = 1; /* row 0 top, col 0 lhs */
public const int ORIENTATION_TOPRIGHT = 2; /* row 0 top, col 0 rhs */
public const int ORIENTATION_BOTRIGHT = 3; /* row 0 bottom, col 0 rhs */
public const int ORIENTATION_BOTLEFT = 4; /* row 0 bottom, col 0 lhs */
public const int ORIENTATION_LEFTTOP = 5; /* row 0 lhs, col 0 top */
public const int ORIENTATION_RIGHTTOP = 6; /* row 0 rhs, col 0 top */
public const int ORIENTATION_RIGHTBOT = 7; /* row 0 rhs, col 0 bottom */
public const int ORIENTATION_LEFTBOT = 8; /* row 0 lhs, col 0 bottom */
public const int TIFFTAG_SAMPLESPERPIXEL = 277; /* samples per pixel */
public const int TIFFTAG_ROWSPERSTRIP = 278; /* rows per strip of data */
public const int TIFFTAG_STRIPBYTECOUNTS = 279; /* bytes counts for strips */
public const int TIFFTAG_MINSAMPLEVALUE = 280; /* +minimum sample value */
public const int TIFFTAG_MAXSAMPLEVALUE = 281; /* +maximum sample value */
public const int TIFFTAG_XRESOLUTION = 282; /* pixels/resolution in x */
public const int TIFFTAG_YRESOLUTION = 283; /* pixels/resolution in y */
public const int TIFFTAG_PLANARCONFIG = 284; /* storage organization */
public const int PLANARCONFIG_CONTIG = 1; /* single image plane */
public const int PLANARCONFIG_SEPARATE = 2; /* separate planes of data */
public const int TIFFTAG_PAGENAME = 285; /* page name image is from */
public const int TIFFTAG_XPOSITION = 286; /* x page offset of image lhs */
public const int TIFFTAG_YPOSITION = 287; /* y page offset of image lhs */
public const int TIFFTAG_FREEOFFSETS = 288; /* +byte offset to free block */
public const int TIFFTAG_FREEBYTECOUNTS = 289; /* +sizes of free blocks */
public const int TIFFTAG_GRAYRESPONSEUNIT = 290; /* $gray scale curve accuracy */
public const int GRAYRESPONSEUNIT_10S = 1; /* tenths of a unit */
public const int GRAYRESPONSEUNIT_100S = 2; /* hundredths of a unit */
public const int GRAYRESPONSEUNIT_1000S = 3; /* thousandths of a unit */
public const int GRAYRESPONSEUNIT_10000S = 4; /* ten-thousandths of a unit */
public const int GRAYRESPONSEUNIT_100000S = 5; /* hundred-thousandths */
public const int TIFFTAG_GRAYRESPONSECURVE = 291; /* $gray scale response curve */
public const int TIFFTAG_GROUP3OPTIONS = 292; /* 32 flag bits */
public const int GROUP3OPT_2DENCODING = 0x1; /* 2-dimensional coding */
public const int GROUP3OPT_UNCOMPRESSED = 0x2; /* data not compressed */
public const int GROUP3OPT_FILLBITS = 0x4; /* fill to byte boundary */
public const int TIFFTAG_GROUP4OPTIONS = 293; /* 32 flag bits */
public const int GROUP4OPT_UNCOMPRESSED = 0x2; /* data not compressed */
public const int TIFFTAG_RESOLUTIONUNIT = 296; /* units of resolutions */
public const int RESUNIT_NONE = 1; /* no meaningful units */
public const int RESUNIT_INCH = 2; /* english */
public const int RESUNIT_CENTIMETER = 3; /* metric */
public const int TIFFTAG_PAGENUMBER = 297; /* page numbers of multi-page */
public const int TIFFTAG_COLORRESPONSEUNIT = 300; /* $color curve accuracy */
public const int COLORRESPONSEUNIT_10S = 1; /* tenths of a unit */
public const int COLORRESPONSEUNIT_100S = 2; /* hundredths of a unit */
public const int COLORRESPONSEUNIT_1000S = 3; /* thousandths of a unit */
public const int COLORRESPONSEUNIT_10000S = 4; /* ten-thousandths of a unit */
public const int COLORRESPONSEUNIT_100000S = 5; /* hundred-thousandths */
public const int TIFFTAG_TRANSFERFUNCTION = 301; /* !colorimetry info */
public const int TIFFTAG_SOFTWARE = 305; /* name & release */
public const int TIFFTAG_DATETIME = 306; /* creation date and time */
public const int TIFFTAG_ARTIST = 315; /* creator of image */
public const int TIFFTAG_HOSTCOMPUTER = 316; /* machine where created */
public const int TIFFTAG_PREDICTOR = 317; /* prediction scheme w/ LZW */
public const int TIFFTAG_WHITEPOINT = 318; /* image white point */
public const int TIFFTAG_PRIMARYCHROMATICITIES = 319; /* !primary chromaticities */
public const int TIFFTAG_COLORMAP = 320; /* RGB map for pallette image */
public const int TIFFTAG_HALFTONEHINTS = 321; /* !highlight+shadow info */
public const int TIFFTAG_TILEWIDTH = 322; /* !rows/data tile */
public const int TIFFTAG_TILELENGTH = 323; /* !cols/data tile */
public const int TIFFTAG_TILEOFFSETS = 324; /* !offsets to data tiles */
public const int TIFFTAG_TILEBYTECOUNTS = 325; /* !byte counts for tiles */
public const int TIFFTAG_BADFAXLINES = 326; /* lines w/ wrong pixel count */
public const int TIFFTAG_CLEANFAXDATA = 327; /* regenerated line info */
public const int CLEANFAXDATA_CLEAN = 0; /* no errors detected */
public const int CLEANFAXDATA_REGENERATED = 1; /* receiver regenerated lines */
public const int CLEANFAXDATA_UNCLEAN = 2; /* uncorrected errors exist */
public const int TIFFTAG_CONSECUTIVEBADFAXLINES = 328; /* max consecutive bad lines */
public const int TIFFTAG_SUBIFD = 330; /* subimage descriptors */
public const int TIFFTAG_INKSET = 332; /* !inks in separated image */
public const int INKSET_CMYK = 1; /* !cyan-magenta-yellow-black */
public const int TIFFTAG_INKNAMES = 333; /* !ascii names of inks */
public const int TIFFTAG_NUMBEROFINKS = 334; /* !number of inks */
public const int TIFFTAG_DOTRANGE = 336; /* !0% and 100% dot codes */
public const int TIFFTAG_TARGETPRINTER = 337; /* !separation target */
public const int TIFFTAG_EXTRASAMPLES = 338; /* !info about extra samples */
public const int EXTRASAMPLE_UNSPECIFIED = 0; /* !unspecified data */
public const int EXTRASAMPLE_ASSOCALPHA = 1; /* !associated alpha data */
public const int EXTRASAMPLE_UNASSALPHA = 2; /* !unassociated alpha data */
public const int TIFFTAG_SAMPLEFORMAT = 339; /* !data sample format */
public const int SAMPLEFORMAT_UINT = 1; /* !unsigned integer data */
public const int SAMPLEFORMAT_INT = 2; /* !signed integer data */
public const int SAMPLEFORMAT_IEEEFP = 3; /* !IEEE floating point data */
public const int SAMPLEFORMAT_VOID = 4; /* !untyped data */
public const int SAMPLEFORMAT_COMPLEXINT = 5; /* !complex signed int */
public const int SAMPLEFORMAT_COMPLEXIEEEFP = 6; /* !complex ieee floating */
public const int TIFFTAG_SMINSAMPLEVALUE = 340; /* !variable MinSampleValue */
public const int TIFFTAG_SMAXSAMPLEVALUE = 341; /* !variable MaxSampleValue */
public const int TIFFTAG_JPEGTABLES = 347; /* %JPEG table stream */
/*
* Tags 512-521 are obsoleted by Technical Note #2
* which specifies a revised JPEG-in-TIFF scheme.
*/
public const int TIFFTAG_JPEGPROC = 512; /* !JPEG processing algorithm */
public const int JPEGPROC_BASELINE = 1; /* !baseline sequential */
public const int JPEGPROC_LOSSLESS = 14; /* !Huffman coded lossless */
public const int TIFFTAG_JPEGIFOFFSET = 513; /* !pointer to SOI marker */
public const int TIFFTAG_JPEGIFBYTECOUNT = 514; /* !JFIF stream length */
public const int TIFFTAG_JPEGRESTARTINTERVAL = 515; /* !restart interval length */
public const int TIFFTAG_JPEGLOSSLESSPREDICTORS = 517; /* !lossless proc predictor */
public const int TIFFTAG_JPEGPOINTTRANSFORM = 518; /* !lossless point transform */
public const int TIFFTAG_JPEGQTABLES = 519; /* !Q matrice offsets */
public const int TIFFTAG_JPEGDCTABLES = 520; /* !DCT table offsets */
public const int TIFFTAG_JPEGACTABLES = 521; /* !AC coefficient offsets */
public const int TIFFTAG_YCBCRCOEFFICIENTS = 529; /* !RGB -> YCbCr transform */
public const int TIFFTAG_YCBCRSUBSAMPLING = 530; /* !YCbCr subsampling factors */
public const int TIFFTAG_YCBCRPOSITIONING = 531; /* !subsample positioning */
public const int YCBCRPOSITION_CENTERED = 1; /* !as in PostScript Level 2 */
public const int YCBCRPOSITION_COSITED = 2; /* !as in CCIR 601-1 */
public const int TIFFTAG_REFERENCEBLACKWHITE = 532; /* !colorimetry info */
/* tags 32952-32956 are private tags registered to Island Graphics */
public const int TIFFTAG_REFPTS = 32953; /* image reference points */
public const int TIFFTAG_REGIONTACKPOINT = 32954; /* region-xform tack point */
public const int TIFFTAG_REGIONWARPCORNERS = 32955; /* warp quadrilateral */
public const int TIFFTAG_REGIONAFFINE = 32956; /* affine transformation mat */
/* tags 32995-32999 are private tags registered to SGI */
public const int TIFFTAG_MATTEING = 32995; /* $use ExtraSamples */
public const int TIFFTAG_DATATYPE = 32996; /* $use SampleFormat */
public const int TIFFTAG_IMAGEDEPTH = 32997; /* z depth of image */
public const int TIFFTAG_TILEDEPTH = 32998; /* z depth/data tile */
/* tags 33300-33309 are private tags registered to Pixar */
/*
* TIFFTAG_PIXAR_IMAGEFULLWIDTH and TIFFTAG_PIXAR_IMAGEFULLLENGTH
* are set when an image has been cropped out of a larger image.
* They reflect the size of the original uncropped image.
* The TIFFTAG_XPOSITION and TIFFTAG_YPOSITION can be used
* to determine the position of the smaller image in the larger one.
*/
public const int TIFFTAG_PIXAR_IMAGEFULLWIDTH = 33300; /* full image size in x */
public const int TIFFTAG_PIXAR_IMAGEFULLLENGTH = 33301; /* full image size in y */
/* Tags 33302-33306 are used to identify special image modes and data
* used by Pixar's texture formats.
*/
public const int TIFFTAG_PIXAR_TEXTUREFORMAT = 33302; /* texture map format */
public const int TIFFTAG_PIXAR_WRAPMODES = 33303; /* s & t wrap modes */
public const int TIFFTAG_PIXAR_FOVCOT = 33304; /* cotan(fov) for env. maps */
public const int TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN = 33305;
public const int TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA = 33306;
/* tag 33405 is a private tag registered to Eastman Kodak */
public const int TIFFTAG_WRITERSERIALNUMBER = 33405; /* device serial number */
/* tag 33432 is listed in the 6.0 spec w/ unknown ownership */
public const int TIFFTAG_COPYRIGHT = 33432; /* copyright string */
/* IPTC TAG from RichTIFF specifications */
public const int TIFFTAG_RICHTIFFIPTC = 33723;
/* 34016-34029 are reserved for ANSI IT8 TIFF/IT <dkelly@etsinc.com) */
public const int TIFFTAG_IT8SITE = 34016; /* site name */
public const int TIFFTAG_IT8COLORSEQUENCE = 34017; /* color seq. [RGB,CMYK,etc] */
public const int TIFFTAG_IT8HEADER = 34018; /* DDES Header */
public const int TIFFTAG_IT8RASTERPADDING = 34019; /* raster scanline padding */
public const int TIFFTAG_IT8BITSPERRUNLENGTH = 34020; /* # of bits in short run */
public const int TIFFTAG_IT8BITSPEREXTENDEDRUNLENGTH = 34021;/* # of bits in long run */
public const int TIFFTAG_IT8COLORTABLE = 34022; /* LW colortable */
public const int TIFFTAG_IT8IMAGECOLORINDICATOR = 34023; /* BP/BL image color switch */
public const int TIFFTAG_IT8BKGCOLORINDICATOR = 34024; /* BP/BL bg color switch */
public const int TIFFTAG_IT8IMAGECOLORVALUE = 34025; /* BP/BL image color value */
public const int TIFFTAG_IT8BKGCOLORVALUE = 34026; /* BP/BL bg color value */
public const int TIFFTAG_IT8PIXELINTENSITYRANGE = 34027; /* MP pixel intensity value */
public const int TIFFTAG_IT8TRANSPARENCYINDICATOR = 34028; /* HC transparency switch */
public const int TIFFTAG_IT8COLORCHARACTERIZATION = 34029; /* color character. table */
/* tags 34232-34236 are private tags registered to Texas Instruments */
public const int TIFFTAG_FRAMECOUNT = 34232; /* Sequence Frame Count */
/* tag 34750 is a private tag registered to Adobe? */
public const int TIFFTAG_ICCPROFILE = 34675; /* ICC profile data */
/* tag 34377 is private tag registered to Adobe for PhotoShop */
public const int TIFFTAG_PHOTOSHOP = 34377;
/* tag 34750 is a private tag registered to Pixel Magic */
public const int TIFFTAG_JBIGOPTIONS = 34750; /* JBIG options */
/* tags 34908-34914 are private tags registered to SGI */
public const int TIFFTAG_FAXRECVPARAMS = 34908; /* encoded Class 2 ses. parms */
public const int TIFFTAG_FAXSUBADDRESS = 34909; /* received SubAddr string */
public const int TIFFTAG_FAXRECVTIME = 34910; /* receive time (secs) */
/* tags 37439-37443 are registered to SGI <gregl@sgi.com> */
public const int TIFFTAG_STONITS = 37439; /* Sample value to Nits */
/* tag 34929 is a private tag registered to FedEx */
public const int TIFFTAG_FEDEX_EDR = 34929; /* unknown use */
/* tag 65535 is an undefined tag used by Eastman Kodak */
public const int TIFFTAG_DCSHUESHIFTVALUES = 65535; /* hue shift correction data */
}
}

View File

@@ -0,0 +1,669 @@
using System;
using System.Collections;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
/*
* Copyright 2003-2008 by Paulo Soares.
*
* This code was originally released in 2001 by SUN (see class
* com.sun.media.imageio.plugins.tiff.TIFFDirectory.java)
* using the BSD license in a specific wording. In a mail dating from
* January 23, 2008, Brian Burkhalter (@sun.com) gave us permission
* to use the code under the following version of the BSD license:
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution 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.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for
* use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
namespace iTextSharp.text.pdf.codec {
/**
* A class representing an Image File Directory (IFD) from a TIFF 6.0
* stream. The TIFF file format is described in more detail in the
* comments for the TIFFDescriptor class.
*
* <p> A TIFF IFD consists of a set of TIFFField tags. Methods are
* provided to query the set of tags and to obtain the raw field
* array. In addition, convenience methods are provided for acquiring
* the values of tags that contain a single value that fits into a
* byte, int, long, float, or double.
*
* <p> Every TIFF file is made up of one or more public IFDs that are
* joined in a linked list, rooted in the file header. A file may
* also contain so-called private IFDs that are referenced from
* tag data and do not appear in the main list.
*
* <p><b> This class is not a committed part of the JAI API. It may
* be removed or changed in future releases of JAI.</b>
*
* @see TIFFField
*/
public class TIFFDirectory {
/** A bool storing the endianness of the stream. */
bool isBigEndian;
/** The number of entries in the IFD. */
int numEntries;
/** An array of TIFFFields. */
TIFFField[] fields;
/** A Hashtable indexing the fields by tag number. */
Hashtable fieldIndex = new Hashtable();
/** The offset of this IFD. */
long IFDOffset = 8;
/** The offset of the next IFD. */
long nextIFDOffset = 0;
/** The default constructor. */
TIFFDirectory() {}
private static bool IsValidEndianTag(int endian) {
return ((endian == 0x4949) || (endian == 0x4d4d));
}
/**
* Constructs a TIFFDirectory from a SeekableStream.
* The directory parameter specifies which directory to read from
* the linked list present in the stream; directory 0 is normally
* read but it is possible to store multiple images in a single
* TIFF file by maintaing multiple directories.
*
* @param stream a SeekableStream to read from.
* @param directory the index of the directory to read.
*/
public TIFFDirectory(RandomAccessFileOrArray stream, int directory)
{
long global_save_offset = stream.FilePointer;
long ifd_offset;
// Read the TIFF header
stream.Seek(0L);
int endian = stream.ReadUnsignedShort();
if (!IsValidEndianTag(endian)) {
throw new
ArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
}
isBigEndian = (endian == 0x4d4d);
int magic = ReadUnsignedShort(stream);
if (magic != 42) {
throw new
ArgumentException("Bad magic number, should be 42.");
}
// Get the initial ifd offset as an unsigned int (using a long)
ifd_offset = ReadUnsignedInt(stream);
for (int i = 0; i < directory; i++) {
if (ifd_offset == 0L) {
throw new
ArgumentException("Directory number too large.");
}
stream.Seek(ifd_offset);
int entries = ReadUnsignedShort(stream);
stream.Skip(12*entries);
ifd_offset = ReadUnsignedInt(stream);
}
stream.Seek(ifd_offset);
Initialize(stream);
stream.Seek(global_save_offset);
}
/**
* Constructs a TIFFDirectory by reading a SeekableStream.
* The ifd_offset parameter specifies the stream offset from which
* to begin reading; this mechanism is sometimes used to store
* private IFDs within a TIFF file that are not part of the normal
* sequence of IFDs.
*
* @param stream a SeekableStream to read from.
* @param ifd_offset the long byte offset of the directory.
* @param directory the index of the directory to read beyond the
* one at the current stream offset; zero indicates the IFD
* at the current offset.
*/
public TIFFDirectory(RandomAccessFileOrArray stream, long ifd_offset, int directory)
{
long global_save_offset = stream.FilePointer;
stream.Seek(0L);
int endian = stream.ReadUnsignedShort();
if (!IsValidEndianTag(endian)) {
throw new
ArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
}
isBigEndian = (endian == 0x4d4d);
// Seek to the first IFD.
stream.Seek(ifd_offset);
// Seek to desired IFD if necessary.
int dirNum = 0;
while (dirNum < directory) {
// Get the number of fields in the current IFD.
int numEntries = ReadUnsignedShort(stream);
// Skip to the next IFD offset value field.
stream.Seek(ifd_offset + 12*numEntries);
// Read the offset to the next IFD beyond this one.
ifd_offset = ReadUnsignedInt(stream);
// Seek to the next IFD.
stream.Seek(ifd_offset);
// Increment the directory.
dirNum++;
}
Initialize(stream);
stream.Seek(global_save_offset);
}
private static int[] sizeOfType = {
0, // 0 = n/a
1, // 1 = byte
1, // 2 = ascii
2, // 3 = short
4, // 4 = long
8, // 5 = rational
1, // 6 = sbyte
1, // 7 = undefined
2, // 8 = sshort
4, // 9 = slong
8, // 10 = srational
4, // 11 = float
8 // 12 = double
};
private void Initialize(RandomAccessFileOrArray stream) {
long nextTagOffset = 0L;
long maxOffset = (long) stream.Length;
int i, j;
IFDOffset = stream.FilePointer;
numEntries = ReadUnsignedShort(stream);
fields = new TIFFField[numEntries];
for (i = 0; (i < numEntries) && (nextTagOffset < maxOffset); i++) {
int tag = ReadUnsignedShort(stream);
int type = ReadUnsignedShort(stream);
int count = (int)(ReadUnsignedInt(stream));
bool processTag = true;
// The place to return to to read the next tag
nextTagOffset = stream.FilePointer + 4;
try {
// If the tag data can't fit in 4 bytes, the next 4 bytes
// contain the starting offset of the data
if (count*sizeOfType[type] > 4) {
long valueOffset = ReadUnsignedInt(stream);
// bounds check offset for EOF
if (valueOffset < maxOffset) {
stream.Seek(valueOffset);
}
else {
// bad offset pointer .. skip tag
processTag = false;
}
}
} catch (ArgumentOutOfRangeException) {
// if the data type is unknown we should skip this TIFF Field
processTag = false;
}
if (processTag) {
fieldIndex[tag] = i;
Object obj = null;
switch (type) {
case TIFFField.TIFF_BYTE:
case TIFFField.TIFF_SBYTE:
case TIFFField.TIFF_UNDEFINED:
case TIFFField.TIFF_ASCII:
byte[] bvalues = new byte[count];
stream.ReadFully(bvalues, 0, count);
if (type == TIFFField.TIFF_ASCII) {
// Can be multiple strings
int index = 0, prevIndex = 0;
ArrayList v = new ArrayList();
while (index < count) {
while ((index < count) && (bvalues[index++] != 0));
// When we encountered zero, means one string has ended
char[] cht = new char[index - prevIndex];
Array.Copy(bvalues, prevIndex, cht, 0, index - prevIndex);
v.Add(new String(cht));
prevIndex = index;
}
count = v.Count;
String[] strings = new String[count];
for (int c = 0 ; c < count; c++) {
strings[c] = (String)v[c];
}
obj = strings;
} else {
obj = bvalues;
}
break;
case TIFFField.TIFF_SHORT:
char[] cvalues = new char[count];
for (j = 0; j < count; j++) {
cvalues[j] = (char)(ReadUnsignedShort(stream));
}
obj = cvalues;
break;
case TIFFField.TIFF_LONG:
long[] lvalues = new long[count];
for (j = 0; j < count; j++) {
lvalues[j] = ReadUnsignedInt(stream);
}
obj = lvalues;
break;
case TIFFField.TIFF_RATIONAL:
long[][] llvalues = new long[count][];
for (j = 0; j < count; j++) {
long v0 = ReadUnsignedInt(stream);
long v1 = ReadUnsignedInt(stream);
llvalues[j] = new long[]{v0, v1};
}
obj = llvalues;
break;
case TIFFField.TIFF_SSHORT:
short[] svalues = new short[count];
for (j = 0; j < count; j++) {
svalues[j] = ReadShort(stream);
}
obj = svalues;
break;
case TIFFField.TIFF_SLONG:
int[] ivalues = new int[count];
for (j = 0; j < count; j++) {
ivalues[j] = ReadInt(stream);
}
obj = ivalues;
break;
case TIFFField.TIFF_SRATIONAL:
int[,] iivalues = new int[count,2];
for (j = 0; j < count; j++) {
iivalues[j,0] = ReadInt(stream);
iivalues[j,1] = ReadInt(stream);
}
obj = iivalues;
break;
case TIFFField.TIFF_FLOAT:
float[] fvalues = new float[count];
for (j = 0; j < count; j++) {
fvalues[j] = ReadFloat(stream);
}
obj = fvalues;
break;
case TIFFField.TIFF_DOUBLE:
double[] dvalues = new double[count];
for (j = 0; j < count; j++) {
dvalues[j] = ReadDouble(stream);
}
obj = dvalues;
break;
default:
break;
}
fields[i] = new TIFFField(tag, type, count, obj);
}
stream.Seek(nextTagOffset);
}
// Read the offset of the next IFD.
try {
nextIFDOffset = ReadUnsignedInt(stream);
}
catch {
// broken tiffs may not have this pointer
nextIFDOffset = 0;
}
}
/** Returns the number of directory entries. */
public int GetNumEntries() {
return numEntries;
}
/**
* Returns the value of a given tag as a TIFFField,
* or null if the tag is not present.
*/
public TIFFField GetField(int tag) {
object i = fieldIndex[tag];
if (i == null) {
return null;
} else {
return fields[(int)i];
}
}
/**
* Returns true if a tag appears in the directory.
*/
public bool IsTagPresent(int tag) {
return fieldIndex.ContainsKey(tag);
}
/**
* Returns an ordered array of ints indicating the tag
* values.
*/
public int[] GetTags() {
int[] tags = new int[fieldIndex.Count];
fieldIndex.Keys.CopyTo(tags, 0);
return tags;
}
/**
* Returns an array of TIFFFields containing all the fields
* in this directory.
*/
public TIFFField[] GetFields() {
return fields;
}
/**
* Returns the value of a particular index of a given tag as a
* byte. The caller is responsible for ensuring that the tag is
* present and has type TIFFField.TIFF_SBYTE, TIFF_BYTE, or
* TIFF_UNDEFINED.
*/
public byte GetFieldAsByte(int tag, int index) {
int i = (int)fieldIndex[tag];
byte [] b = (fields[(int)i]).GetAsBytes();
return b[index];
}
/**
* Returns the value of index 0 of a given tag as a
* byte. The caller is responsible for ensuring that the tag is
* present and has type TIFFField.TIFF_SBYTE, TIFF_BYTE, or
* TIFF_UNDEFINED.
*/
public byte GetFieldAsByte(int tag) {
return GetFieldAsByte(tag, 0);
}
/**
* Returns the value of a particular index of a given tag as a
* long. The caller is responsible for ensuring that the tag is
* present and has type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED,
* TIFF_SHORT, TIFF_SSHORT, TIFF_SLONG or TIFF_LONG.
*/
public long GetFieldAsLong(int tag, int index) {
int i = (int)fieldIndex[tag];
return (fields[i]).GetAsLong(index);
}
/**
* Returns the value of index 0 of a given tag as a
* long. The caller is responsible for ensuring that the tag is
* present and has type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED,
* TIFF_SHORT, TIFF_SSHORT, TIFF_SLONG or TIFF_LONG.
*/
public long GetFieldAsLong(int tag) {
return GetFieldAsLong(tag, 0);
}
/**
* Returns the value of a particular index of a given tag as a
* float. The caller is responsible for ensuring that the tag is
* present and has numeric type (all but TIFF_UNDEFINED and
* TIFF_ASCII).
*/
public float GetFieldAsFloat(int tag, int index) {
int i = (int)fieldIndex[tag];
return fields[i].GetAsFloat(index);
}
/**
* Returns the value of index 0 of a given tag as a float. The
* caller is responsible for ensuring that the tag is present and
* has numeric type (all but TIFF_UNDEFINED and TIFF_ASCII).
*/
public float GetFieldAsFloat(int tag) {
return GetFieldAsFloat(tag, 0);
}
/**
* Returns the value of a particular index of a given tag as a
* double. The caller is responsible for ensuring that the tag is
* present and has numeric type (all but TIFF_UNDEFINED and
* TIFF_ASCII).
*/
public double GetFieldAsDouble(int tag, int index) {
int i = (int)fieldIndex[tag];
return fields[i].GetAsDouble(index);
}
/**
* Returns the value of index 0 of a given tag as a double. The
* caller is responsible for ensuring that the tag is present and
* has numeric type (all but TIFF_UNDEFINED and TIFF_ASCII).
*/
public double GetFieldAsDouble(int tag) {
return GetFieldAsDouble(tag, 0);
}
// Methods to read primitive data types from the stream
private short ReadShort(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadShort();
} else {
return stream.ReadShortLE();
}
}
private int ReadUnsignedShort(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadUnsignedShort();
} else {
return stream.ReadUnsignedShortLE();
}
}
private int ReadInt(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadInt();
} else {
return stream.ReadIntLE();
}
}
private long ReadUnsignedInt(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadUnsignedInt();
} else {
return stream.ReadUnsignedIntLE();
}
}
private long ReadLong(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadLong();
} else {
return stream.ReadLongLE();
}
}
private float ReadFloat(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadFloat();
} else {
return stream.ReadFloatLE();
}
}
private double ReadDouble(RandomAccessFileOrArray stream)
{
if (isBigEndian) {
return stream.ReadDouble();
} else {
return stream.ReadDoubleLE();
}
}
private static int ReadUnsignedShort(RandomAccessFileOrArray stream,
bool isBigEndian)
{
if (isBigEndian) {
return stream.ReadUnsignedShort();
} else {
return stream.ReadUnsignedShortLE();
}
}
private static long ReadUnsignedInt(RandomAccessFileOrArray stream,
bool isBigEndian)
{
if (isBigEndian) {
return stream.ReadUnsignedInt();
} else {
return stream.ReadUnsignedIntLE();
}
}
// Utilities
/**
* Returns the number of image directories (subimages) stored in a
* given TIFF file, represented by a <code>SeekableStream</code>.
*/
public static int GetNumDirectories(RandomAccessFileOrArray stream)
{
long pointer = stream.FilePointer; // Save stream pointer
stream.Seek(0L);
int endian = stream.ReadUnsignedShort();
if (!IsValidEndianTag(endian)) {
throw new ArgumentException("Bad endianness tag (not 0x4949 or 0x4d4d).");
}
bool isBigEndian = (endian == 0x4d4d);
int magic = ReadUnsignedShort(stream, isBigEndian);
if (magic != 42) {
throw new
ArgumentException("Bad magic number, should be 42.");
}
stream.Seek(4L);
long offset = ReadUnsignedInt(stream, isBigEndian);
int numDirectories = 0;
while (offset != 0L) {
++numDirectories;
// EOFException means IFD was probably not properly terminated.
try {
stream.Seek(offset);
int entries = ReadUnsignedShort(stream, isBigEndian);
stream.Skip(12*entries);
offset = ReadUnsignedInt(stream, isBigEndian);
} catch (EndOfStreamException) {
//numDirectories--;
break;
}
}
stream.Seek(pointer); // Reset stream pointer
return numDirectories;
}
/**
* Returns a bool indicating whether the byte order used in the
* the TIFF file is big-endian (i.e. whether the byte order is from
* the most significant to the least significant)
*/
public bool IsBigEndian() {
return isBigEndian;
}
/**
* Returns the offset of the IFD corresponding to this
* <code>TIFFDirectory</code>.
*/
public long GetIFDOffset() {
return IFDOffset;
}
/**
* Returns the offset of the next IFD after the IFD corresponding to this
* <code>TIFFDirectory</code>.
*/
public long GetNextIFDOffset() {
return nextIFDOffset;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,487 @@
using System;
/*
* Copyright 2003-2008 by Paulo Soares.
*
* This code was originally released in 2001 by SUN (see class
* com.sun.media.imageio.plugins.tiff.TIFFField.java)
* using the BSD license in a specific wording. In a mail dating from
* January 23, 2008, Brian Burkhalter (@sun.com) gave us permission
* to use the code under the following version of the BSD license:
*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution 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.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for
* use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
namespace iTextSharp.text.pdf.codec {
/**
* A class representing a field in a TIFF 6.0 Image File Directory.
*
* <p> The TIFF file format is described in more detail in the
* comments for the TIFFDescriptor class.
*
* <p> A field in a TIFF Image File Directory (IFD). A field is defined
* as a sequence of values of identical data type. TIFF 6.0 defines
* 12 data types, which are mapped internally onto the Java datatypes
* byte, int, long, float, and double.
*
* <p><b> This class is not a committed part of the JAI API. It may
* be removed or changed in future releases of JAI.</b>
*
* @see TIFFDirectory
*/
public class TIFFField : IComparable {
/** Flag for 8 bit unsigned integers. */
public const int TIFF_BYTE = 1;
/** Flag for null-terminated ASCII strings. */
public const int TIFF_ASCII = 2;
/** Flag for 16 bit unsigned integers. */
public const int TIFF_SHORT = 3;
/** Flag for 32 bit unsigned integers. */
public const int TIFF_LONG = 4;
/** Flag for pairs of 32 bit unsigned integers. */
public const int TIFF_RATIONAL = 5;
/** Flag for 8 bit signed integers. */
public const int TIFF_SBYTE = 6;
/** Flag for 8 bit uninterpreted bytes. */
public const int TIFF_UNDEFINED = 7;
/** Flag for 16 bit signed integers. */
public const int TIFF_SSHORT = 8;
/** Flag for 32 bit signed integers. */
public const int TIFF_SLONG = 9;
/** Flag for pairs of 32 bit signed integers. */
public const int TIFF_SRATIONAL = 10;
/** Flag for 32 bit IEEE floats. */
public const int TIFF_FLOAT = 11;
/** Flag for 64 bit IEEE doubles. */
public const int TIFF_DOUBLE = 12;
/** The tag number. */
int tag;
/** The tag type. */
int type;
/** The number of data items present in the field. */
int count;
/** The field data. */
Object data;
/** The default constructor. */
internal TIFFField() {}
/**
* Constructs a TIFFField with arbitrary data. The data
* parameter must be an array of a Java type appropriate for the
* type of the TIFF field. Since there is no available 32-bit
* unsigned datatype, long is used. The mapping between types is
* as follows:
*
* <table border=1>
* <tr>
* <th> TIFF type </th> <th> Java type </th>
* <tr>
* <td><tt>TIFF_BYTE</tt></td> <td><tt>byte</tt></td>
* <tr>
* <td><tt>TIFF_ASCII</tt></td> <td><tt>String</tt></td>
* <tr>
* <td><tt>TIFF_SHORT</tt></td> <td><tt>char</tt></td>
* <tr>
* <td><tt>TIFF_LONG</tt></td> <td><tt>long</tt></td>
* <tr>
* <td><tt>TIFF_RATIONAL</tt></td> <td><tt>long[2]</tt></td>
* <tr>
* <td><tt>TIFF_SBYTE</tt></td> <td><tt>byte</tt></td>
* <tr>
* <td><tt>TIFF_UNDEFINED</tt></td> <td><tt>byte</tt></td>
* <tr>
* <td><tt>TIFF_SSHORT</tt></td> <td><tt>short</tt></td>
* <tr>
* <td><tt>TIFF_SLONG</tt></td> <td><tt>int</tt></td>
* <tr>
* <td><tt>TIFF_SRATIONAL</tt></td> <td><tt>int[2]</tt></td>
* <tr>
* <td><tt>TIFF_FLOAT</tt></td> <td><tt>float</tt></td>
* <tr>
* <td><tt>TIFF_DOUBLE</tt></td> <td><tt>double</tt></td>
* </table>
*/
public TIFFField(int tag, int type, int count, Object data) {
this.tag = tag;
this.type = type;
this.count = count;
this.data = data;
}
/**
* Returns the tag number, between 0 and 65535.
*/
public int GetTag() {
return tag;
}
/**
* Returns the type of the data stored in the IFD.
* For a TIFF6.0 file, the value will equal one of the
* TIFF_ constants defined in this class. For future
* revisions of TIFF, higher values are possible.
*
*/
public new int GetType() {
return type;
}
/**
* Returns the number of elements in the IFD.
*/
public int GetCount() {
return count;
}
/**
* Returns the data as an uninterpreted array of bytes.
* The type of the field must be one of TIFF_BYTE, TIFF_SBYTE,
* or TIFF_UNDEFINED;
*
* <p> For data in TIFF_BYTE format, the application must take
* care when promoting the data to longer integral types
* to avoid sign extension.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_BYTE, TIFF_SBYTE, or TIFF_UNDEFINED.
*/
public byte[] GetAsBytes() {
return (byte[])data;
}
/**
* Returns TIFF_SHORT data as an array of chars (unsigned 16-bit
* integers).
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_SHORT.
*/
public char[] GetAsChars() {
return (char[])data;
}
/**
* Returns TIFF_SSHORT data as an array of shorts (signed 16-bit
* integers).
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_SSHORT.
*/
public short[] GetAsShorts() {
return (short[])data;
}
/**
* Returns TIFF_SLONG data as an array of ints (signed 32-bit
* integers).
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_SLONG.
*/
public int[] GetAsInts() {
return (int[])data;
}
/**
* Returns TIFF_LONG data as an array of longs (signed 64-bit
* integers).
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_LONG.
*/
public long[] GetAsLongs() {
return (long[])data;
}
/**
* Returns TIFF_FLOAT data as an array of floats.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_FLOAT.
*/
public float[] GetAsFloats() {
return (float[])data;
}
/**
* Returns TIFF_DOUBLE data as an array of doubles.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_DOUBLE.
*/
public double[] GetAsDoubles() {
return (double[])data;
}
/**
* Returns TIFF_SRATIONAL data as an array of 2-element arrays of ints.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_SRATIONAL.
*/
public int[][] GetAsSRationals() {
return (int[][])data;
}
/**
* Returns TIFF_RATIONAL data as an array of 2-element arrays of longs.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_RATTIONAL.
*/
public long[][] GetAsRationals() {
return (long[][])data;
}
/**
* Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
* TIFF_SSHORT, or TIFF_SLONG format as an int.
*
* <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned;
* that is, no sign extension will take place and the returned
* value will be in the range [0, 255]. TIFF_SBYTE data will
* be returned in the range [-128, 127].
*
* <p> A ClassCastException will be thrown if the field is not of
* type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
* TIFF_SSHORT, or TIFF_SLONG.
*/
public int GetAsInt(int index) {
switch (type) {
case TIFF_BYTE: case TIFF_UNDEFINED:
return ((byte[])data)[index] & 0xff;
case TIFF_SBYTE:
return ((byte[])data)[index];
case TIFF_SHORT:
return ((char[])data)[index] & 0xffff;
case TIFF_SSHORT:
return ((short[])data)[index];
case TIFF_SLONG:
return ((int[])data)[index];
default:
throw new InvalidCastException();
}
}
/**
* Returns data in TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
* TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG format as a long.
*
* <p> TIFF_BYTE and TIFF_UNDEFINED data are treated as unsigned;
* that is, no sign extension will take place and the returned
* value will be in the range [0, 255]. TIFF_SBYTE data will
* be returned in the range [-128, 127].
*
* <p> A ClassCastException will be thrown if the field is not of
* type TIFF_BYTE, TIFF_SBYTE, TIFF_UNDEFINED, TIFF_SHORT,
* TIFF_SSHORT, TIFF_SLONG, or TIFF_LONG.
*/
public long GetAsLong(int index) {
switch (type) {
case TIFF_BYTE: case TIFF_UNDEFINED:
return ((byte[])data)[index] & 0xff;
case TIFF_SBYTE:
return ((byte[])data)[index];
case TIFF_SHORT:
return ((char[])data)[index] & 0xffff;
case TIFF_SSHORT:
return ((short[])data)[index];
case TIFF_SLONG:
return ((int[])data)[index];
case TIFF_LONG:
return ((long[])data)[index];
default:
throw new InvalidCastException();
}
}
/**
* Returns data in any numerical format as a float. Data in
* TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by
* dividing the numerator into the denominator using
* double-precision arithmetic and then truncating to single
* precision. Data in TIFF_SLONG, TIFF_LONG, or TIFF_DOUBLE
* format may suffer from truncation.
*
* <p> A ClassCastException will be thrown if the field is
* of type TIFF_UNDEFINED or TIFF_ASCII.
*/
public float GetAsFloat(int index) {
switch (type) {
case TIFF_BYTE:
return ((byte[])data)[index] & 0xff;
case TIFF_SBYTE:
return ((byte[])data)[index];
case TIFF_SHORT:
return ((char[])data)[index] & 0xffff;
case TIFF_SSHORT:
return ((short[])data)[index];
case TIFF_SLONG:
return ((int[])data)[index];
case TIFF_LONG:
return ((long[])data)[index];
case TIFF_FLOAT:
return ((float[])data)[index];
case TIFF_DOUBLE:
return (float)((double[])data)[index];
case TIFF_SRATIONAL:
int[] ivalue = GetAsSRational(index);
return (float)((double)ivalue[0]/ivalue[1]);
case TIFF_RATIONAL:
long[] lvalue = GetAsRational(index);
return (float)((double)lvalue[0]/lvalue[1]);
default:
throw new InvalidCastException();
}
}
/**
* Returns data in any numerical format as a float. Data in
* TIFF_SRATIONAL or TIFF_RATIONAL format are evaluated by
* dividing the numerator into the denominator using
* double-precision arithmetic.
*
* <p> A ClassCastException will be thrown if the field is of
* type TIFF_UNDEFINED or TIFF_ASCII.
*/
public double GetAsDouble(int index) {
switch (type) {
case TIFF_BYTE:
return ((byte[])data)[index] & 0xff;
case TIFF_SBYTE:
return ((byte[])data)[index];
case TIFF_SHORT:
return ((char[])data)[index] & 0xffff;
case TIFF_SSHORT:
return ((short[])data)[index];
case TIFF_SLONG:
return ((int[])data)[index];
case TIFF_LONG:
return ((long[])data)[index];
case TIFF_FLOAT:
return ((float[])data)[index];
case TIFF_DOUBLE:
return ((double[])data)[index];
case TIFF_SRATIONAL:
int[] ivalue = GetAsSRational(index);
return (double)ivalue[0]/ivalue[1];
case TIFF_RATIONAL:
long[] lvalue = GetAsRational(index);
return (double)lvalue[0]/lvalue[1];
default:
throw new InvalidCastException();
}
}
/**
* Returns a TIFF_ASCII data item as a String.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_ASCII.
*/
public String GetAsString(int index) {
return ((String[])data)[index];
}
/**
* Returns a TIFF_SRATIONAL data item as a two-element array
* of ints.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_SRATIONAL.
*/
public int[] GetAsSRational(int index) {
return ((int[][])data)[index];
}
/**
* Returns a TIFF_RATIONAL data item as a two-element array
* of ints.
*
* <p> A ClassCastException will be thrown if the field is not
* of type TIFF_RATIONAL.
*/
public long[] GetAsRational(int index) {
if (type == TIFF_LONG)
return GetAsLongs();
return ((long[][])data)[index];
}
/**
* Compares this <code>TIFFField</code> with another
* <code>TIFFField</code> by comparing the tags.
*
* <p><b>Note: this class has a natural ordering that is inconsistent
* with <code>equals()</code>.</b>
*
* @throws IllegalArgumentException if the parameter is <code>null</code>.
* @throws ClassCastException if the parameter is not a
* <code>TIFFField</code>.
*/
public int CompareTo(Object o) {
if (o == null) {
throw new ArgumentException();
}
int oTag = ((TIFFField)o).GetTag();
if (tag < oTag) {
return -1;
} else if (tag > oTag) {
return 1;
} else {
return 0;
}
}
}
}

View File

@@ -0,0 +1,272 @@
using System;
/*
* Copyright 2003-2008 by Paulo Soares.
*
* This code was originally released in 2001 by SUN (see class
* com.sun.media.imageioimpl.plugins.tiff.TIFFLZWDecompressor.java)
* using the BSD license in a specific wording. In a mail dating from
* January 23, 2008, Brian Burkhalter (@sun.com) gave us permission
* to use the code under the following version of the BSD license:
*
* Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution 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.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for
* use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
namespace iTextSharp.text.pdf.codec {
/**
* A class for performing LZW decoding.
*
*
*/
public class TIFFLZWDecoder {
byte[][] stringTable;
byte[] data = null;
byte[] uncompData;
int tableIndex, bitsToGet = 9;
int bytePointer;
int dstIndex;
int w, h;
int predictor, samplesPerPixel;
int nextData = 0;
int nextBits = 0;
int[] andTable = {
511,
1023,
2047,
4095
};
public TIFFLZWDecoder(int w, int predictor, int samplesPerPixel) {
this.w = w;
this.predictor = predictor;
this.samplesPerPixel = samplesPerPixel;
}
/**
* Method to decode LZW compressed data.
*
* @param data The compressed data.
* @param uncompData Array to return the uncompressed data in.
* @param h The number of rows the compressed data contains.
*/
public byte[] Decode(byte[] data, byte[] uncompData, int h) {
if (data[0] == (byte)0x00 && data[1] == (byte)0x01) {
throw new InvalidOperationException("TIFF 5.0-style LZW codes are not supported.");
}
InitializeStringTable();
this.data = data;
this.h = h;
this.uncompData = uncompData;
// Initialize pointers
bytePointer = 0;
dstIndex = 0;
nextData = 0;
nextBits = 0;
int code, oldCode = 0;
byte[] strn;
while ( ((code = GetNextCode()) != 257) &&
dstIndex < uncompData.Length) {
if (code == 256) {
InitializeStringTable();
code = GetNextCode();
if (code == 257) {
break;
}
WriteString(stringTable[code]);
oldCode = code;
} else {
if (code < tableIndex) {
strn = stringTable[code];
WriteString(strn);
AddStringToTable(stringTable[oldCode], strn[0]);
oldCode = code;
} else {
strn = stringTable[oldCode];
strn = ComposeString(strn, strn[0]);
WriteString(strn);
AddStringToTable(strn);
oldCode = code;
}
}
}
// Horizontal Differencing Predictor
if (predictor == 2) {
int count;
for (int j = 0; j < h; j++) {
count = samplesPerPixel * (j * w + 1);
for (int i = samplesPerPixel; i < w * samplesPerPixel; i++) {
uncompData[count] += uncompData[count - samplesPerPixel];
count++;
}
}
}
return uncompData;
}
/**
* Initialize the string table.
*/
public void InitializeStringTable() {
stringTable = new byte[4096][];
for (int i=0; i<256; i++) {
stringTable[i] = new byte[1];
stringTable[i][0] = (byte)i;
}
tableIndex = 258;
bitsToGet = 9;
}
/**
* Write out the string just uncompressed.
*/
public void WriteString(byte[] strn) {
// Fix for broken tiff files
int max = uncompData.Length - dstIndex;
if (strn.Length < max)
max = strn.Length;
System.Array.Copy(strn, 0, uncompData, dstIndex, max);
dstIndex += max;
}
/**
* Add a new string to the string table.
*/
public void AddStringToTable(byte[] oldString, byte newString) {
int length = oldString.Length;
byte[] strn = new byte[length + 1];
Array.Copy(oldString, 0, strn, 0, length);
strn[length] = newString;
// Add this new String to the table
stringTable[tableIndex++] = strn;
if (tableIndex == 511) {
bitsToGet = 10;
} else if (tableIndex == 1023) {
bitsToGet = 11;
} else if (tableIndex == 2047) {
bitsToGet = 12;
}
}
/**
* Add a new string to the string table.
*/
public void AddStringToTable(byte[] strn) {
// Add this new String to the table
stringTable[tableIndex++] = strn;
if (tableIndex == 511) {
bitsToGet = 10;
} else if (tableIndex == 1023) {
bitsToGet = 11;
} else if (tableIndex == 2047) {
bitsToGet = 12;
}
}
/**
* Append <code>newString</code> to the end of <code>oldString</code>.
*/
public byte[] ComposeString(byte[] oldString, byte newString) {
int length = oldString.Length;
byte[] strn = new byte[length + 1];
Array.Copy(oldString, 0, strn, 0, length);
strn[length] = newString;
return strn;
}
// Returns the next 9, 10, 11 or 12 bits
public int GetNextCode() {
// Attempt to get the next code. The exception is caught to make
// this robust to cases wherein the EndOfInformation code has been
// omitted from a strip. Examples of such cases have been observed
// in practice.
try {
nextData = (nextData << 8) | (data[bytePointer++] & 0xff);
nextBits += 8;
if (nextBits < bitsToGet) {
nextData = (nextData << 8) | (data[bytePointer++] & 0xff);
nextBits += 8;
}
int code =
(nextData >> (nextBits - bitsToGet)) & andTable[bitsToGet-9];
nextBits -= bitsToGet;
return code;
} catch (IndexOutOfRangeException) {
// Strip not terminated as expected: return EndOfInformation code.
return 257;
}
}
}
}

View File

@@ -0,0 +1,546 @@
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.util.zlib;
/*
* Copyright 2003 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec {
/** Reads TIFF images
* @author Paulo Soares (psoares@consiste.pt)
*/
public class TiffImage {
/** Gets the number of pages the TIFF document has.
* @param s the file source
* @return the number of pages
*/
public static int GetNumberOfPages(RandomAccessFileOrArray s) {
return TIFFDirectory.GetNumDirectories(s);
}
static int GetDpi(TIFFField fd, int resolutionUnit) {
if (fd == null)
return 0;
long[] res = fd.GetAsRational(0);
float frac = (float)res[0] / (float)res[1];
int dpi = 0;
switch (resolutionUnit) {
case TIFFConstants.RESUNIT_INCH:
case TIFFConstants.RESUNIT_NONE:
dpi = (int)(frac + 0.5);
break;
case TIFFConstants.RESUNIT_CENTIMETER:
dpi = (int)(frac * 2.54 + 0.5);
break;
}
return dpi;
}
/** Reads a page from a TIFF image. Direct mode is not used.
* @param s the file source
* @param page the page to get. The first page is 1
* @return the <CODE>Image</CODE>
*/
public static Image GetTiffImage(RandomAccessFileOrArray s, int page) {
return GetTiffImage(s, page, false);
}
/** Reads a page from a TIFF image.
* @param s the file source
* @param page the page to get. The first page is 1
* @param direct for single strip, CCITT images, generate the image
* by direct byte copying. It's faster but may not work
* every time
* @return the <CODE>Image</CODE>
*/
public static Image GetTiffImage(RandomAccessFileOrArray s, int page, bool direct) {
if (page < 1)
throw new ArgumentException("The page number must be >= 1.");
TIFFDirectory dir = new TIFFDirectory(s, page - 1);
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_TILEWIDTH))
throw new ArgumentException("Tiles are not supported.");
int compression = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_COMPRESSION);
switch (compression) {
case TIFFConstants.COMPRESSION_CCITTRLEW:
case TIFFConstants.COMPRESSION_CCITTRLE:
case TIFFConstants.COMPRESSION_CCITTFAX3:
case TIFFConstants.COMPRESSION_CCITTFAX4:
break;
default:
return GetTiffImageColor(dir, s);
}
float rotation = 0;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ORIENTATION)) {
int rot = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_ORIENTATION);
if (rot == TIFFConstants.ORIENTATION_BOTRIGHT || rot == TIFFConstants.ORIENTATION_BOTLEFT)
rotation = (float)Math.PI;
else if (rot == TIFFConstants.ORIENTATION_LEFTTOP || rot == TIFFConstants.ORIENTATION_LEFTBOT)
rotation = (float)(Math.PI / 2.0);
else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIENTATION_RIGHTBOT)
rotation = -(float)(Math.PI / 2.0);
}
Image img = null;
long tiffT4Options = 0;
long tiffT6Options = 0;
int fillOrder = 1;
int h = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH);
int w = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH);
int dpiX = 0;
int dpiY = 0;
float XYRatio = 0;
int resolutionUnit = TIFFConstants.RESUNIT_INCH;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_RESOLUTIONUNIT))
resolutionUnit = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_RESOLUTIONUNIT);
dpiX = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
dpiY = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
if (resolutionUnit == TIFFConstants.RESUNIT_NONE) {
if (dpiY != 0)
XYRatio = (float)dpiX / (float)dpiY;
dpiX = 0;
dpiY = 0;
}
int rowsStrip = h;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ROWSPERSTRIP))
rowsStrip = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP);
if (rowsStrip <= 0 || rowsStrip > h)
rowsStrip = h;
long[] offset = GetArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPOFFSETS);
long[] size = GetArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPBYTECOUNTS);
if ((size == null || (size.Length == 1 && (size[0] == 0 || size[0] + offset[0] > s.Length))) && h == rowsStrip) { // some TIFF producers are really lousy, so...
size = new long[]{s.Length - (int)offset[0]};
}
bool reverse = false;
TIFFField fillOrderField = dir.GetField(TIFFConstants.TIFFTAG_FILLORDER);
if (fillOrderField != null)
fillOrder = fillOrderField.GetAsInt(0);
reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB);
int paramsn = 0;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_PHOTOMETRIC)) {
long photo = dir.GetFieldAsLong(TIFFConstants.TIFFTAG_PHOTOMETRIC);
if (photo == TIFFConstants.PHOTOMETRIC_MINISBLACK)
paramsn |= Image.CCITT_BLACKIS1;
}
int imagecomp = 0;
switch (compression) {
case TIFFConstants.COMPRESSION_CCITTRLEW:
case TIFFConstants.COMPRESSION_CCITTRLE:
imagecomp = Image.CCITTG3_1D;
paramsn |= Image.CCITT_ENCODEDBYTEALIGN | Image.CCITT_ENDOFBLOCK;
break;
case TIFFConstants.COMPRESSION_CCITTFAX3:
imagecomp = Image.CCITTG3_1D;
paramsn |= Image.CCITT_ENDOFLINE | Image.CCITT_ENDOFBLOCK;
TIFFField t4OptionsField = dir.GetField(TIFFConstants.TIFFTAG_GROUP3OPTIONS);
if (t4OptionsField != null) {
tiffT4Options = t4OptionsField.GetAsLong(0);
if ((tiffT4Options & TIFFConstants.GROUP3OPT_2DENCODING) != 0)
imagecomp = Image.CCITTG3_2D;
if ((tiffT4Options & TIFFConstants.GROUP3OPT_FILLBITS) != 0)
paramsn |= Image.CCITT_ENCODEDBYTEALIGN;
}
break;
case TIFFConstants.COMPRESSION_CCITTFAX4:
imagecomp = Image.CCITTG4;
TIFFField t6OptionsField = dir.GetField(TIFFConstants.TIFFTAG_GROUP4OPTIONS);
if (t6OptionsField != null)
tiffT6Options = t6OptionsField.GetAsLong(0);
break;
}
if (direct && rowsStrip == h) { //single strip, direct
byte[] im = new byte[(int)size[0]];
s.Seek(offset[0]);
s.ReadFully(im);
img = Image.GetInstance(w, h, false, imagecomp, paramsn, im);
img.Inverted = true;
}
else {
int rowsLeft = h;
CCITTG4Encoder g4 = new CCITTG4Encoder(w);
for (int k = 0; k < offset.Length; ++k) {
byte[] im = new byte[(int)size[k]];
s.Seek(offset[k]);
s.ReadFully(im);
int height = Math.Min(rowsStrip, rowsLeft);
TIFFFaxDecoder decoder = new TIFFFaxDecoder(fillOrder, w, height);
byte[] outBuf = new byte[(w + 7) / 8 * height];
switch (compression) {
case TIFFConstants.COMPRESSION_CCITTRLEW:
case TIFFConstants.COMPRESSION_CCITTRLE:
decoder.Decode1D(outBuf, im, 0, height);
g4.Fax4Encode(outBuf, height);
break;
case TIFFConstants.COMPRESSION_CCITTFAX3:
try {
decoder.Decode2D(outBuf, im, 0, height, tiffT4Options);
}
catch (Exception e) {
// let's flip the fill bits and try again...
tiffT4Options ^= TIFFConstants.GROUP3OPT_FILLBITS;
try {
decoder.Decode2D(outBuf, im, 0, height, tiffT4Options);
}
catch {
throw e;
}
}
g4.Fax4Encode(outBuf, height);
break;
case TIFFConstants.COMPRESSION_CCITTFAX4:
decoder.DecodeT6(outBuf, im, 0, height, tiffT6Options);
g4.Fax4Encode(outBuf, height);
break;
}
rowsLeft -= rowsStrip;
}
byte[] g4pic = g4.Close();
img = Image.GetInstance(w, h, false, Image.CCITTG4, paramsn & Image.CCITT_BLACKIS1, g4pic);
}
img.SetDpi(dpiX, dpiY);
img.XYRatio = XYRatio;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) {
try {
TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_ICCPROFILE);
ICC_Profile icc_prof = ICC_Profile.GetInstance(fd.GetAsBytes());
if (icc_prof.NumComponents == 1)
img.TagICC = icc_prof;
}
catch {
//empty
}
}
img.OriginalType = Image.ORIGINAL_TIFF;
if (rotation != 0)
img.InitialRotation = rotation;
return img;
}
protected static Image GetTiffImageColor(TIFFDirectory dir, RandomAccessFileOrArray s) {
int predictor = 1;
TIFFLZWDecoder lzwDecoder = null;
int compression = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_COMPRESSION);
switch (compression) {
case TIFFConstants.COMPRESSION_NONE:
case TIFFConstants.COMPRESSION_LZW:
case TIFFConstants.COMPRESSION_PACKBITS:
case TIFFConstants.COMPRESSION_DEFLATE:
case TIFFConstants.COMPRESSION_ADOBE_DEFLATE:
case TIFFConstants.COMPRESSION_OJPEG:
case TIFFConstants.COMPRESSION_JPEG:
break;
default:
throw new ArgumentException("The compression " + compression + " is not supported.");
}
int photometric = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_PHOTOMETRIC);
switch (photometric) {
case TIFFConstants.PHOTOMETRIC_MINISWHITE:
case TIFFConstants.PHOTOMETRIC_MINISBLACK:
case TIFFConstants.PHOTOMETRIC_RGB:
case TIFFConstants.PHOTOMETRIC_SEPARATED:
case TIFFConstants.PHOTOMETRIC_PALETTE:
break;
default:
if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG)
throw new ArgumentException("The photometric " + photometric + " is not supported.");
break;
}
float rotation = 0;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ORIENTATION)) {
int rot = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_ORIENTATION);
if (rot == TIFFConstants.ORIENTATION_BOTRIGHT || rot == TIFFConstants.ORIENTATION_BOTLEFT)
rotation = (float)Math.PI;
else if (rot == TIFFConstants.ORIENTATION_LEFTTOP || rot == TIFFConstants.ORIENTATION_LEFTBOT)
rotation = (float)(Math.PI / 2.0);
else if (rot == TIFFConstants.ORIENTATION_RIGHTTOP || rot == TIFFConstants.ORIENTATION_RIGHTBOT)
rotation = -(float)(Math.PI / 2.0);
}
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_PLANARCONFIG)
&& dir.GetFieldAsLong(TIFFConstants.TIFFTAG_PLANARCONFIG) == TIFFConstants.PLANARCONFIG_SEPARATE)
throw new ArgumentException("Planar images are not supported.");
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_EXTRASAMPLES))
throw new ArgumentException("Extra samples are not supported.");
int samplePerPixel = 1;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL)) // 1,3,4
samplePerPixel = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL);
int bitsPerSample = 1;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_BITSPERSAMPLE))
bitsPerSample = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_BITSPERSAMPLE);
switch (bitsPerSample) {
case 1:
case 2:
case 4:
case 8:
break;
default:
throw new ArgumentException("Bits per sample " + bitsPerSample + " is not supported.");
}
Image img = null;
int h = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGELENGTH);
int w = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_IMAGEWIDTH);
int dpiX = 0;
int dpiY = 0;
int resolutionUnit = TIFFConstants.RESUNIT_INCH;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_RESOLUTIONUNIT))
resolutionUnit = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_RESOLUTIONUNIT);
dpiX = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_XRESOLUTION), resolutionUnit);
dpiY = GetDpi(dir.GetField(TIFFConstants.TIFFTAG_YRESOLUTION), resolutionUnit);
int fillOrder = 1;
bool reverse = false;
TIFFField fillOrderField = dir.GetField(TIFFConstants.TIFFTAG_FILLORDER);
if (fillOrderField != null)
fillOrder = fillOrderField.GetAsInt(0);
reverse = (fillOrder == TIFFConstants.FILLORDER_LSB2MSB);
int rowsStrip = h;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ROWSPERSTRIP)) //another hack for broken tiffs
rowsStrip = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP);
if (rowsStrip <= 0 || rowsStrip > h)
rowsStrip = h;
long[] offset = GetArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPOFFSETS);
long[] size = GetArrayLongShort(dir, TIFFConstants.TIFFTAG_STRIPBYTECOUNTS);
if ((size == null || (size.Length == 1 && (size[0] == 0 || size[0] + offset[0] > s.Length))) && h == rowsStrip) { // some TIFF producers are really lousy, so...
size = new long[]{s.Length - (int)offset[0]};
}
if (compression == TIFFConstants.COMPRESSION_LZW) {
TIFFField predictorField = dir.GetField(TIFFConstants.TIFFTAG_PREDICTOR);
if (predictorField != null) {
predictor = predictorField.GetAsInt(0);
if (predictor != 1 && predictor != 2) {
throw new Exception("Illegal value for Predictor in TIFF file.");
}
if (predictor == 2 && bitsPerSample != 8) {
throw new Exception(bitsPerSample + "-bit samples are not supported for Horizontal differencing Predictor.");
}
}
lzwDecoder = new TIFFLZWDecoder(w, predictor,
samplePerPixel);
}
int rowsLeft = h;
MemoryStream stream = null;
ZDeflaterOutputStream zip = null;
CCITTG4Encoder g4 = null;
if (bitsPerSample == 1 && samplePerPixel == 1) {
g4 = new CCITTG4Encoder(w);
}
else {
stream = new MemoryStream();
if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG)
zip = new ZDeflaterOutputStream(stream);
}
if (compression == TIFFConstants.COMPRESSION_OJPEG) {
// Assume that the TIFFTAG_JPEGIFBYTECOUNT tag is optional, since it's obsolete and
// is often missing
if ((!dir.IsTagPresent(TIFFConstants.TIFFTAG_JPEGIFOFFSET))) {
throw new IOException("Missing tag(s) for OJPEG compression.");
}
int jpegOffset = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFOFFSET);
int jpegLength = s.Length - jpegOffset;
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT)) {
jpegLength = (int)dir.GetFieldAsLong(TIFFConstants.TIFFTAG_JPEGIFBYTECOUNT) +
(int)size[0];
}
byte[] jpeg = new byte[Math.Min(jpegLength, s.Length - jpegOffset)];
int posFilePointer = s.FilePointer;
posFilePointer += jpegOffset;
s.Seek(posFilePointer);
s.ReadFully(jpeg);
img = new Jpeg(jpeg);
}
else if (compression == TIFFConstants.COMPRESSION_JPEG) {
if (size.Length > 1)
throw new IOException("Compression JPEG is only supported with a single strip. This image has " + size.Length + " strips.");
byte[] jpeg = new byte[(int)size[0]];
s.Seek(offset[0]);
s.ReadFully(jpeg);
img = new Jpeg(jpeg);
}
else {
for (int k = 0; k < offset.Length; ++k) {
byte[] im = new byte[(int)size[k]];
s.Seek(offset[k]);
s.ReadFully(im);
int height = Math.Min(rowsStrip, rowsLeft);
byte[] outBuf = null;
if (compression != TIFFConstants.COMPRESSION_NONE)
outBuf = new byte[(w * bitsPerSample * samplePerPixel + 7) / 8 * height];
if (reverse)
TIFFFaxDecoder.ReverseBits(im);
switch (compression) {
case TIFFConstants.COMPRESSION_DEFLATE:
case TIFFConstants.COMPRESSION_ADOBE_DEFLATE:
Inflate(im, outBuf);
break;
case TIFFConstants.COMPRESSION_NONE:
outBuf = im;
break;
case TIFFConstants.COMPRESSION_PACKBITS:
DecodePackbits(im, outBuf);
break;
case TIFFConstants.COMPRESSION_LZW:
lzwDecoder.Decode(im, outBuf, height);
break;
}
if (bitsPerSample == 1 && samplePerPixel == 1) {
g4.Fax4Encode(outBuf, height);
}
else {
zip.Write(outBuf, 0, outBuf.Length);
}
rowsLeft -= rowsStrip;
}
if (bitsPerSample == 1 && samplePerPixel == 1) {
img = Image.GetInstance(w, h, false, Image.CCITTG4,
photometric == TIFFConstants.PHOTOMETRIC_MINISBLACK ? Image.CCITT_BLACKIS1 : 0, g4.Close());
}
else {
zip.Close();
img = Image.GetInstance(w, h, samplePerPixel, bitsPerSample, stream.ToArray());
img.Deflated = true;
}
}
img.SetDpi(dpiX, dpiY);
if (compression != TIFFConstants.COMPRESSION_OJPEG && compression != TIFFConstants.COMPRESSION_JPEG) {
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_ICCPROFILE)) {
try {
TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_ICCPROFILE);
ICC_Profile icc_prof = ICC_Profile.GetInstance(fd.GetAsBytes());
if (samplePerPixel == icc_prof.NumComponents)
img.TagICC = icc_prof;
}
catch {
//empty
}
}
if (dir.IsTagPresent(TIFFConstants.TIFFTAG_COLORMAP)) {
TIFFField fd = dir.GetField(TIFFConstants.TIFFTAG_COLORMAP);
char[] rgb = fd.GetAsChars();
byte[] palette = new byte[rgb.Length];
int gColor = rgb.Length / 3;
int bColor = gColor * 2;
for (int k = 0; k < gColor; ++k) {
palette[k * 3] = (byte)(rgb[k] >> 8);
palette[k * 3 + 1] = (byte)(rgb[k + gColor] >> 8);
palette[k * 3 + 2] = (byte)(rgb[k + bColor] >> 8);
}
PdfArray indexed = new PdfArray();
indexed.Add(PdfName.INDEXED);
indexed.Add(PdfName.DEVICERGB);
indexed.Add(new PdfNumber(gColor - 1));
indexed.Add(new PdfString(palette));
PdfDictionary additional = new PdfDictionary();
additional.Put(PdfName.COLORSPACE, indexed);
img.Additional = additional;
}
img.OriginalType = Image.ORIGINAL_TIFF;
}
if (photometric == TIFFConstants.PHOTOMETRIC_MINISWHITE)
img.Inverted = true;
if (rotation != 0)
img.InitialRotation = rotation;
return img;
}
static long[] GetArrayLongShort(TIFFDirectory dir, int tag) {
TIFFField field = dir.GetField(tag);
if (field == null)
return null;
long[] offset;
if (field.GetType() == TIFFField.TIFF_LONG)
offset = field.GetAsLongs();
else { // must be short
char[] temp = field.GetAsChars();
offset = new long[temp.Length];
for (int k = 0; k < temp.Length; ++k)
offset[k] = temp[k];
}
return offset;
}
// Uncompress packbits compressed image data.
public static void DecodePackbits(byte[] data, byte[] dst) {
int srcCount = 0, dstCount = 0;
sbyte repeat, b;
try {
while (dstCount < dst.Length) {
b = (sbyte)data[srcCount++];
if (b >= 0 && b <= 127) {
// literal run packet
for (int i=0; i<(b + 1); i++) {
dst[dstCount++] = data[srcCount++];
}
} else if (b <= -1 && b >= -127) {
// 2 byte encoded run packet
repeat = (sbyte)data[srcCount++];
for (int i=0; i<(-b + 1); i++) {
dst[dstCount++] = (byte)repeat;
}
} else {
// no-op packet. Do nothing
srcCount++;
}
}
}
catch {
}
}
public static void Inflate(byte[] deflated, byte[] inflated) {
byte[] outp = PdfReader.FlateDecode(deflated);
System.Array.Copy(outp, 0, inflated, 0, Math.Min(outp.Length, inflated.Length));
}
}
}

View File

@@ -0,0 +1,117 @@
using System;
using System.IO;
using iTextSharp.text;
/*
* $Id: InputMeta.cs,v 1.4 2008/05/13 11:25:36 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf {
/// <summary>
/// Summary description for InputMeta.
/// </summary>
public class InputMeta {
Stream sr;
int length;
public InputMeta(Stream istr) {
this.sr = istr;
}
public int ReadWord() {
length += 2;
int k1 = sr.ReadByte();
if (k1 < 0)
return 0;
return (k1 + (sr.ReadByte() << 8)) & 0xffff;
}
public int ReadShort() {
int k = ReadWord();
if (k > 0x7fff)
k -= 0x10000;
return k;
}
public Int32 ReadInt() {
length += 4;
int k1 = sr.ReadByte();
if (k1 < 0)
return 0;
int k2 = sr.ReadByte() << 8;
int k3 = sr.ReadByte() << 16;
return k1 + k2 + k3 + (sr.ReadByte() << 24);
}
public int ReadByte() {
++length;
return sr.ReadByte() & 0xff;
}
public void Skip(int len) {
length += len;
Utilities.Skip(sr, len);
}
public int Length {
get {
return length;
}
}
public Color ReadColor() {
int red = ReadByte();
int green = ReadByte();
int blue = ReadByte();
ReadByte();
return new Color(red, green, blue);
}
}
}

View File

@@ -0,0 +1,101 @@
using System;
using iTextSharp.text;
/*
* $Id: MetaBrush.cs,v 1.3 2008/05/13 11:25:36 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf {
public class MetaBrush : MetaObject {
public const int BS_SOLID = 0;
public const int BS_NULL = 1;
public const int BS_HATCHED = 2;
public const int BS_PATTERN = 3;
public const int BS_DIBPATTERN = 5;
public const int HS_HORIZONTAL = 0;
public const int HS_VERTICAL = 1;
public const int HS_FDIAGONAL = 2;
public const int HS_BDIAGONAL = 3;
public const int HS_CROSS = 4;
public const int HS_DIAGCROSS = 5;
int style = BS_SOLID;
int hatch;
Color color = Color.WHITE;
public MetaBrush() {
type = META_BRUSH;
}
public void Init(InputMeta meta) {
style = meta.ReadWord();
color = meta.ReadColor();
hatch = meta.ReadWord();
}
public int Style {
get {
return style;
}
}
public int Hatch {
get {
return hatch;
}
}
public Color Color {
get {
return color;
}
}
}
}

View File

@@ -0,0 +1,764 @@
using System;
using System.IO;
using System.Net;
using iTextSharp.text;
using System.Collections;
/*
* $Id: MetaDo.cs,v 1.4 2008/05/13 11:25:36 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf
{
/// <summary>
/// Summary description for MetaDo.
/// </summary>
public class MetaDo
{
public const int META_SETBKCOLOR = 0x0201;
public const int META_SETBKMODE = 0x0102;
public const int META_SETMAPMODE = 0x0103;
public const int META_SETROP2 = 0x0104;
public const int META_SETRELABS = 0x0105;
public const int META_SETPOLYFILLMODE = 0x0106;
public const int META_SETSTRETCHBLTMODE = 0x0107;
public const int META_SETTEXTCHAREXTRA = 0x0108;
public const int META_SETTEXTCOLOR = 0x0209;
public const int META_SETTEXTJUSTIFICATION = 0x020A;
public const int META_SETWINDOWORG = 0x020B;
public const int META_SETWINDOWEXT = 0x020C;
public const int META_SETVIEWPORTORG = 0x020D;
public const int META_SETVIEWPORTEXT = 0x020E;
public const int META_OFFSETWINDOWORG = 0x020F;
public const int META_SCALEWINDOWEXT = 0x0410;
public const int META_OFFSETVIEWPORTORG = 0x0211;
public const int META_SCALEVIEWPORTEXT = 0x0412;
public const int META_LINETO = 0x0213;
public const int META_MOVETO = 0x0214;
public const int META_EXCLUDECLIPRECT = 0x0415;
public const int META_INTERSECTCLIPRECT = 0x0416;
public const int META_ARC = 0x0817;
public const int META_ELLIPSE = 0x0418;
public const int META_FLOODFILL = 0x0419;
public const int META_PIE = 0x081A;
public const int META_RECTANGLE = 0x041B;
public const int META_ROUNDRECT = 0x061C;
public const int META_PATBLT = 0x061D;
public const int META_SAVEDC = 0x001E;
public const int META_SETPIXEL = 0x041F;
public const int META_OFFSETCLIPRGN = 0x0220;
public const int META_TEXTOUT = 0x0521;
public const int META_BITBLT = 0x0922;
public const int META_STRETCHBLT = 0x0B23;
public const int META_POLYGON = 0x0324;
public const int META_POLYLINE = 0x0325;
public const int META_ESCAPE = 0x0626;
public const int META_RESTOREDC = 0x0127;
public const int META_FILLREGION = 0x0228;
public const int META_FRAMEREGION = 0x0429;
public const int META_INVERTREGION = 0x012A;
public const int META_PAINTREGION = 0x012B;
public const int META_SELECTCLIPREGION = 0x012C;
public const int META_SELECTOBJECT = 0x012D;
public const int META_SETTEXTALIGN = 0x012E;
public const int META_CHORD = 0x0830;
public const int META_SETMAPPERFLAGS = 0x0231;
public const int META_EXTTEXTOUT = 0x0a32;
public const int META_SETDIBTODEV = 0x0d33;
public const int META_SELECTPALETTE = 0x0234;
public const int META_REALIZEPALETTE = 0x0035;
public const int META_ANIMATEPALETTE = 0x0436;
public const int META_SETPALENTRIES = 0x0037;
public const int META_POLYPOLYGON = 0x0538;
public const int META_RESIZEPALETTE = 0x0139;
public const int META_DIBBITBLT = 0x0940;
public const int META_DIBSTRETCHBLT = 0x0b41;
public const int META_DIBCREATEPATTERNBRUSH = 0x0142;
public const int META_STRETCHDIB = 0x0f43;
public const int META_EXTFLOODFILL = 0x0548;
public const int META_DELETEOBJECT = 0x01f0;
public const int META_CREATEPALETTE = 0x00f7;
public const int META_CREATEPATTERNBRUSH = 0x01F9;
public const int META_CREATEPENINDIRECT = 0x02FA;
public const int META_CREATEFONTINDIRECT = 0x02FB;
public const int META_CREATEBRUSHINDIRECT = 0x02FC;
public const int META_CREATEREGION = 0x06FF;
public PdfContentByte cb;
public InputMeta meta;
int left;
int top;
int right;
int bottom;
int inch;
MetaState state = new MetaState();
public MetaDo(Stream meta, PdfContentByte cb) {
this.cb = cb;
this.meta = new InputMeta(meta);
}
public void ReadAll() {
if (meta.ReadInt() != unchecked((int)0x9AC6CDD7)) {
throw new DocumentException("Not a placeable windows metafile");
}
meta.ReadWord();
left = meta.ReadShort();
top = meta.ReadShort();
right = meta.ReadShort();
bottom = meta.ReadShort();
inch = meta.ReadWord();
state.ScalingX = (float)(right - left) / (float)inch * 72f;
state.ScalingY = (float)(bottom - top) / (float)inch * 72f;
state.OffsetWx = left;
state.OffsetWy = top;
state.ExtentWx = right - left;
state.ExtentWy = bottom - top;
meta.ReadInt();
meta.ReadWord();
meta.Skip(18);
int tsize;
int function;
cb.SetLineCap(1);
cb.SetLineJoin(1);
for (;;) {
int lenMarker = meta.Length;
tsize = meta.ReadInt();
if (tsize < 3)
break;
function = meta.ReadWord();
switch (function) {
case 0:
break;
case META_CREATEPALETTE:
case META_CREATEREGION:
case META_DIBCREATEPATTERNBRUSH:
state.AddMetaObject(new MetaObject());
break;
case META_CREATEPENINDIRECT:
{
MetaPen pen = new MetaPen();
pen.Init(meta);
state.AddMetaObject(pen);
break;
}
case META_CREATEBRUSHINDIRECT:
{
MetaBrush brush = new MetaBrush();
brush.Init(meta);
state.AddMetaObject(brush);
break;
}
case META_CREATEFONTINDIRECT:
{
MetaFont font = new MetaFont();
font.Init(meta);
state.AddMetaObject(font);
break;
}
case META_SELECTOBJECT:
{
int idx = meta.ReadWord();
state.SelectMetaObject(idx, cb);
break;
}
case META_DELETEOBJECT:
{
int idx = meta.ReadWord();
state.DeleteMetaObject(idx);
break;
}
case META_SAVEDC:
state.SaveState(cb);
break;
case META_RESTOREDC:
{
int idx = meta.ReadShort();
state.RestoreState(idx, cb);
break;
}
case META_SETWINDOWORG:
state.OffsetWy = meta.ReadShort();
state.OffsetWx = meta.ReadShort();
break;
case META_SETWINDOWEXT:
state.ExtentWy = meta.ReadShort();
state.ExtentWx = meta.ReadShort();
break;
case META_MOVETO:
{
int y = meta.ReadShort();
System.Drawing.Point p = new System.Drawing.Point(meta.ReadShort(), y);
state.CurrentPoint = p;
break;
}
case META_LINETO:
{
int y = meta.ReadShort();
int x = meta.ReadShort();
System.Drawing.Point p = state.CurrentPoint;
cb.MoveTo(state.TransformX(p.X), state.TransformY(p.Y));
cb.LineTo(state.TransformX(x), state.TransformY(y));
cb.Stroke();
state.CurrentPoint = new System.Drawing.Point(x, y);
break;
}
case META_POLYLINE:
{
state.LineJoinPolygon = cb;
int len = meta.ReadWord();
int x = meta.ReadShort();
int y = meta.ReadShort();
cb.MoveTo(state.TransformX(x), state.TransformY(y));
for (int k = 1; k < len; ++k) {
x = meta.ReadShort();
y = meta.ReadShort();
cb.LineTo(state.TransformX(x), state.TransformY(y));
}
cb.Stroke();
break;
}
case META_POLYGON:
{
if (IsNullStrokeFill(false))
break;
int len = meta.ReadWord();
int sx = meta.ReadShort();
int sy = meta.ReadShort();
cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
for (int k = 1; k < len; ++k) {
int x = meta.ReadShort();
int y = meta.ReadShort();
cb.LineTo(state.TransformX(x), state.TransformY(y));
}
cb.LineTo(state.TransformX(sx), state.TransformY(sy));
StrokeAndFill();
break;
}
case META_POLYPOLYGON:
{
if (IsNullStrokeFill(false))
break;
int numPoly = meta.ReadWord();
int[] lens = new int[numPoly];
for (int k = 0; k < lens.Length; ++k)
lens[k] = meta.ReadWord();
for (int j = 0; j < lens.Length; ++j) {
int len = lens[j];
int sx = meta.ReadShort();
int sy = meta.ReadShort();
cb.MoveTo(state.TransformX(sx), state.TransformY(sy));
for (int k = 1; k < len; ++k) {
int x = meta.ReadShort();
int y = meta.ReadShort();
cb.LineTo(state.TransformX(x), state.TransformY(y));
}
cb.LineTo(state.TransformX(sx), state.TransformY(sy));
}
StrokeAndFill();
break;
}
case META_ELLIPSE:
{
if (IsNullStrokeFill(state.LineNeutral))
break;
int b = meta.ReadShort();
int r = meta.ReadShort();
int t = meta.ReadShort();
int l = meta.ReadShort();
cb.Arc(state.TransformX(l), state.TransformY(b), state.TransformX(r), state.TransformY(t), 0, 360);
StrokeAndFill();
break;
}
case META_ARC:
{
if (IsNullStrokeFill(state.LineNeutral))
break;
float yend = state.TransformY(meta.ReadShort());
float xend = state.TransformX(meta.ReadShort());
float ystart = state.TransformY(meta.ReadShort());
float xstart = state.TransformX(meta.ReadShort());
float b = state.TransformY(meta.ReadShort());
float r = state.TransformX(meta.ReadShort());
float t = state.TransformY(meta.ReadShort());
float l = state.TransformX(meta.ReadShort());
float cx = (r + l) / 2;
float cy = (t + b) / 2;
float arc1 = GetArc(cx, cy, xstart, ystart);
float arc2 = GetArc(cx, cy, xend, yend);
arc2 -= arc1;
if (arc2 <= 0)
arc2 += 360;
cb.Arc(l, b, r, t, arc1, arc2);
cb.Stroke();
break;
}
case META_PIE:
{
if (IsNullStrokeFill(state.LineNeutral))
break;
float yend = state.TransformY(meta.ReadShort());
float xend = state.TransformX(meta.ReadShort());
float ystart = state.TransformY(meta.ReadShort());
float xstart = state.TransformX(meta.ReadShort());
float b = state.TransformY(meta.ReadShort());
float r = state.TransformX(meta.ReadShort());
float t = state.TransformY(meta.ReadShort());
float l = state.TransformX(meta.ReadShort());
float cx = (r + l) / 2;
float cy = (t + b) / 2;
float arc1 = GetArc(cx, cy, xstart, ystart);
float arc2 = GetArc(cx, cy, xend, yend);
arc2 -= arc1;
if (arc2 <= 0)
arc2 += 360;
ArrayList ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
if (ar.Count == 0)
break;
float[] pt = (float [])ar[0];
cb.MoveTo(cx, cy);
cb.LineTo(pt[0], pt[1]);
for (int k = 0; k < ar.Count; ++k) {
pt = (float [])ar[k];
cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
}
cb.LineTo(cx, cy);
StrokeAndFill();
break;
}
case META_CHORD:
{
if (IsNullStrokeFill(state.LineNeutral))
break;
float yend = state.TransformY(meta.ReadShort());
float xend = state.TransformX(meta.ReadShort());
float ystart = state.TransformY(meta.ReadShort());
float xstart = state.TransformX(meta.ReadShort());
float b = state.TransformY(meta.ReadShort());
float r = state.TransformX(meta.ReadShort());
float t = state.TransformY(meta.ReadShort());
float l = state.TransformX(meta.ReadShort());
float cx = (r + l) / 2;
float cy = (t + b) / 2;
float arc1 = GetArc(cx, cy, xstart, ystart);
float arc2 = GetArc(cx, cy, xend, yend);
arc2 -= arc1;
if (arc2 <= 0)
arc2 += 360;
ArrayList ar = PdfContentByte.BezierArc(l, b, r, t, arc1, arc2);
if (ar.Count == 0)
break;
float[] pt = (float [])ar[0];
cx = pt[0];
cy = pt[1];
cb.MoveTo(cx, cy);
for (int k = 0; k < ar.Count; ++k) {
pt = (float [])ar[k];
cb.CurveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]);
}
cb.LineTo(cx, cy);
StrokeAndFill();
break;
}
case META_RECTANGLE:
{
if (IsNullStrokeFill(true))
break;
float b = state.TransformY(meta.ReadShort());
float r = state.TransformX(meta.ReadShort());
float t = state.TransformY(meta.ReadShort());
float l = state.TransformX(meta.ReadShort());
cb.Rectangle(l, b, r - l, t - b);
StrokeAndFill();
break;
}
case META_ROUNDRECT:
{
if (IsNullStrokeFill(true))
break;
float h = state.TransformY(0) - state.TransformY(meta.ReadShort());
float w = state.TransformX(meta.ReadShort()) - state.TransformX(0);
float b = state.TransformY(meta.ReadShort());
float r = state.TransformX(meta.ReadShort());
float t = state.TransformY(meta.ReadShort());
float l = state.TransformX(meta.ReadShort());
cb.RoundRectangle(l, b, r - l, t - b, (h + w) / 4);
StrokeAndFill();
break;
}
case META_INTERSECTCLIPRECT:
{
float b = state.TransformY(meta.ReadShort());
float r = state.TransformX(meta.ReadShort());
float t = state.TransformY(meta.ReadShort());
float l = state.TransformX(meta.ReadShort());
cb.Rectangle(l, b, r - l, t - b);
cb.EoClip();
cb.NewPath();
break;
}
case META_EXTTEXTOUT:
{
int y = meta.ReadShort();
int x = meta.ReadShort();
int count = meta.ReadWord();
int flag = meta.ReadWord();
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
if ((flag & (MetaFont.ETO_CLIPPED | MetaFont.ETO_OPAQUE)) != 0) {
x1 = meta.ReadShort();
y1 = meta.ReadShort();
x2 = meta.ReadShort();
y2 = meta.ReadShort();
}
byte[] text = new byte[count];
int k;
for (k = 0; k < count; ++k) {
byte c = (byte)meta.ReadByte();
if (c == 0)
break;
text[k] = c;
}
string s;
try {
s = System.Text.Encoding.GetEncoding(1252).GetString(text, 0, k);
}
catch {
s = System.Text.ASCIIEncoding.ASCII.GetString(text, 0, k);
}
OutputText(x, y, flag, x1, y1, x2, y2, s);
break;
}
case META_TEXTOUT:
{
int count = meta.ReadWord();
byte[] text = new byte[count];
int k;
for (k = 0; k < count; ++k) {
byte c = (byte)meta.ReadByte();
if (c == 0)
break;
text[k] = c;
}
string s;
try {
s = System.Text.Encoding.GetEncoding(1252).GetString(text, 0, k);
}
catch {
s = System.Text.ASCIIEncoding.ASCII.GetString(text, 0, k);
}
count = (count + 1) & 0xfffe;
meta.Skip(count - k);
int y = meta.ReadShort();
int x = meta.ReadShort();
OutputText(x, y, 0, 0, 0, 0, 0, s);
break;
}
case META_SETBKCOLOR:
state.CurrentBackgroundColor = meta.ReadColor();
break;
case META_SETTEXTCOLOR:
state.CurrentTextColor = meta.ReadColor();
break;
case META_SETTEXTALIGN:
state.TextAlign = meta.ReadWord();
break;
case META_SETBKMODE:
state.BackgroundMode = meta.ReadWord();
break;
case META_SETPOLYFILLMODE:
state.PolyFillMode = meta.ReadWord();
break;
case META_SETPIXEL:
{
Color color = meta.ReadColor();
int y = meta.ReadShort();
int x = meta.ReadShort();
cb.SaveState();
cb.SetColorFill(color);
cb.Rectangle(state.TransformX(x), state.TransformY(y), .2f, .2f);
cb.Fill();
cb.RestoreState();
break;
}
case META_DIBSTRETCHBLT:
case META_STRETCHDIB: {
int rop = meta.ReadInt();
if (function == META_STRETCHDIB) {
/*int usage = */ meta.ReadWord();
}
int srcHeight = meta.ReadShort();
int srcWidth = meta.ReadShort();
int ySrc = meta.ReadShort();
int xSrc = meta.ReadShort();
float destHeight = state.TransformY(meta.ReadShort()) - state.TransformY(0);
float destWidth = state.TransformX(meta.ReadShort()) - state.TransformX(0);
float yDest = state.TransformY(meta.ReadShort());
float xDest = state.TransformX(meta.ReadShort());
byte[] b = new byte[(tsize * 2) - (meta.Length - lenMarker)];
for (int k = 0; k < b.Length; ++k)
b[k] = (byte)meta.ReadByte();
try {
MemoryStream inb = new MemoryStream(b);
Image bmp = BmpImage.GetImage(inb, true, b.Length);
cb.SaveState();
cb.Rectangle(xDest, yDest, destWidth, destHeight);
cb.Clip();
cb.NewPath();
bmp.ScaleAbsolute(destWidth * bmp.Width / srcWidth, -destHeight * bmp.Height / srcHeight);
bmp.SetAbsolutePosition(xDest - destWidth * xSrc / srcWidth, yDest + destHeight * ySrc / srcHeight - bmp.ScaledHeight);
cb.AddImage(bmp);
cb.RestoreState();
}
catch {
// empty on purpose
}
break;
}
}
meta.Skip((tsize * 2) - (meta.Length - lenMarker));
}
state.Cleanup(cb);
}
public void OutputText(int x, int y, int flag, int x1, int y1, int x2, int y2, string text) {
MetaFont font = state.CurrentFont;
float refX = state.TransformX(x);
float refY = state.TransformY(y);
float angle = state.TransformAngle(font.Angle);
float sin = (float)Math.Sin(angle);
float cos = (float)Math.Cos(angle);
float fontSize = font.GetFontSize(state);
BaseFont bf = font.Font;
int align = state.TextAlign;
float textWidth = bf.GetWidthPoint(text, fontSize);
float tx = 0;
float ty = 0;
float descender = bf.GetFontDescriptor(BaseFont.DESCENT, fontSize);
float ury = bf.GetFontDescriptor(BaseFont.BBOXURY, fontSize);
cb.SaveState();
cb.ConcatCTM(cos, sin, -sin, cos, refX, refY);
if ((align & MetaState.TA_CENTER) == MetaState.TA_CENTER)
tx = -textWidth / 2;
else if ((align & MetaState.TA_RIGHT) == MetaState.TA_RIGHT)
tx = -textWidth;
if ((align & MetaState.TA_BASELINE) == MetaState.TA_BASELINE)
ty = 0;
else if ((align & MetaState.TA_BOTTOM) == MetaState.TA_BOTTOM)
ty = -descender;
else
ty = -ury;
Color textColor;
if (state.BackgroundMode == MetaState.OPAQUE) {
textColor = state.CurrentBackgroundColor;
cb.SetColorFill(textColor);
cb.Rectangle(tx, ty + descender, textWidth, ury - descender);
cb.Fill();
}
textColor = state.CurrentTextColor;
cb.SetColorFill(textColor);
cb.BeginText();
cb.SetFontAndSize(bf, fontSize);
cb.SetTextMatrix(tx, ty);
cb.ShowText(text);
cb.EndText();
if (font.IsUnderline()) {
cb.Rectangle(tx, ty - fontSize / 4, textWidth, fontSize / 15);
cb.Fill();
}
if (font.IsStrikeout()) {
cb.Rectangle(tx, ty + fontSize / 3, textWidth, fontSize / 15);
cb.Fill();
}
cb.RestoreState();
}
public bool IsNullStrokeFill(bool isRectangle) {
MetaPen pen = state.CurrentPen;
MetaBrush brush = state.CurrentBrush;
bool noPen = (pen.Style == MetaPen.PS_NULL);
int style = brush.Style;
bool isBrush = (style == MetaBrush.BS_SOLID || (style == MetaBrush.BS_HATCHED && state.BackgroundMode == MetaState.OPAQUE));
bool result = noPen && !isBrush;
if (!noPen) {
if (isRectangle)
state.LineJoinRectangle = cb;
else
state.LineJoinPolygon = cb;
}
return result;
}
public void StrokeAndFill(){
MetaPen pen = state.CurrentPen;
MetaBrush brush = state.CurrentBrush;
int penStyle = pen.Style;
int brushStyle = brush.Style;
if (penStyle == MetaPen.PS_NULL) {
cb.ClosePath();
if (state.PolyFillMode == MetaState.ALTERNATE) {
cb.EoFill();
}
else {
cb.Fill();
}
}
else {
bool isBrush = (brushStyle == MetaBrush.BS_SOLID || (brushStyle == MetaBrush.BS_HATCHED && state.BackgroundMode == MetaState.OPAQUE));
if (isBrush) {
if (state.PolyFillMode == MetaState.ALTERNATE)
cb.ClosePathEoFillStroke();
else
cb.ClosePathFillStroke();
}
else {
cb.ClosePathStroke();
}
}
}
internal static float GetArc(float xCenter, float yCenter, float xDot, float yDot) {
double s = Math.Atan2(yDot - yCenter, xDot - xCenter);
if (s < 0)
s += Math.PI * 2;
return (float)(s / Math.PI * 180);
}
public static byte[] WrapBMP(Image image) {
if (image.OriginalType != Image.ORIGINAL_BMP)
throw new IOException("Only BMP can be wrapped in WMF.");
Stream imgIn;
byte[] data = null;
if (image.OriginalData == null) {
imgIn = WebRequest.Create(image.Url).GetResponse().GetResponseStream();
MemoryStream outp = new MemoryStream();
int b = 0;
while ((b = imgIn.ReadByte()) != -1)
outp.WriteByte((byte)b);
imgIn.Close();
data = outp.ToArray();
}
else
data = image.OriginalData;
int sizeBmpWords = (data.Length - 14 + 1) >> 1;
MemoryStream os = new MemoryStream();
// write metafile header
WriteWord(os, 1);
WriteWord(os, 9);
WriteWord(os, 0x0300);
WriteDWord(os, 9 + 4 + 5 + 5 + (13 + sizeBmpWords) + 3); // total metafile size
WriteWord(os, 1);
WriteDWord(os, 14 + sizeBmpWords); // max record size
WriteWord(os, 0);
// write records
WriteDWord(os, 4);
WriteWord(os, META_SETMAPMODE);
WriteWord(os, 8);
WriteDWord(os, 5);
WriteWord(os, META_SETWINDOWORG);
WriteWord(os, 0);
WriteWord(os, 0);
WriteDWord(os, 5);
WriteWord(os, META_SETWINDOWEXT);
WriteWord(os, (int)image.Height);
WriteWord(os, (int)image.Width);
WriteDWord(os, 13 + sizeBmpWords);
WriteWord(os, META_DIBSTRETCHBLT);
WriteDWord(os, 0x00cc0020);
WriteWord(os, (int)image.Height);
WriteWord(os, (int)image.Width);
WriteWord(os, 0);
WriteWord(os, 0);
WriteWord(os, (int)image.Height);
WriteWord(os, (int)image.Width);
WriteWord(os, 0);
WriteWord(os, 0);
os.Write(data, 14, data.Length - 14);
if ((data.Length & 1) == 1)
os.WriteByte(0);
// WriteDWord(os, 14 + sizeBmpWords);
// WriteWord(os, META_STRETCHDIB);
// WriteDWord(os, 0x00cc0020);
// WriteWord(os, 0);
// WriteWord(os, (int)image.Height);
// WriteWord(os, (int)image.Width);
// WriteWord(os, 0);
// WriteWord(os, 0);
// WriteWord(os, (int)image.Height);
// WriteWord(os, (int)image.Width);
// WriteWord(os, 0);
// WriteWord(os, 0);
// os.Write(data, 14, data.length - 14);
// if ((data.length & 1) == 1)
// os.Write(0);
WriteDWord(os, 3);
WriteWord(os, 0);
os.Close();
return os.ToArray();
}
public static void WriteWord(Stream os, int v) {
os.WriteByte((byte)(v & 0xff));
os.WriteByte((byte)((v >> 8) & 0xff));
}
public static void WriteDWord(Stream os, int v) {
WriteWord(os, v & 0xffff);
WriteWord(os, (v >> 16) & 0xffff);
}
}
}

View File

@@ -0,0 +1,206 @@
using System;
/*
* $Id: MetaFont.cs,v 1.7 2008/05/13 11:25:37 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf {
public class MetaFont : MetaObject {
static string[] fontNames = {
"Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
"Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique",
"Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
"Symbol", "ZapfDingbats"};
internal const int MARKER_BOLD = 1;
internal const int MARKER_ITALIC = 2;
internal const int MARKER_COURIER = 0;
internal const int MARKER_HELVETICA = 4;
internal const int MARKER_TIMES = 8;
internal const int MARKER_SYMBOL = 12;
internal const int DEFAULT_PITCH = 0;
internal const int FIXED_PITCH = 1;
internal const int VARIABLE_PITCH = 2;
internal const int FF_DONTCARE = 0;
internal const int FF_ROMAN = 1;
internal const int FF_SWISS = 2;
internal const int FF_MODERN = 3;
internal const int FF_SCRIPT = 4;
internal const int FF_DECORATIVE = 5;
internal const int BOLDTHRESHOLD = 600;
internal const int nameSize = 32;
internal const int ETO_OPAQUE = 2;
internal const int ETO_CLIPPED = 4;
int height;
float angle;
int bold;
int italic;
bool underline;
bool strikeout;
int charset;
int pitchAndFamily;
string faceName = "arial";
BaseFont font = null;
public MetaFont() {
type = META_FONT;
}
public void Init(InputMeta meta) {
height = Math.Abs(meta.ReadShort());
meta.Skip(2);
angle = (float)(meta.ReadShort() / 1800.0 * Math.PI);
meta.Skip(2);
bold = (meta.ReadShort() >= BOLDTHRESHOLD ? MARKER_BOLD : 0);
italic = (meta.ReadByte() != 0 ? MARKER_ITALIC : 0);
underline = (meta.ReadByte() != 0);
strikeout = (meta.ReadByte() != 0);
charset = meta.ReadByte();
meta.Skip(3);
pitchAndFamily = meta.ReadByte();
byte[] name = new byte[nameSize];
int k;
for (k = 0; k < nameSize; ++k) {
int c = meta.ReadByte();
if (c == 0) {
break;
}
name[k] = (byte)c;
}
try {
faceName = System.Text.Encoding.GetEncoding(1252).GetString(name, 0, k);
}
catch {
faceName = System.Text.ASCIIEncoding.ASCII.GetString(name, 0, k);
}
faceName = faceName.ToLower(System.Globalization.CultureInfo.InvariantCulture);
}
public BaseFont Font {
get {
if (font != null)
return font;
iTextSharp.text.Font ff2 = FontFactory.GetFont(faceName, BaseFont.CP1252, true, 10, ((italic != 0) ? iTextSharp.text.Font.ITALIC : 0) | ((bold != 0) ? iTextSharp.text.Font.BOLD : 0));
font = ff2.BaseFont;
if (font != null)
return font;
string fontName;
if (faceName.IndexOf("courier") != -1 || faceName.IndexOf("terminal") != -1
|| faceName.IndexOf("fixedsys") != -1) {
fontName = fontNames[MARKER_COURIER + italic + bold];
}
else if (faceName.IndexOf("ms sans serif") != -1 || faceName.IndexOf("arial") != -1
|| faceName.IndexOf("system") != -1) {
fontName = fontNames[MARKER_HELVETICA + italic + bold];
}
else if (faceName.IndexOf("arial black") != -1) {
fontName = fontNames[MARKER_HELVETICA + italic + MARKER_BOLD];
}
else if (faceName.IndexOf("times") != -1 || faceName.IndexOf("ms serif") != -1
|| faceName.IndexOf("roman") != -1) {
fontName = fontNames[MARKER_TIMES + italic + bold];
}
else if (faceName.IndexOf("symbol") != -1) {
fontName = fontNames[MARKER_SYMBOL];
}
else {
int pitch = pitchAndFamily & 3;
int family = (pitchAndFamily >> 4) & 7;
switch (family) {
case FF_MODERN:
fontName = fontNames[MARKER_COURIER + italic + bold];
break;
case FF_ROMAN:
fontName = fontNames[MARKER_TIMES + italic + bold];
break;
case FF_SWISS:
case FF_SCRIPT:
case FF_DECORATIVE:
fontName = fontNames[MARKER_HELVETICA + italic + bold];
break;
default: {
switch (pitch) {
case FIXED_PITCH:
fontName = fontNames[MARKER_COURIER + italic + bold];
break;
default:
fontName = fontNames[MARKER_HELVETICA + italic + bold];
break;
}
break;
}
}
}
font = BaseFont.CreateFont(fontName, BaseFont.CP1252, false);
return font;
}
}
public float Angle {
get {
return angle;
}
}
public bool IsUnderline() {
return underline;
}
public bool IsStrikeout() {
return strikeout;
}
public float GetFontSize(MetaState state) {
return Math.Abs(state.TransformY(height) - state.TransformY(0)) * Document.WmfFontCorrection;
}
}
}

View File

@@ -0,0 +1,76 @@
using System;
/*
* $Id: MetaObject.cs,v 1.3 2008/05/13 11:25:37 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf
{
public class MetaObject
{
public const int META_NOT_SUPPORTED = 0;
public const int META_PEN = 1;
public const int META_BRUSH = 2;
public const int META_FONT = 3;
public int type = META_NOT_SUPPORTED;
public MetaObject() {
}
public MetaObject(int type) {
this.type = type;
}
public int Type {
get {
return type;
}
}
}
}

View File

@@ -0,0 +1,98 @@
using System;
using iTextSharp.text;
/*
* $Id: MetaPen.cs,v 1.3 2008/05/13 11:25:37 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf {
public class MetaPen : MetaObject {
public const int PS_SOLID = 0;
public const int PS_DASH = 1;
public const int PS_DOT = 2;
public const int PS_DASHDOT = 3;
public const int PS_DASHDOTDOT = 4;
public const int PS_NULL = 5;
public const int PS_INSIDEFRAME = 6;
int style = PS_SOLID;
int penWidth = 1;
Color color = Color.BLACK;
public MetaPen() {
type = META_PEN;
}
public void Init(InputMeta meta) {
style = meta.ReadWord();
penWidth = meta.ReadShort();
meta.ReadWord();
color = meta.ReadColor();
}
public int Style {
get {
return style;
}
}
public int PenWidth {
get {
return penWidth;
}
}
public Color Color {
get {
return color;
}
}
}
}

View File

@@ -0,0 +1,390 @@
using System;
using System.Collections;
using iTextSharp.text;
/*
* $Id: MetaState.cs,v 1.6 2008/05/13 11:25:37 psoares33 Exp $
*
*
* Copyright 2001, 2002 Paulo Soares
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf.codec.wmf {
public class MetaState {
public static int TA_NOUPDATECP = 0;
public static int TA_UPDATECP = 1;
public static int TA_LEFT = 0;
public static int TA_RIGHT = 2;
public static int TA_CENTER = 6;
public static int TA_TOP = 0;
public static int TA_BOTTOM = 8;
public static int TA_BASELINE = 24;
public static int TRANSPARENT = 1;
public static int OPAQUE = 2;
public static int ALTERNATE = 1;
public static int WINDING = 2;
public Stack savedStates;
public ArrayList MetaObjects;
public System.Drawing.Point currentPoint;
public MetaPen currentPen;
public MetaBrush currentBrush;
public MetaFont currentFont;
public Color currentBackgroundColor = Color.WHITE;
public Color currentTextColor = Color.BLACK;
public int backgroundMode = OPAQUE;
public int polyFillMode = ALTERNATE;
public int lineJoin = 1;
public int textAlign;
public int offsetWx;
public int offsetWy;
public int extentWx;
public int extentWy;
public float scalingX;
public float scalingY;
/** Creates new MetaState */
public MetaState() {
savedStates = new Stack();
MetaObjects = new ArrayList();
currentPoint = new System.Drawing.Point(0, 0);
currentPen = new MetaPen();
currentBrush = new MetaBrush();
currentFont = new MetaFont();
}
public MetaState(MetaState state) {
metaState = state;
}
public MetaState metaState {
set {
savedStates = value.savedStates;
MetaObjects = value.MetaObjects;
currentPoint = value.currentPoint;
currentPen = value.currentPen;
currentBrush = value.currentBrush;
currentFont = value.currentFont;
currentBackgroundColor = value.currentBackgroundColor;
currentTextColor = value.currentTextColor;
backgroundMode = value.backgroundMode;
polyFillMode = value.polyFillMode;
textAlign = value.textAlign;
lineJoin = value.lineJoin;
offsetWx = value.offsetWx;
offsetWy = value.offsetWy;
extentWx = value.extentWx;
extentWy = value.extentWy;
scalingX = value.scalingX;
scalingY = value.scalingY;
}
}
public void AddMetaObject(MetaObject obj) {
for (int k = 0; k < MetaObjects.Count; ++k) {
if (MetaObjects[k] == null) {
MetaObjects[k] = obj;
return;
}
}
MetaObjects.Add(obj);
}
public void SelectMetaObject(int index, PdfContentByte cb) {
MetaObject obj = (MetaObject)MetaObjects[index];
if (obj == null)
return;
int style;
switch (obj.Type) {
case MetaObject.META_BRUSH:
currentBrush = (MetaBrush)obj;
style = currentBrush.Style;
if (style == MetaBrush.BS_SOLID) {
Color color = currentBrush.Color;
cb.SetColorFill(color);
}
else if (style == MetaBrush.BS_HATCHED) {
Color color = currentBackgroundColor;
cb.SetColorFill(color);
}
break;
case MetaObject.META_PEN: {
currentPen = (MetaPen)obj;
style = currentPen.Style;
if (style != MetaPen.PS_NULL) {
Color color = currentPen.Color;
cb.SetColorStroke(color);
cb.SetLineWidth(Math.Abs((float)currentPen.PenWidth * scalingX / extentWx));
switch (style) {
case MetaPen.PS_DASH:
cb.SetLineDash(18, 6, 0);
break;
case MetaPen.PS_DASHDOT:
cb.SetLiteral("[9 6 3 6]0 d\n");
break;
case MetaPen.PS_DASHDOTDOT:
cb.SetLiteral("[9 3 3 3 3 3]0 d\n");
break;
case MetaPen.PS_DOT:
cb.SetLineDash(3, 0);
break;
default:
cb.SetLineDash(0);
break;
}
}
break;
}
case MetaObject.META_FONT: {
currentFont = (MetaFont)obj;
break;
}
}
}
public void DeleteMetaObject(int index) {
MetaObjects[index] = null;
}
public void SaveState(PdfContentByte cb) {
cb.SaveState();
MetaState state = new MetaState(this);
savedStates.Push(state);
}
public void RestoreState(int index, PdfContentByte cb) {
int pops;
if (index < 0)
pops = Math.Min(-index, savedStates.Count);
else
pops = Math.Max(savedStates.Count - index, 0);
if (pops == 0)
return;
MetaState state = null;
while (pops-- != 0) {
cb.RestoreState();
state = (MetaState)savedStates.Pop();
}
metaState = state;
}
public void Cleanup(PdfContentByte cb) {
int k = savedStates.Count;
while (k-- > 0)
cb.RestoreState();
}
public float TransformX(int x) {
return ((float)x - offsetWx) * scalingX / extentWx;
}
public float TransformY(int y) {
return (1f - ((float)y - offsetWy) / extentWy) * scalingY;
}
public float ScalingX {
set {
this.scalingX = value;
}
}
public float ScalingY {
set {
this.scalingY = value;
}
}
public int OffsetWx {
set {
this.offsetWx = value;
}
}
public int OffsetWy {
set {
this.offsetWy = value;
}
}
public int ExtentWx {
set {
this.extentWx = value;
}
}
public int ExtentWy {
set {
this.extentWy = value;
}
}
public float TransformAngle(float angle) {
float ta = scalingY < 0 ? -angle : angle;
return (float)(scalingX < 0 ? Math.PI - ta : ta);
}
public System.Drawing.Point CurrentPoint {
get {
return currentPoint;
}
set {
currentPoint = value;
}
}
public MetaBrush CurrentBrush {
get {
return currentBrush;
}
}
public MetaPen CurrentPen {
get {
return currentPen;
}
}
public MetaFont CurrentFont {
get {
return currentFont;
}
}
/** Getter for property currentBackgroundColor.
* @return Value of property currentBackgroundColor.
*/
public Color CurrentBackgroundColor {
get {
return currentBackgroundColor;
}
set {
this.currentBackgroundColor = value;
}
}
/** Getter for property currentTextColor.
* @return Value of property currentTextColor.
*/
public Color CurrentTextColor {
get {
return currentTextColor;
}
set {
this.currentTextColor = value;
}
}
/** Getter for property backgroundMode.
* @return Value of property backgroundMode.
*/
public int BackgroundMode {
get {
return backgroundMode;
}
set {
this.backgroundMode = value;
}
}
/** Getter for property textAlign.
* @return Value of property textAlign.
*/
public int TextAlign {
get {
return textAlign;
}
set {
this.textAlign = value;
}
}
/** Getter for property polyFillMode.
* @return Value of property polyFillMode.
*/
public int PolyFillMode {
get {
return polyFillMode;
}
set {
this.polyFillMode = value;
}
}
public PdfContentByte LineJoinRectangle {
set {
if (lineJoin != 0) {
lineJoin = 0;
value.SetLineJoin(0);
}
}
}
public PdfContentByte LineJoinPolygon {
set {
if (lineJoin == 0) {
lineJoin = 1;
value.SetLineJoin(1);
}
}
}
public bool LineNeutral {
get {
return (lineJoin == 0);
}
}
}
}