// // WriteImage() // write an image to a file in PNG format // This version writes the entire image // void CImageIOPng::WriteImage(const CImage& image, CNcbiOstream& ostr, CImageIO::ECompress compress) { // make sure we've got an image if ( !image.GetData() ) { NCBI_THROW(CImageException, eWriteError, "CImageIOPng::WriteImage(): " "attempt to write an empty image"); } // validate our image - we need RGB or RGBA images if (image.GetDepth() != 3 && image.GetDepth() != 4) { string msg("CImageIOPng::WriteImage(): invalid image depth: "); msg += NStr::NumericToString(image.GetDepth()); NCBI_THROW(CImageException, eWriteError, msg); } png_structp png_ptr = NULL; png_infop info_ptr = NULL; try { // initialize png stuff s_PngWriteInit(png_ptr, info_ptr, image.GetWidth(), image.GetHeight(), image.GetDepth(), compress); // begin writing data png_set_write_fn(png_ptr, &ostr, s_PngWrite, s_PngFlush); png_write_info(png_ptr, info_ptr); // write our image, line-by-line unsigned char* row_ptr = const_cast<unsigned char*> (image.GetData()); size_t width = image.GetWidth(); size_t height = image.GetHeight(); size_t depth = image.GetDepth(); for (size_t i = 0; i < height; ++i) { png_write_row(png_ptr, row_ptr); row_ptr += width * depth; } // standard clean-up png_write_end(png_ptr, info_ptr); s_PngWriteFinalize(png_ptr, info_ptr); } catch (...) { s_PngWriteFinalize(png_ptr, info_ptr); throw; } }
void CImageIORaw::WriteImage(const CImage& image, CNcbiOstream& ostr, size_t /* x */, size_t y, size_t width, size_t height, CImageIO::ECompress) { // write the header ostr.write(reinterpret_cast<const char*>(sc_Header), 4); // write dimensions size_t depth = image.GetDepth(); ostr.write(reinterpret_cast<const char*>(&width), sizeof(size_t)); ostr.write(reinterpret_cast<const char*>(&height), sizeof(size_t)); ostr.write(reinterpret_cast<const char*>(&depth), sizeof(size_t)); // calculate the bytes per line for our sub-image anf dor the input image const size_t input_bpl = image.GetWidth() * depth; const size_t output_bpl = width * depth; // write the image data const unsigned char* data = image.GetData(); data += input_bpl * y; for (size_t i = 0; i < height; ++i, data += input_bpl) { ostr.write(reinterpret_cast<const char*>(data), output_bpl); } }
void CDevILCodec::CodeToFile(const nstring & filename, const CImage &image) { ILuint imageid; CDevILFormats informat; informat.SetExFormat(image.GetPixelFormat()); // Generate the main image name to use. ilGenImages(1, &imageid); // Bind this image name. ilBindImage(imageid); ilTexImage(image.GetWidth(), image.GetHeight(), image.GetDepth(), informat.GetInternalChannels(), informat.GetFormat(), IL_UNSIGNED_BYTE, image.GetBitsPtr()); ilSaveImage(filename.c_str()); ilDeleteImages(1, &imageid); ILenum Error = 0; if((Error = ilGetError()) != NULL) { nstring str("CDevILCodec::CodeToFile: "); str.append(iluErrorString(Error)); throw NOVA_EXP(str.c_str(), BAD_OPERATION); } }
// // WriteImage() // write an image to a file in PNG format // This version writes only a subpart of the image // void CImageIOPng::WriteImage(const CImage& image, CNcbiOstream& ostr, size_t x, size_t y, size_t w, size_t h, CImageIO::ECompress compress) { // make sure we've got an image if ( !image.GetData() ) { NCBI_THROW(CImageException, eWriteError, "CImageIOPng::WriteImage(): " "attempt to write an empty image"); } // validate our image - we need RGB or RGBA images if (image.GetDepth() != 3 && image.GetDepth() != 4) { string msg("CImageIOPng::WriteImage(): invalid image depth: "); msg += NStr::NumericToString(image.GetDepth()); NCBI_THROW(CImageException, eWriteError, msg); } png_structp png_ptr = NULL; png_infop info_ptr = NULL; try { // initialize png stuff s_PngWriteInit(png_ptr, info_ptr, w, h, image.GetDepth(), compress); // begin writing data png_set_write_fn(png_ptr, &ostr, s_PngWrite, s_PngFlush); png_write_info(png_ptr, info_ptr); // write our image // we plan to march through only part of our image // get a pointer to the start of our scan line // // NB: the const cast is necessary as png_write_row takes a non-const // pointer (go figure...) unsigned char* from_data = const_cast<unsigned char*>(image.GetData()); from_data += (y * image.GetWidth() + x) * image.GetDepth(); size_t from_stride = w * image.GetDepth(); // march out h scan lines for (size_t i = 0; i < h; ++i) { png_write_row(png_ptr, from_data); from_data += from_stride; } // standard clean-up png_write_end(png_ptr, info_ptr); s_PngWriteFinalize(png_ptr, info_ptr); } catch (...) { s_PngWriteFinalize(png_ptr, info_ptr); throw; } }
void CImageIORaw::WriteImage(const CImage& image, CNcbiOstream& ostr, CImageIO::ECompress) { // write the header ostr.write(reinterpret_cast<const char*>(sc_Header), 4); // write dimensions size_t width = image.GetWidth(); size_t height = image.GetHeight(); size_t depth = image.GetDepth(); ostr.write(reinterpret_cast<const char*>(&width), sizeof(size_t)); ostr.write(reinterpret_cast<const char*>(&height), sizeof(size_t)); ostr.write(reinterpret_cast<const char*>(&depth), sizeof(size_t)); // write the image data ostr.write(reinterpret_cast<const char*>(image.GetData()), width * height * depth); }
void CDevILCodec::CodeToBuffer(CMemoryBuffer & out, const CImage &image, ESaveFormats ext) { ILuint imageid; CDevILFormats informat; informat.SetExFormat(image.GetPixelFormat()); // Generate the main image name to use. ilGenImages(1, &imageid); // Bind this image name. ilBindImage(imageid); ilTexImage(image.GetWidth(), image.GetHeight(), image.GetDepth(), informat.GetInternalChannels(), informat.GetFormat(), IL_UNSIGNED_BYTE, image.GetBitsPtr()); ILenum type = 0; switch(ext) { case SF_BMP: type = IL_BMP; break; case SF_ICO: type = IL_ICO; break; case SF_JPG: type = IL_JPG; break; case SF_PCX: type = IL_PCX; break; case SF_PIC: type = IL_PIC; break; case SF_PNG: type = IL_PNG; break; case SF_TGA: type = IL_TGA; break; case SF_TIF: type = IL_TIF; break; case SF_GIF: type = IL_GIF; break; case SF_DDS: type = IL_DDS; break; case SF_PIX: type = IL_PIX; break; case SF_HDR: type = IL_HDR; break; default: return; } out.AllocBuffer(image.GetSize()+0xff); ilSaveL(type, out.GetBegin(), out.GetBufferSize()); ilDeleteImages(1, &imageid); ILenum Error = 0; if((Error = ilGetError()) != NULL) { nstring str("CDevILCodec::CodeToStream: "); str.append(iluErrorString(Error)); throw NOVA_EXP(str.c_str(), BAD_OPERATION); } }