Added custom exception to replace all the 'return null' statements
This commit is contained in:
@@ -80,7 +80,14 @@ namespace TEST_DecodePng
|
||||
private void btnDecode_Click(object sender, EventArgs e)
|
||||
{
|
||||
PngDecoder decoder = new PngDecoder();
|
||||
decoder.Decode(selectedFile);
|
||||
try{
|
||||
decoder.Decode(selectedFile);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
TEST_DecodePng/InvalidPngHeadersFileException.cs
Normal file
24
TEST_DecodePng/InvalidPngHeadersFileException.cs
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ namespace TEST_DecodePng
|
||||
|
||||
//Checks if the file has a valid png signature
|
||||
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
|
||||
|
||||
@@ -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 (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
|
||||
|
||||
@@ -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
|
||||
|
||||
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)
|
||||
{
|
||||
case 0:
|
||||
//Each pixel is a grayscale sample.
|
||||
if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16)
|
||||
return null;
|
||||
throw (incoherentColorsAndBitDepths);
|
||||
break;
|
||||
case 2:
|
||||
//Each pixel is an R,G,B triple.
|
||||
if (bitDepth != 8 && bitDepth != 16)
|
||||
return null;
|
||||
throw (incoherentColorsAndBitDepths);
|
||||
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;
|
||||
throw (incoherentColorsAndBitDepths);
|
||||
break;
|
||||
case 4:
|
||||
//Each pixel is a grayscale sample, followed by an alpha sample.
|
||||
if (bitDepth != 8 && bitDepth != 16)
|
||||
return null;
|
||||
throw (incoherentColorsAndBitDepths);
|
||||
break;
|
||||
case 6:
|
||||
//Each pixel is an R,G,B triple, followed by an alpha sample.
|
||||
if (bitDepth != 8 && bitDepth != 16)
|
||||
return null;
|
||||
throw (incoherentColorsAndBitDepths);
|
||||
break;
|
||||
default:
|
||||
//Only 0,2,3,4 and 6 are valid color type values
|
||||
return null;
|
||||
throw (incoherentColorsAndBitDepths);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
//Insert code
|
||||
|
||||
MessageBox.Show("Image width: " + imageWidth +
|
||||
Environment.NewLine + "Image height: " + imageHeight +
|
||||
@@ -165,16 +167,6 @@ namespace TEST_DecodePng
|
||||
//TO REMOVE
|
||||
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)
|
||||
{
|
||||
if (firstArray.Length != secondArray.Length)
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
<Compile Include="Form1.Designer.cs">
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="InvalidPngHeadersFileException.cs" />
|
||||
<Compile Include="PngDecoder.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Reference in New Issue
Block a user