diff --git a/pyext/GFitsImageByte.i b/pyext/GFitsImageByte.i index 052be0e..d61f946 100644 --- a/pyext/GFitsImageByte.i +++ b/pyext/GFitsImageByte.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageByte { unsigned char __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], unsigned char value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageByte copy() { return (*self); diff --git a/pyext/GFitsImageDouble.i b/pyext/GFitsImageDouble.i index ed8f142..e01bccc 100644 --- a/pyext/GFitsImageDouble.i +++ b/pyext/GFitsImageDouble.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageDouble { double __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], double value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageDouble copy() { return (*self); diff --git a/pyext/GFitsImageFloat.i b/pyext/GFitsImageFloat.i index 64fe9f9..df932d0 100644 --- a/pyext/GFitsImageFloat.i +++ b/pyext/GFitsImageFloat.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageFloat { float __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], float value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageFloat copy() { return (*self); diff --git a/pyext/GFitsImageLong.i b/pyext/GFitsImageLong.i index 3fdc134..334da8b 100644 --- a/pyext/GFitsImageLong.i +++ b/pyext/GFitsImageLong.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageLong { long __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], long value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageLong copy() { return (*self); diff --git a/pyext/GFitsImageLongLong.i b/pyext/GFitsImageLongLong.i index 88e1016..79ae6e8 100644 --- a/pyext/GFitsImageLongLong.i +++ b/pyext/GFitsImageLongLong.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageLongLong { long long __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], long long value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageLongLong copy() { return (*self); diff --git a/pyext/GFitsImageSByte.i b/pyext/GFitsImageSByte.i index 4157f14..d43004f 100644 --- a/pyext/GFitsImageSByte.i +++ b/pyext/GFitsImageSByte.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageSByte { char __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], char value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageSByte copy() { return (*self); diff --git a/pyext/GFitsImageShort.i b/pyext/GFitsImageShort.i index 89f19c5..169d9cc 100644 --- a/pyext/GFitsImageShort.i +++ b/pyext/GFitsImageShort.i @@ -66,29 +66,55 @@ public: %extend GFitsImageShort { short __getitem__(int GFitsImageInx[]) { if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], short value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageShort copy() { return (*self); diff --git a/pyext/GFitsImageULong.i b/pyext/GFitsImageULong.i index ae384f1..6d55cc5 100644 --- a/pyext/GFitsImageULong.i +++ b/pyext/GFitsImageULong.i @@ -65,30 +65,55 @@ public: ***************************************************************************/ %extend GFitsImageULong { unsigned long __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], unsigned long value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageULong copy() { return (*self); diff --git a/pyext/GFitsImageUShort.i b/pyext/GFitsImageUShort.i index 85c1abf..36f0027 100644 --- a/pyext/GFitsImageUShort.i +++ b/pyext/GFitsImageUShort.i @@ -66,30 +66,55 @@ public: ***************************************************************************/ %extend GFitsImageUShort { unsigned short __getitem__(int GFitsImageInx[]) { - if (GFitsImageInx[0] == 1) - return self->at(GFitsImageInx[1]); - else if (GFitsImageInx[0] == 2) - return self->at(GFitsImageInx[1], GFitsImageInx[2]); - else if (GFitsImageInx[0] == 3) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); - else if (GFitsImageInx[0] == 4) - return self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]); - else + // Check image dimensions + for (int i = 0; i < GFitsImageInx[0]; ++i) { + if (GFitsImageInx[i+1] < 0 || GFitsImageInx[i+1] >= self->naxes(i)) { + throw GException::out_of_range("__getitem__(int)", + "FITS image axis "+ + gammalib::str(i)+" index", + GFitsImageInx[i+1], + self->naxes(i)); + } + } + // Return pixel value + if (GFitsImageInx[0] == 1) { + return (*self)(GFitsImageInx[1]); + } + else if (GFitsImageInx[0] == 2) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2]); + } + else if (GFitsImageInx[0] == 3) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]); + } + else if (GFitsImageInx[0] == 4) { + return (*self)(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]); + } + else { throw GException::fits_wrong_image_operator("__getitem__(int)", self->naxis(), GFitsImageInx[0]); + } } void __setitem__(int GFitsImageInx[], unsigned short value) { - if (GFitsImageInx[0] == 1) + if (GFitsImageInx[0] == 1) { self->at(GFitsImageInx[1]) = value; - else if (GFitsImageInx[0] == 2) + } + else if (GFitsImageInx[0] == 2) { self->at(GFitsImageInx[1], GFitsImageInx[2]) = value; - else if (GFitsImageInx[0] == 3) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = value; - else if (GFitsImageInx[0] == 4) - self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], GFitsImageInx[4]) = value; - else + } + else if (GFitsImageInx[0] == 3) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3]) = + value; + } + else if (GFitsImageInx[0] == 4) { + self->at(GFitsImageInx[1], GFitsImageInx[2], GFitsImageInx[3], + GFitsImageInx[4]) = value; + } + else { throw GException::fits_wrong_image_operator("__setitem__(int)", - self->naxis(), GFitsImageInx[0]); + self->naxis(), + GFitsImageInx[0]); + } } GFitsImageUShort copy() { return (*self);