Skip to content

Commit e0ae4ba

Browse files
committed
Use custom exception class
1 parent d597832 commit e0ae4ba

14 files changed

+59
-53
lines changed

Diff for: ImageFormats/CutReader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static Image Load(Stream stream)
6060
Util.LittleEndian(reader.ReadUInt16()); // reserved word
6161

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

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

Diff for: ImageFormats/DicomReader.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static Image Load(Stream stream)
6262
// check signature
6363
string signature = System.Text.Encoding.ASCII.GetString(tempBytes, 0, 4);
6464
if (!signature.Equals("DICM"))
65-
throw new ApplicationException("Not a valid DICOM file.");
65+
throw new ImageDecodeException("Not a valid DICOM file.");
6666

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

7979
tempBytes = new byte[metaGroupLen];
8080
stream.Read(tempBytes, 0, metaGroupLen);
@@ -93,9 +93,9 @@ public static Image Load(Stream stream)
9393

9494

9595
if (isRLE)
96-
throw new ApplicationException("RLE-encoded DICOM images are not supported.");
96+
throw new ImageDecodeException("RLE-encoded DICOM images are not supported.");
9797
if (isJPEG)
98-
throw new ApplicationException("JPEG-encoded DICOM images are not supported.");
98+
throw new ImageDecodeException("JPEG-encoded DICOM images are not supported.");
9999

100100

101101
// get header information:
@@ -227,7 +227,7 @@ public static Image Load(Stream stream)
227227

228228

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

232232

233233
MemoryStream dataStream = new MemoryStream(data);
@@ -244,10 +244,10 @@ public static Image Load(Stream stream)
244244
numFrames = 1;
245245

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

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

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

@@ -422,25 +422,25 @@ private static UInt32 getNumeric(BinaryReader reader, int groupNumber, bool bigE
422422
if (v1 == 'U' && v2 == 'S')
423423
{
424424
if (len != 2)
425-
throw new ApplicationException("Incorrect size for a US field.");
425+
throw new ImageDecodeException("Incorrect size for a US field.");
426426
ret = getShort(reader, groupNumber, bigEndian);
427427
}
428428
else if (v1 == 'U' && v2 == 'L')
429429
{
430430
if (len != 4)
431-
throw new ApplicationException("Incorrect size for a UL field.");
431+
throw new ImageDecodeException("Incorrect size for a UL field.");
432432
ret = getInt(reader, groupNumber, bigEndian);
433433
}
434434
else if (v1 == 'S' && v2 == 'S')
435435
{
436436
if (len != 2)
437-
throw new ApplicationException("Incorrect size for a SS field.");
437+
throw new ImageDecodeException("Incorrect size for a SS field.");
438438
ret = getShort(reader, groupNumber, bigEndian);
439439
}
440440
else if (v1 == 'S' && v2 == 'L')
441441
{
442442
if (len != 4)
443-
throw new ApplicationException("Incorrect size for a SL field.");
443+
throw new ImageDecodeException("Incorrect size for a SL field.");
444444
ret = getInt(reader, groupNumber, bigEndian);
445445
}
446446
else if (v1 == 'I' && v2 == 'S' && len < 16)

Diff for: ImageFormats/IffDeepReader.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ public static Image Load(Stream stream, bool wantOpacity = false)
7070
byte[] tempBytes = new byte[65536];
7171

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

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

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

8181
while (stream.Position < stream.Length)
8282
{
@@ -136,7 +136,7 @@ public static Image Load(Stream stream, bool wantOpacity = false)
136136

137137
if (imgWidth == -1 || imgHeight == -1)
138138
{
139-
throw new ApplicationException("Invalid format of DEEP file.");
139+
throw new ImageDecodeException("Invalid format of DEEP file.");
140140
}
141141

142142
byte[] bmpData = new byte[(imgWidth + 1) * 4 * imgHeight];
@@ -302,7 +302,7 @@ public static Image Load(Stream stream, bool wantOpacity = false)
302302
}
303303
else
304304
{
305-
throw new ApplicationException("Invalid compression type.");
305+
throw new ImageDecodeException("Invalid compression type.");
306306
}
307307
}
308308
catch (Exception e)

Diff for: ImageFormats/IffIlbmReader.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ public static Image Load(Stream stream, bool resizeForAspect = false)
8080
byte[] tempBytes = new byte[65536];
8181

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

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

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

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

296296
if (bodyChunkPosition < 0)
297297
{
298-
throw new ApplicationException("Image does not seem to contain a body chunk.");
298+
throw new ImageDecodeException("Image does not seem to contain a body chunk.");
299299
}
300300

301301
stream.Position = bodyChunkPosition;
302302

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

308308
if (maskType == 1)
309309
{
310-
throw new ApplicationException("ILBM images with mask plane not yet implemented.");
310+
throw new ImageDecodeException("ILBM images with mask plane not yet implemented.");
311311
}
312312

313313
if (!haveCAMG)
@@ -411,7 +411,7 @@ public static Image Load(Stream stream, bool resizeForAspect = false)
411411
}
412412
else
413413
{
414-
throw new ApplicationException("Unsupported bit width: " + numPlanes);
414+
throw new ImageDecodeException("Unsupported bit width: " + numPlanes);
415415
}
416416
}
417417
else
@@ -555,7 +555,7 @@ public static Image Load(Stream stream, bool resizeForAspect = false)
555555
}
556556
else
557557
{
558-
throw new ApplicationException("unsupported XBMI mode: " + modeXBMI);
558+
throw new ImageDecodeException("unsupported XBMI mode: " + modeXBMI);
559559
}
560560

561561
bmpData[4 * (y * imgWidth + x)] = (byte)prevB;

Diff for: ImageFormats/IffRgbnReader.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public static Image Load(Stream stream)
5757
byte[] tempBytes = new byte[65536];
5858

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

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

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

6868
bool isRgb8 = fileType == "RGB8";
6969

@@ -101,7 +101,7 @@ public static Image Load(Stream stream)
101101

102102
if (imgWidth == -1 || imgHeight == -1)
103103
{
104-
throw new ApplicationException("Invalid format of RGBN file.");
104+
throw new ImageDecodeException("Invalid format of RGBN file.");
105105
}
106106

107107
byte[] bmpData = new byte[(imgWidth + 1) * 4 * imgHeight];
@@ -163,7 +163,7 @@ public static Image Load(Stream stream)
163163
}
164164
else
165165
{
166-
throw new ApplicationException("Invalid compression type.");
166+
throw new ImageDecodeException("Invalid compression type.");
167167
}
168168
}
169169
catch (Exception e)

Diff for: ImageFormats/MacPaintReader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public static Image Load(Stream stream)
4646

4747
if (headerBytes[0] != 0)
4848
{
49-
throw new ApplicationException("This is not a valid MacPaint file.");
49+
throw new ImageDecodeException("This is not a valid MacPaint file.");
5050
}
5151

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

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

7171
// Skip over pattern data

Diff for: ImageFormats/PcxReader.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public static Image Load(Stream stream, bool useCgaPalette = false)
6262

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

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

7171

7272
// This variable controls whether the bit plane values are interpreted as literal color states
@@ -78,11 +78,11 @@ public static Image Load(Stream stream, bool useCgaPalette = false)
7878

7979
tempByte = (byte)stream.ReadByte();
8080
if (tempByte != 1)
81-
throw new ApplicationException("Invalid PCX compression type.");
81+
throw new ImageDecodeException("Invalid PCX compression type.");
8282

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

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

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

9898
Util.LittleEndian(reader.ReadUInt16()); // hdpi
9999
Util.LittleEndian(reader.ReadUInt16()); // vdpi

Diff for: ImageFormats/PnmReader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public static Image Load(Stream inStream, bool bigEndian = true)
6464
int bmpWidth, bmpHeight, bmpMaxVal;
6565

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

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

@@ -86,7 +86,7 @@ public static Image Load(Stream inStream, bool bigEndian = true)
8686

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

9191
int numPixels = bmpWidth * bmpHeight;
9292
int maxElementCount = numPixels * 4;

Diff for: ImageFormats/RasReader.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static Image Load(Stream stream)
6262
BinaryReader reader = new BinaryReader(stream);
6363
UInt32 tempDword = Util.BigEndian(reader.ReadUInt32());
6464
if (tempDword != 0x59a66a95)
65-
throw new ApplicationException("This is not a valid RAS file.");
65+
throw new ImageDecodeException("This is not a valid RAS file.");
6666

6767
int imgWidth = (int)Util.BigEndian(reader.ReadUInt32());
6868
int imgHeight = (int)Util.BigEndian(reader.ReadUInt32());
@@ -75,10 +75,10 @@ public static Image Load(Stream stream)
7575
RleReader rleReader = new RleReader(stream, rasType == RAS_TYPE_RLE);
7676

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

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

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

Diff for: ImageFormats/SgiReader.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ public static Image Load(Stream stream)
5858

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

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

6767
if (compressionType > 1)
68-
throw new ApplicationException("Unsupported compression type.");
68+
throw new ImageDecodeException("Unsupported compression type.");
6969
if (bytesPerComponent != 1)
70-
throw new ApplicationException("Unsupported bytes per component.");
70+
throw new ImageDecodeException("Unsupported bytes per component.");
7171
if (dimension != 1 && dimension != 2 && dimension != 3)
72-
throw new ApplicationException("Unsupported dimension.");
72+
throw new ImageDecodeException("Unsupported dimension.");
7373

7474
int imgWidth = Util.BigEndian(reader.ReadUInt16());
7575
int imgHeight = Util.BigEndian(reader.ReadUInt16());
@@ -78,7 +78,7 @@ public static Image Load(Stream stream)
7878
UInt32 pixMax = Util.BigEndian(reader.ReadUInt32());
7979

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

8383
stream.Seek(4, SeekOrigin.Current);
8484

0 commit comments

Comments
 (0)