//*********************************************************** void PNGImageIFile::getPixel(long x, long y, AbstractPixel* inpixel) throw(ImageException, std::bad_alloc) { if (Photometric == PHOTO_PALETTE) { unsigned char tpixel; // temporarily pixel unsigned short int tcolor; //temporary color RGBPixel entry; //get the raw pixel getRawPixel(x, y, &tpixel); //get the entry out of the palette. palette->getEntry(tpixel, entry); entry.getRed(tcolor); inpixel->setRed(tcolor); entry.getGreen(tcolor); inpixel->setGreen(tcolor); entry.getBlue(tcolor); inpixel->setBlue(tcolor); return; } else if (Photometric == PHOTO_GRAY) { unsigned char tpixel; getRawPixel(x, y, &tpixel); inpixel->setGrey(tpixel); return; } else if (Photometric == PHOTO_RGB) { unsigned char tpixel[3]; getRawPixel(x, y, tpixel); inpixel->setRed(tpixel[0]); inpixel->setGreen(tpixel[1]); inpixel->setBlue(tpixel[2]); return; } }
//**************************************************************** void PNGImageIFile::getScanline(long row, GreyPixel* inarray) throw(ImageException, std::bad_alloc) { unsigned char * trow = 0; //for 8 bit samples case short unsigned int * trow16 = 0; //for 16 bit samples case short unsigned int color = 0; //temporary color long int size(Width*samplesPerPixel); long int counter = 0; //temporary counter RGBPixel tpixel; //temp pixel try { if (bitsPerSample != 16) { if(!(trow = new (std::nothrow) unsigned char[bytesperline])) throw std::bad_alloc(); getRawScanline(row, trow); } else { if(!(trow16 = new (std::nothrow) short unsigned int [size])) throw std::bad_alloc(); getRawScanline(row, trow16); } switch(Photometric) { case PHOTO_PALETTE: //check the palette if (!palette) throw ImageException(IMAGE_INVALID_OP_ERR); for(counter = 0; counter < Width; ++counter) { palette->getEntry(trow[counter], tpixel); tpixel.getRed(color); inarray[counter].setRed(color); tpixel.getGreen(color); inarray[counter].setGreen(color); tpixel.getBlue(color); inarray[counter].setBlue(color); } break; case PHOTO_RGB: //see if its 8 or 16 bit data if (bitsPerSample == 8) { for (counter = 0; counter < Width; ++counter) { inarray[counter].setRed(trow[counter*samplesPerPixel]); inarray[counter].setGreen(trow[counter*samplesPerPixel+1]); inarray[counter].setBlue(trow[counter*samplesPerPixel+2]); } } else { //16 bit for (counter = 0; counter < Width; ++counter) { inarray[counter].setRed(trow16[counter*samplesPerPixel]); inarray[counter].setGreen(trow16[counter*samplesPerPixel + 1]); inarray[counter].setBlue(trow16[counter*samplesPerPixel + 2]); } } break; case PHOTO_GRAY: //the 8 bit case if (bitsPerSample <= 8) { for(counter = 0; counter < Width; ++counter) { inarray[counter].setGrey(trow[counter]); } } else //16 bits per sample { for (counter = 0; counter < Width; ++counter) { inarray[counter].setGrey(trow16[counter]); } } break; default: throw ImageException(IMAGE_FILE_READ_ERR); } delete [] trow; delete [] trow16; } catch(ImageException & e) { //just rethrow delete [] trow; delete [] trow16; throw e; } catch(...) { if(trow) { delete [] trow; delete [] trow16; } throw ImageException(IMAGE_FILE_READ_ERR); } }