Fixed the bug where the gamma readings where all over the place

This commit is contained in:
2022-09-29 13:20:41 +02:00
parent 2362a7041c
commit 4899d9f51f

View File

@@ -19,12 +19,17 @@ namespace TEST_DecodePng
const int CHUNCK_TYPE_FIELD_LENGTH = 4;
const int CHUNCK_CRC_FIELD_LENGTH = 4;
const int IMAGE_WIDTH_FIELD_SIZE = 4;
const int IMAGE_HEIGHT_FIELD_SIZE = 4;
const int GAMA_FILED_LENGTH = 4;
//Critical chunks
const string FIRST_CHUNK_TYPE = "IHDR";
const string LAST_CHUNCK_TYPE = "IEND";
const string DATA_CHUNCK_TYPE = "IDAT";
const int IMAGE_WIDTH_FIELD_SIZE = 4;
const int IMAGE_HEIGHT_FIELD_SIZE = 4;
const string GAMA_CHUNCK_TYPE = "GAMA";
public Stopwatch chrono;
public PngDecoder()
@@ -62,7 +67,7 @@ namespace TEST_DecodePng
int chunckSize = 0;
string chunckType = "";
while (chunckType != "IEND")
while (chunckType.ToLower() != LAST_CHUNCK_TYPE.ToLower())
{
//Reading Chunck Size (Carefull, does not contain the size, type and crc bytes)
byte[] ckln = new byte[] { content[cursor], content[cursor + 1], content[cursor + 2], content[cursor + 3] };
@@ -117,6 +122,7 @@ namespace TEST_DecodePng
PngMetaData header = null;
float gama = 0;
//Now we can process them
for (int chunckCounter = 0; chunckCounter < chuncks.Count; chunckCounter++)
@@ -125,8 +131,8 @@ namespace TEST_DecodePng
switch (cktype.ToUpper())
{
case "IHDR":
//Unrecognized chunck, as said in the doc we should not stop the decoding if this happends
//IHDR
case FIRST_CHUNK_TYPE:
int chunckCursor = 0;
byte[] btaImageWidth = new byte[IMAGE_WIDTH_FIELD_SIZE];
@@ -165,9 +171,21 @@ namespace TEST_DecodePng
if (!header.IsValid())
throw new InvalidPngHeadersFileException("Header invalide");
break;
case GAMA_CHUNCK_TYPE:
byte[] btGamma = new byte[GAMA_FILED_LENGTH];
//btGamma = new byte[] { chuncks[chunckCounter][0], chuncks[chunckCounter][1], chuncks[chunckCounter][2], chuncks[chunckCounter][3] };
for (int i = 0; i < GAMA_FILED_LENGTH; i++)
{
btGamma[i] = chuncks[chunckCounter][i];
}
//btGamma.Reverse();
Array.Reverse(btGamma);
gama = ((float)BitConverter.ToInt32(btGamma,0) / 1000F);
break;
default:
//Unrecognized chunck, as said in the doc should not stop the decoding
break;
}
}
@@ -180,7 +198,8 @@ namespace TEST_DecodePng
Environment.NewLine + "color type : " + header.ColorType +
Environment.NewLine + "compression method : " + header.CompressionMethod +
Environment.NewLine + "filter method : " + header.FilterMethod +
Environment.NewLine + "interface method : " + header.InterlaceMethod
Environment.NewLine + "interface method : " + header.InterlaceMethod +
Environment.NewLine + "gama : " + gama
);
}