void ReadPng()
{
for (int i = 0; i < PNGID.Length; i++) {
if (PNGID[i] != isp.ReadByte()) {
throw new IOException(MessageLocalization.GetComposedMessage("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(MessageLocalization.GetComposedMessage("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(MessageLocalization.GetComposedMessage("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);
}
}