Fixed the bug where the IDHD header would return mystic data and added full decoding of it. Also added the checks to prevent unrecognized code
This commit is contained in:
@@ -74,7 +74,7 @@ namespace TEST_DecodePng
|
|||||||
|
|
||||||
//As it is the first chunck, his structure is a little bit different
|
//As it is the first chunck, his structure is a little bit different
|
||||||
|
|
||||||
int imageWidth, imageHeight, bitDepth, colorType, compressionMethod, filterMethod, interfaceMethod;
|
int imageWidth, imageHeight, bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
|
||||||
|
|
||||||
byte[] btaImageWidth = new byte[IMAGE_WIDTH_FIELD_SIZE];
|
byte[] btaImageWidth = new byte[IMAGE_WIDTH_FIELD_SIZE];
|
||||||
byte[] btaImageHeight = new byte[IMAGE_HEIGHT_FIELD_SIZE];
|
byte[] btaImageHeight = new byte[IMAGE_HEIGHT_FIELD_SIZE];
|
||||||
@@ -92,10 +92,13 @@ namespace TEST_DecodePng
|
|||||||
//Those are in little endien notation so we need to reverse it
|
//Those are in little endien notation so we need to reverse it
|
||||||
Array.Reverse(btaImageWidth);
|
Array.Reverse(btaImageWidth);
|
||||||
Array.Reverse(btaImageHeight);
|
Array.Reverse(btaImageHeight);
|
||||||
imageWidth = BitConverter.ToInt32(btaImageWidth,0);
|
imageWidth = BitConverter.ToInt32(btaImageWidth, 0);
|
||||||
imageHeight = BitConverter.ToInt32(btaImageHeight,0);
|
imageHeight = BitConverter.ToInt32(btaImageHeight, 0);
|
||||||
|
|
||||||
|
//As specified in the documentation, images with 0 widht or height are invalid
|
||||||
|
if (imageWidth == 0 || imageHeight == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
cursor++;
|
|
||||||
bitDepth = Convert.ToInt32(content[cursor]);
|
bitDepth = Convert.ToInt32(content[cursor]);
|
||||||
cursor++;
|
cursor++;
|
||||||
colorType = Convert.ToInt32(content[cursor]);
|
colorType = Convert.ToInt32(content[cursor]);
|
||||||
@@ -104,7 +107,46 @@ namespace TEST_DecodePng
|
|||||||
cursor++;
|
cursor++;
|
||||||
filterMethod = Convert.ToInt32(content[cursor]);
|
filterMethod = Convert.ToInt32(content[cursor]);
|
||||||
cursor++;
|
cursor++;
|
||||||
interfaceMethod = Convert.ToInt32(content[cursor]);
|
interlaceMethod = Convert.ToInt32(content[cursor]);
|
||||||
|
|
||||||
|
//Those are all the rules seen on the documentation at : https://www.w3.org/TR/PNG-Chunks.html
|
||||||
|
|
||||||
|
if (compressionMethod != 0 || filterMethod != 0 || interlaceMethod != 0 && interlaceMethod != 1)
|
||||||
|
return null;
|
||||||
|
switch (colorType)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
//Each pixel is a grayscale sample.
|
||||||
|
if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16)
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
//Each pixel is an R,G,B triple.
|
||||||
|
if (bitDepth != 8 && bitDepth != 16)
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
//Each pixel is a palette index, a PLTE chunk must appear.
|
||||||
|
if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8)
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
//Each pixel is a grayscale sample, followed by an alpha sample.
|
||||||
|
if (bitDepth != 8 && bitDepth != 16)
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
//Each pixel is an R,G,B triple, followed by an alpha sample.
|
||||||
|
if (bitDepth != 8 && bitDepth != 16)
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
//Only 0,2,3,4 and 6 are valid color type values
|
||||||
|
return null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MessageBox.Show("Image width: " + imageWidth +
|
MessageBox.Show("Image width: " + imageWidth +
|
||||||
Environment.NewLine + "Image height: " + imageHeight +
|
Environment.NewLine + "Image height: " + imageHeight +
|
||||||
@@ -112,11 +154,10 @@ namespace TEST_DecodePng
|
|||||||
Environment.NewLine + "color type : " + colorType +
|
Environment.NewLine + "color type : " + colorType +
|
||||||
Environment.NewLine + "compression method : " + compressionMethod +
|
Environment.NewLine + "compression method : " + compressionMethod +
|
||||||
Environment.NewLine + "filter method : " + filterMethod +
|
Environment.NewLine + "filter method : " + filterMethod +
|
||||||
Environment.NewLine + "interface method : " + interfaceMethod
|
Environment.NewLine + "interface method : " + interlaceMethod
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Timing Stuff
|
//Timing Stuff
|
||||||
chrono.Stop();
|
chrono.Stop();
|
||||||
//Timing Stuff
|
//Timing Stuff
|
||||||
|
|||||||
Reference in New Issue
Block a user