//****************************************************************** void PNGImageIFile::setupPalette() throw(ImageException, std::bad_alloc) { png_color * cpal = 0; //the c palette int color_num = 0; //the number of entries in the palette RGBPixel tpixel; //temporary pixel int counter = 0; //counter for loops //get a pointer to the palette or throw if (!png_get_PLTE(png_ptr, info_ptr, &cpal, &color_num)) { throw ImageException(IMAGE_FILE_READ_ERR); } //that went well so create the class palette if (!(palette = new (std::nothrow) RGBPalette(color_num))) throw std::bad_alloc(); //May need to add something here to handle grayscale palette //stuff. CBB for(; counter < color_num; ++counter) { tpixel.setRed(cpal[counter].red); tpixel.setGreen(cpal[counter].green); tpixel.setBlue(cpal[counter].blue); palette->setEntry(counter, tpixel); } }
void cbbViewImage::setData(dtkAbstractData* data) { DTK_D(cbbViewImage); if (!data) return; Images::Image* image = static_cast<Images::Image*>(data->data()); if (!image || image->dimension()!=2) return; using namespace Images; using namespace Images::Pixels; typedef RGB<unsigned char> RGBPixel; typedef Polymorphic::MultiDim<2> Images2D; typedef Images2D::MultiType<float,Images2D::MultiType<double,Images2D::MultiType<unsigned char,Images2D::MultiType<unsigned> > > > Scalar2D; typedef Images2D::MultiType<RGBPixel,Scalar2D> Accepted; typedef RGB<double> RGBDouble; Accepted::Converter<RGBDouble> convert; const Image2D<RGBDouble>& im = convert(image); const IntensityMap<RGBDouble> map(im); QImage qim(im.dimx(),im.dimy(),QImage::Format_RGB888); const Image2D<RGBDouble>::Shape& shape = im.shape(); for (typename Image2D<RGBDouble>::Shape::const_iterator i=shape.begin();i!=shape.end();++i) { RGBPixel pix = map(im(i)); qim.setPixel(i(1),i(2),(pix.red() << 8 | pix.green()) << 8 | pix.blue()); } QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(qim)); d->scene->addItem(item); d->view->show(); }
void operator()(const Mat& mat, char* data) { ImageAccessor<RGBPixel> acc; typename Mat::const_row_iterator row = mat.row_begin(); typename Mat::const_col_iterator col; char* i = data; for (; row != mat.row_end(); ++row) { for (col = row.begin(); col != row.end(); ++col) { RGBPixel tmp = acc.get(col); *(i++) = (unsigned char)tmp.red(); *(i++) = (unsigned char)tmp.green(); *(i++) = (unsigned char)tmp.blue(); } } }
//*********************************************************** 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); } }