Added custom exception to replace all the 'return null' statements

This commit is contained in:
2022-09-22 15:35:24 +02:00
parent 4c0a2bc3d2
commit 4eae601fc7
4 changed files with 45 additions and 21 deletions
+7
View File
@@ -80,7 +80,14 @@ namespace TEST_DecodePng
private void btnDecode_Click(object sender, EventArgs e) private void btnDecode_Click(object sender, EventArgs e)
{ {
PngDecoder decoder = new PngDecoder(); PngDecoder decoder = new PngDecoder();
try{
decoder.Decode(selectedFile); decoder.Decode(selectedFile);
} }
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
} }
} }
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TEST_DecodePng
{
public class InvalidPngHeadersFileException : Exception
{
public InvalidPngHeadersFileException()
{
//empty
}
public InvalidPngHeadersFileException(string message):base(message)
{
//empty
}
public InvalidPngHeadersFileException(string message, Exception inner):base(message,inner)
{
//empty
}
}
}
+12 -20
View File
@@ -48,7 +48,7 @@ namespace TEST_DecodePng
//Checks if the file has a valid png signature //Checks if the file has a valid png signature
if (!compareByteArrays(EXPECTED_PNG_SIGNATURE, pngSignature)) if (!compareByteArrays(EXPECTED_PNG_SIGNATURE, pngSignature))
return null; throw (new InvalidPngHeadersFileException("File signature not recognized"));
//We are now sure that we are working with a png file and we will then read chuncks //We are now sure that we are working with a png file and we will then read chuncks
@@ -70,7 +70,7 @@ namespace TEST_DecodePng
//If the first chunck is not the IHDR that means that we should not continue to decompress it //If the first chunck is not the IHDR that means that we should not continue to decompress it
if (ckType != FIRST_CHUNK_TYPE) if (ckType != FIRST_CHUNK_TYPE)
return null; throw (new InvalidPngHeadersFileException("First chunck not valid, possible corruption of the file"));
//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
@@ -112,41 +112,43 @@ namespace TEST_DecodePng
//Those are all the rules seen on the documentation at : https://www.w3.org/TR/PNG-Chunks.html //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) if (compressionMethod != 0 || filterMethod != 0 || interlaceMethod != 0 && interlaceMethod != 1)
return null; throw (new InvalidPngHeadersFileException("Incoherent IHDR chunck informations"));
InvalidPngHeadersFileException incoherentColorsAndBitDepths = new InvalidPngHeadersFileException("Incoherent colorType and bitDepths, could be due to corruption of the file");
switch (colorType) switch (colorType)
{ {
case 0: case 0:
//Each pixel is a grayscale sample. //Each pixel is a grayscale sample.
if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16) if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16)
return null; throw (incoherentColorsAndBitDepths);
break; break;
case 2: case 2:
//Each pixel is an R,G,B triple. //Each pixel is an R,G,B triple.
if (bitDepth != 8 && bitDepth != 16) if (bitDepth != 8 && bitDepth != 16)
return null; throw (incoherentColorsAndBitDepths);
break; break;
case 3: case 3:
//Each pixel is a palette index, a PLTE chunk must appear. //Each pixel is a palette index, a PLTE chunk must appear.
if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8) if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8)
return null; throw (incoherentColorsAndBitDepths);
break; break;
case 4: case 4:
//Each pixel is a grayscale sample, followed by an alpha sample. //Each pixel is a grayscale sample, followed by an alpha sample.
if (bitDepth != 8 && bitDepth != 16) if (bitDepth != 8 && bitDepth != 16)
return null; throw (incoherentColorsAndBitDepths);
break; break;
case 6: case 6:
//Each pixel is an R,G,B triple, followed by an alpha sample. //Each pixel is an R,G,B triple, followed by an alpha sample.
if (bitDepth != 8 && bitDepth != 16) if (bitDepth != 8 && bitDepth != 16)
return null; throw (incoherentColorsAndBitDepths);
break; break;
default: default:
//Only 0,2,3,4 and 6 are valid color type values //Only 0,2,3,4 and 6 are valid color type values
return null; throw (incoherentColorsAndBitDepths);
break; break;
} }
//Insert code
MessageBox.Show("Image width: " + imageWidth + MessageBox.Show("Image width: " + imageWidth +
Environment.NewLine + "Image height: " + imageHeight + Environment.NewLine + "Image height: " + imageHeight +
@@ -165,16 +167,6 @@ namespace TEST_DecodePng
//TO REMOVE //TO REMOVE
return new Bitmap(100, 100); return new Bitmap(100, 100);
} }
public byte[] ReadChunck()
{
//Must always return two values in the array or null
//TO REMOVE
return new byte[] { 0, 0 };
}
public bool compareByteArrays(byte[] firstArray, byte[] secondArray) public bool compareByteArrays(byte[] firstArray, byte[] secondArray)
{ {
if (firstArray.Length != secondArray.Length) if (firstArray.Length != secondArray.Length)
+1
View File
@@ -52,6 +52,7 @@
<Compile Include="Form1.Designer.cs"> <Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="InvalidPngHeadersFileException.cs" />
<Compile Include="PngDecoder.cs" /> <Compile Include="PngDecoder.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />