Skip to content

Commit

Permalink
Use custom exception class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrant committed Oct 22, 2023
1 parent e1e60aa commit 6d8252a
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 53 deletions.
2 changes: 1 addition & 1 deletion ImageFormats/CutReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static Image Load(Stream stream)
Util.LittleEndian(reader.ReadUInt16()); //reserved word

if ((imgWidth < 1) || (imgHeight < 1) || (imgWidth > 32767) || (imgHeight > 32767))
throw new ApplicationException("This CUT file appears to have invalid dimensions.");
throw new ImageDecodeException("This CUT file appears to have invalid dimensions.");

byte[] bmpData = new byte[imgWidth * 4 * imgHeight];

Expand Down
22 changes: 11 additions & 11 deletions ImageFormats/DicomReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static Image Load(Stream stream)
//check signature...
string signature = System.Text.Encoding.ASCII.GetString(tempBytes, 0, 4);
if (!signature.Equals("DICM"))
throw new ApplicationException("Not a valid DICOM file.");
throw new ImageDecodeException("Not a valid DICOM file.");

int imgWidth = 0, imgHeight = 0;
int samplesPerPixel = 0, numFrames = 0, bitsPerSample = 0, bitsStored = 0;
Expand All @@ -75,7 +75,7 @@ public static Image Load(Stream stream)
//read the meta-group, and determine stuff from it
int metaGroupLen = (int)Util.LittleEndian(BitConverter.ToUInt32(tempBytes, 0xC));
if(metaGroupLen > 10000)
throw new ApplicationException("Meta group is a bit too long. May not be a valid DICOM file.");
throw new ImageDecodeException("Meta group is a bit too long. May not be a valid DICOM file.");

tempBytes = new byte[metaGroupLen];
stream.Read(tempBytes, 0, metaGroupLen);
Expand All @@ -94,9 +94,9 @@ public static Image Load(Stream stream)


if(isRLE)
throw new ApplicationException("RLE-encoded DICOM images are not supported.");
throw new ImageDecodeException("RLE-encoded DICOM images are not supported.");
if (isJPEG)
throw new ApplicationException("JPEG-encoded DICOM images are not supported.");
throw new ImageDecodeException("JPEG-encoded DICOM images are not supported.");


//get header information:
Expand Down Expand Up @@ -228,7 +228,7 @@ public static Image Load(Stream stream)


if (dataLength == 0)
throw new ApplicationException("DICOM file does not appear to have any image data.");
throw new ImageDecodeException("DICOM file does not appear to have any image data.");


MemoryStream dataStream = new MemoryStream(data);
Expand All @@ -245,10 +245,10 @@ public static Image Load(Stream stream)
numFrames = 1;

if (samplesPerPixel > 4)
throw new ApplicationException("Do not support greater than 4 samples per pixel.");
throw new ImageDecodeException("Do not support greater than 4 samples per pixel.");

if ((bitsPerSample != 8) && (bitsPerSample != 16) && (bitsPerSample != 32))
throw new ApplicationException("Invalid bits per sample.");
throw new ImageDecodeException("Invalid bits per sample.");

byte[] bmpData = new byte[imgWidth * 4 * imgHeight];

Expand Down Expand Up @@ -425,25 +425,25 @@ private static UInt32 getNumeric(BinaryReader reader, int groupNumber, bool bigE
if (v1 == 'U' && v2 == 'S')
{
if (len != 2)
throw new ApplicationException("Incorrect size for a US field.");
throw new ImageDecodeException("Incorrect size for a US field.");
ret = getShort(reader, groupNumber, bigEndian);
}
else if (v1 == 'U' && v2 == 'L')
{
if (len != 4)
throw new ApplicationException("Incorrect size for a UL field.");
throw new ImageDecodeException("Incorrect size for a UL field.");
ret = getInt(reader, groupNumber, bigEndian);
}
else if (v1 == 'S' && v2 == 'S')
{
if (len != 2)
throw new ApplicationException("Incorrect size for a SS field.");
throw new ImageDecodeException("Incorrect size for a SS field.");
ret = getShort(reader, groupNumber, bigEndian);
}
else if (v1 == 'S' && v2 == 'L')
{
if (len != 4)
throw new ApplicationException("Incorrect size for a SL field.");
throw new ImageDecodeException("Incorrect size for a SL field.");
ret = getInt(reader, groupNumber, bigEndian);
}
else if (v1 == 'I' && v2 == 'S' && len < 16)
Expand Down
8 changes: 4 additions & 4 deletions ImageFormats/IffDeepReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public static Image Load(Stream stream, bool wantOpacity = false)
byte[] tempBytes = new byte[65536];

stream.Read(tempBytes, 0, 4);
if (Encoding.ASCII.GetString(tempBytes, 0, 4) != "FORM") { throw new ApplicationException("This is not a valid DEEP file."); }
if (Encoding.ASCII.GetString(tempBytes, 0, 4) != "FORM") { throw new ImageDecodeException("This is not a valid DEEP file."); }

uint chunkSize = Util.BigEndian(reader.ReadUInt32());

stream.Read(tempBytes, 0, 4);
string fileType = Encoding.ASCII.GetString(tempBytes, 0, 4);
if (fileType != "DEEP" && fileType != "TVPP") { throw new ApplicationException("This is not a valid DEEP file."); }
if (fileType != "DEEP" && fileType != "TVPP") { throw new ImageDecodeException("This is not a valid DEEP file."); }

while (stream.Position < stream.Length)
{
Expand Down Expand Up @@ -134,7 +134,7 @@ public static Image Load(Stream stream, bool wantOpacity = false)

if (imgWidth == -1 || imgHeight == -1)
{
throw new ApplicationException("Invalid format of DEEP file.");
throw new ImageDecodeException("Invalid format of DEEP file.");
}

byte[] bmpData = new byte[(imgWidth + 1) * 4 * imgHeight];
Expand Down Expand Up @@ -301,7 +301,7 @@ public static Image Load(Stream stream, bool wantOpacity = false)
}
else
{
throw new ApplicationException("Invalid compression type.");
throw new ImageDecodeException("Invalid compression type.");
}
}
catch (Exception e)
Expand Down
14 changes: 7 additions & 7 deletions ImageFormats/IffIlbmReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ public static Image Load(Stream stream, bool resizeForAspect = false)
byte[] tempBytes = new byte[65536];

stream.Read(tempBytes, 0, 4);
if (Encoding.ASCII.GetString(tempBytes, 0, 4) != "FORM") { throw new ApplicationException("This is not a valid ILBM file."); }
if (Encoding.ASCII.GetString(tempBytes, 0, 4) != "FORM") { throw new ImageDecodeException("This is not a valid ILBM file."); }

uint chunkSize = Util.BigEndian(reader.ReadUInt32());

stream.Read(tempBytes, 0, 4);
string fileType = Encoding.ASCII.GetString(tempBytes, 0, 4);
if (fileType != "ILBM" && !fileType.StartsWith("PBM") && fileType != "ACBM") { throw new ApplicationException("This is not a valid ILBM file."); }
if (fileType != "ILBM" && !fileType.StartsWith("PBM") && fileType != "ACBM") { throw new ImageDecodeException("This is not a valid ILBM file."); }
if (fileType.StartsWith("PBM")) { modePbm = true; }
else if (fileType == "ACBM") { modeAcbm = true; }

Expand Down Expand Up @@ -295,19 +295,19 @@ public static Image Load(Stream stream, bool resizeForAspect = false)

if (bodyChunkPosition < 0)
{
throw new ApplicationException("Image does not seem to contain a body chunk.");
throw new ImageDecodeException("Image does not seem to contain a body chunk.");
}

stream.Position = bodyChunkPosition;

if (imgWidth == -1 || imgHeight == -1 || (numPlanes > 12 && numPlanes != 24 && numPlanes != 32))
{
throw new ApplicationException("Invalid format of ILBM file.");
throw new ImageDecodeException("Invalid format of ILBM file.");
}

if (maskType == 1)
{
throw new ApplicationException("ILBM images with mask plane not yet implemented.");
throw new ImageDecodeException("ILBM images with mask plane not yet implemented.");
}

if (!haveCAMG)
Expand Down Expand Up @@ -411,7 +411,7 @@ public static Image Load(Stream stream, bool resizeForAspect = false)
}
else
{
throw new ApplicationException("Unsupported bit width: " + numPlanes);
throw new ImageDecodeException("Unsupported bit width: " + numPlanes);
}
}
else
Expand Down Expand Up @@ -549,7 +549,7 @@ public static Image Load(Stream stream, bool resizeForAspect = false)
}
else
{
throw new ApplicationException("Unsupported XBMI mode: " + modeXBMI);
throw new ImageDecodeException("Unsupported XBMI mode: " + modeXBMI);
}

bmpData[4 * (y * imgWidth + x)] = (byte)prevB;
Expand Down
8 changes: 4 additions & 4 deletions ImageFormats/IffRgbnReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public static Image Load(Stream stream)
byte[] tempBytes = new byte[65536];

stream.Read(tempBytes, 0, 4);
if (Encoding.ASCII.GetString(tempBytes, 0, 4) != "FORM") { throw new ApplicationException("This is not a valid RGBN file."); }
if (Encoding.ASCII.GetString(tempBytes, 0, 4) != "FORM") { throw new ImageDecodeException("This is not a valid RGBN file."); }

uint chunkSize = Util.BigEndian(reader.ReadUInt32());

stream.Read(tempBytes, 0, 4);
string fileType = Encoding.ASCII.GetString(tempBytes, 0, 4);
if (fileType != "RGBN" && fileType != "RGB8") { throw new ApplicationException("This is not a valid RGBN file."); }
if (fileType != "RGBN" && fileType != "RGB8") { throw new ImageDecodeException("This is not a valid RGBN file."); }

bool isRgb8 = fileType == "RGB8";

Expand Down Expand Up @@ -108,7 +108,7 @@ public static Image Load(Stream stream)

if (imgWidth == -1 || imgHeight == -1)
{
throw new ApplicationException("Invalid format of RGBN file.");
throw new ImageDecodeException("Invalid format of RGBN file.");
}

byte[] bmpData = new byte[(imgWidth + 1) * 4 * imgHeight];
Expand Down Expand Up @@ -170,7 +170,7 @@ public static Image Load(Stream stream)
}
else
{
throw new ApplicationException("Invalid compression type.");
throw new ImageDecodeException("Invalid compression type.");
}
}
catch (Exception e)
Expand Down
6 changes: 3 additions & 3 deletions ImageFormats/MacPaintReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public static Image Load(Stream stream)

if (headerBytes[0] != 0)
{
throw new ApplicationException("This is not a valid MacPaint file.");
throw new ImageDecodeException("This is not a valid MacPaint file.");
}

string fileType = Encoding.ASCII.GetString(headerBytes, 0x41, 4);
if (fileType != "PNTG")
{
throw new ApplicationException("This is not a valid MacPaint file.");
throw new ImageDecodeException("This is not a valid MacPaint file.");
}

int fileNameLen = headerBytes[1];
Expand All @@ -65,7 +65,7 @@ public static Image Load(Stream stream)
if (startMagic != 0x2)
{
// I have actually seen MacPaint files that do not have this magic value...
//throw new ApplicationException("This is not a valid MacPaint file.");
//throw new ImageDecodeException("This is not a valid MacPaint file.");
}

// Skip over pattern data
Expand Down
10 changes: 5 additions & 5 deletions ImageFormats/PcxReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public static Image Load(Stream stream, bool useCgaPalette = false)

byte tempByte = (byte)stream.ReadByte();
if (tempByte != 10)
throw new ApplicationException("This is not a valid PCX file.");
throw new ImageDecodeException("This is not a valid PCX file.");

var version = (byte)stream.ReadByte();
if (version > 5)
throw new ApplicationException("Only Version 5 or lower PCX files are supported.");
throw new ImageDecodeException("Only Version 5 or lower PCX files are supported.");

// This variable controls whether the bit plane values are interpreted as literal color states
// instead of indices into the palette. In other words, this controls whether the palette is
Expand All @@ -78,11 +78,11 @@ public static Image Load(Stream stream, bool useCgaPalette = false)

tempByte = (byte)stream.ReadByte();
if (tempByte != 1)
throw new ApplicationException("Invalid PCX compression type.");
throw new ImageDecodeException("Invalid PCX compression type.");

int imgBpp = stream.ReadByte();
if (imgBpp != 8 && imgBpp != 4 && imgBpp != 2 && imgBpp != 1)
throw new ApplicationException("Only 8, 4, 2, and 1-bit PCX samples are supported.");
throw new ImageDecodeException("Only 8, 4, 2, and 1-bit PCX samples are supported.");

UInt16 xmin = Util.LittleEndian(reader.ReadUInt16());
UInt16 ymin = Util.LittleEndian(reader.ReadUInt16());
Expand All @@ -93,7 +93,7 @@ public static Image Load(Stream stream, bool useCgaPalette = false)
int imgHeight = ymax - ymin + 1;

if ((imgWidth < 1) || (imgHeight < 1) || (imgWidth > 32767) || (imgHeight > 32767))
throw new ApplicationException("This PCX file appears to have invalid dimensions.");
throw new ImageDecodeException("This PCX file appears to have invalid dimensions.");

Util.LittleEndian(reader.ReadUInt16()); //hdpi
Util.LittleEndian(reader.ReadUInt16()); //vdpi
Expand Down
6 changes: 3 additions & 3 deletions ImageFormats/PnmReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public static Image Load(Stream inStream, bool bigEndian = true)
int bmpWidth, bmpHeight, bmpMaxVal;

//check if the format is correct...
if ((char)bytes[bytePtr++] != 'P') throw new ApplicationException("Incorrect file format.");
if ((char)bytes[bytePtr++] != 'P') throw new ImageDecodeException("Incorrect file format.");
pnmType = (char)bytes[bytePtr++];
if ((pnmType < '1') || (pnmType > '6')) throw new ApplicationException("Unrecognized bitmap type.");
if ((pnmType < '1') || (pnmType > '6')) throw new ImageDecodeException("Unrecognized bitmap type.");

ReadNextInts(bytes, ref bytePtr, lineInts, 2, out _);

Expand All @@ -87,7 +87,7 @@ public static Image Load(Stream inStream, bool bigEndian = true)

//check for nonsensical dimensions
if ((bmpWidth <= 0) || (bmpHeight <= 0) || (bmpMaxVal <= 0))
throw new ApplicationException("Invalid image dimensions.");
throw new ImageDecodeException("Invalid image dimensions.");

int numPixels = bmpWidth * bmpHeight;
int maxElementCount = numPixels * 4;
Expand Down
6 changes: 3 additions & 3 deletions ImageFormats/RasReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static Image Load(Stream stream)
BinaryReader reader = new BinaryReader(stream);
UInt32 tempDword = Util.BigEndian(reader.ReadUInt32());
if (tempDword != 0x59a66a95)
throw new ApplicationException("This is not a valid RAS file.");
throw new ImageDecodeException("This is not a valid RAS file.");

int imgWidth = (int)Util.BigEndian(reader.ReadUInt32());
int imgHeight = (int)Util.BigEndian(reader.ReadUInt32());
Expand All @@ -74,10 +74,10 @@ public static Image Load(Stream stream)
RleReader rleReader = new RleReader(stream, rasType == RAS_TYPE_RLE);

if ((imgWidth < 1) || (imgHeight < 1) || (imgWidth > 32767) || (imgHeight > 32767) || (mapLength > 32767))
throw new ApplicationException("This RAS file appears to have invalid dimensions.");
throw new ImageDecodeException("This RAS file appears to have invalid dimensions.");

if ((imgBpp != 32) && (imgBpp != 24) && (imgBpp != 8) && (imgBpp != 4) && (imgBpp != 1))
throw new ApplicationException("Only 1, 4, 8, 24, and 32 bit images are supported.");
throw new ImageDecodeException("Only 1, 4, 8, 24, and 32 bit images are supported.");

byte[] bmpData = new byte[imgWidth * 4 * imgHeight];

Expand Down
10 changes: 5 additions & 5 deletions ImageFormats/SgiReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ public static Image Load(Stream stream)

UInt16 magic = Util.BigEndian(reader.ReadUInt16());
if (magic != 0x1DA)
throw new ApplicationException("Not a valid SGI file.");
throw new ImageDecodeException("Not a valid SGI file.");

int compressionType = stream.ReadByte();
int bytesPerComponent = stream.ReadByte();
UInt16 dimension = Util.BigEndian(reader.ReadUInt16());

if(compressionType > 1)
throw new ApplicationException("Unsupported compression type.");
throw new ImageDecodeException("Unsupported compression type.");
if (bytesPerComponent != 1)
throw new ApplicationException("Unsupported bytes per component.");
throw new ImageDecodeException("Unsupported bytes per component.");
if (dimension != 1 && dimension != 2 && dimension != 3)
throw new ApplicationException("Unsupported dimension.");
throw new ImageDecodeException("Unsupported dimension.");

int imgWidth = Util.BigEndian(reader.ReadUInt16());
int imgHeight = Util.BigEndian(reader.ReadUInt16());
Expand All @@ -76,7 +76,7 @@ public static Image Load(Stream stream)
UInt32 pixMax = Util.BigEndian(reader.ReadUInt32());

if ((imgWidth < 1) || (imgHeight < 1) || (imgWidth > 32767) || (imgHeight > 32767))
throw new ApplicationException("This SGI file appears to have invalid dimensions.");
throw new ImageDecodeException("This SGI file appears to have invalid dimensions.");

stream.Seek(4, SeekOrigin.Current);

Expand Down
Loading

0 comments on commit 6d8252a

Please sign in to comment.