///////////////////////////////////////////////////////// // really open the file ! (OS dependent) // ///////////////////////////////////////////////////////// bool imageMAGICK :: load(std::string filename, imageStruct&result, gem::Properties&props) { Magick::Image image; try { ::verbose(2, "reading '%s' with ImageMagick", filename.c_str()); // Read a file into image object try { image.read( filename ); } catch (Magick::Warning e) { verbose(1, "magick loading problem: %s", e.what()); } result.xsize=static_cast<GLint>(image.columns()); result.ysize=static_cast<GLint>(image.rows()); result.setCsizeByFormat(GL_RGBA); result.reallocate(); result.upsidedown=true; try { image.write(0,0,result.xsize,result.ysize, "RGBA", Magick::CharPixel, reinterpret_cast<void*>(result.data)); } catch (Magick::Warning e) { verbose(1, "magick decoding problem: %s", e.what()); } }catch( Magick::Exception e ) { verbose(1, "magick loading image failed with: %s", e.what()); return false; } return true; }
///////////////////////////////////////////////////////// // processImage // ///////////////////////////////////////////////////////// void pix_frei0r :: processRGBAImage(imageStruct &image) { static double time=0; if(!m_plugin)return; m_image.xsize=image.xsize; m_image.ysize=image.ysize; m_image.reallocate(); m_plugin->process(time, image, m_image); time++; image.data = m_image.data; image.notowned = true; image.setCsizeByFormat(m_image.format); }
///////////////////////////////////////////////////////// // CreateBuffer // ///////////////////////////////////////////////////////// bool refresh_buffer(const imageStruct&reference, imageStruct&buffer) { // only 1 channel !!, to keep data-size handy unsigned char*data = buffer.data; size_t dataSize = reference.xsize * reference.xsize * reference.ysize * reference.csize * sizeof(unsigned char); bool refresh= (reference.xsize != buffer.xsize) || (reference.ysize != buffer.ysize) || (reference.csize != buffer.csize); buffer.xsize = reference.xsize; buffer.ysize = reference.ysize; buffer.setCsizeByFormat(reference.format); if(data!=buffer.reallocate( dataSize ) || refresh) { buffer.setBlack(); } return (0!=buffer.data); }
static bool QuickTimeImage2mem(GraphicsImportComponent inImporter, imageStruct&result) { Rect r; if (::GraphicsImportGetNaturalBounds(inImporter, &r)) { return NULL; //get an image size } ::OffsetRect(&r, -r.left, -r.top); if (::GraphicsImportSetBoundsRect(inImporter, &r)) { return NULL; } ImageDescriptionHandle imageDescH = NULL; if (::GraphicsImportGetImageDescription(inImporter, &imageDescH)) { return NULL; } result.xsize = (*imageDescH)->width; result.ysize = (*imageDescH)->height; result.upsidedown = true; OSType pixelformat = 0; /* afaik, there is no 8bit grayscale format.... * and even if it was, k8GrayPixelFormat would not be a define... */ #ifdef k8GrayPixelFormat /* from the docs on "depth": what depth is this data (1-32) or ( 33-40 grayscale ) */ if ((*imageDescH)->depth <= 32) { result.setCsizeByFormat(GL_RGBA_GEM); pixelformat = IMAGEQT_RGBA_PIXELFORMAT; } else { result.setCsizeByFormat(GL_LUMINANCE); pixelformat = k8GrayPixelFormat; } #else result.setCsizeByFormat(GL_RGBA_GEM); pixelformat = IMAGEQT_RGBA_PIXELFORMAT; #endif ::DisposeHandle(reinterpret_cast<Handle>(imageDescH)); imageDescH = NULL; result.reallocate(); verbose(1, "[GEM:imageQT] QuickTimeImage2mem() allocate %d bytes", result.xsize*result.ysize*result.csize); if (result.data == NULL) { verbose(0, "[GEM:imageQT] Can't allocate memory for an image."); return false; } GWorldPtr gw = NULL; OSErr err = QTNewGWorldFromPtr(&gw, /* taken from pix_filmDarwin */ pixelformat, // gives noErr &r, NULL, NULL, 0, // keepLocal, //useDistantHdwrMem, result.data, static_cast<long>(result.xsize * result.csize)); if (err) { verbose(0, "[GEM:imageQT] Can't create QTNewWorld"); } ::GraphicsImportSetGWorld(inImporter, gw, NULL); ::GraphicsImportDraw(inImporter); ::DisposeGWorld(gw); //dispose the offscreen gw = NULL; return true; }
///////////////////////////////////////////////////////// // really open the file ! (OS dependent) // ///////////////////////////////////////////////////////// bool imageJPEG :: load(std::string filename, imageStruct&result, gem::Properties&props) { // open up the file FILE * infile; ::verbose(2, "reading '%s' with libJPEG", filename.c_str()); if ((infile = fopen(filename.c_str(), "rb")) == NULL) { //verbose(2, "GemImageLoad(JPEG): Unable to open image file: %s", filename.c_str()); return(false); } // create the jpeg structures jpeg_decompress_struct cinfo; my_error_mgr jerr; // We set up the normal JPEG error routines, then override error_exit cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; // Establish the setjmp return context for my_error_exit to use. if ( setjmp(jerr.setjmp_buffer) ) { // If we get here, the JPEG code has signaled an error. // We need to clean up the JPEG object, close the input file, and return. jpeg_destroy_decompress(&cinfo); fclose(infile); return(false); } // create the decompression structure jpeg_create_decompress(&cinfo); // associate the decompress struct with the file jpeg_stdio_src(&cinfo, infile); // read in the file info jpeg_read_header(&cinfo, TRUE); // do we have an RGB image? if (cinfo.jpeg_color_space == JCS_RGB) { result.setCsizeByFormat(GL_RGBA); } else if (cinfo.jpeg_color_space == JCS_GRAYSCALE) { // do we have a gray8 image? result.setCsizeByFormat(GL_LUMINANCE); } else { // something else, so decompress as RGB result.setCsizeByFormat(GL_RGBA); cinfo.out_color_space = JCS_RGB; } // start the decompression jpeg_start_decompress(&cinfo); int xSize = cinfo.output_width; int ySize = cinfo.output_height; int cSize = result.csize; result.upsidedown=true; result.xsize = xSize; result.ysize = ySize; result.reallocate(); // cycle through the scan lines unsigned char *srcLine = new unsigned char[xSize * cSize]; unsigned char *dstLine = result.data; int yStride = xSize * cSize; int lines = ySize; int pixes = xSize; // do RGBA/RGB data if (cSize == 4) { while (lines--) { unsigned char *src = srcLine; unsigned char *dst = dstLine; jpeg_read_scanlines(&cinfo, &src, 1); pixes = xSize; while (pixes--) { dst[chRed] = src[0]; dst[chGreen] = src[1]; dst[chBlue] = src[2]; dst[chAlpha] = 255; dst += 4; src += 3; } dstLine += yStride; } } else { // do grayscale data while (lines--) { unsigned char *src = srcLine; unsigned char *dst = dstLine; jpeg_read_scanlines(&cinfo, &src, 1); pixes = xSize; while (pixes--) { *dst++ = *src++; } dstLine += yStride; } } // finish the decompression jpeg_finish_decompress(&cinfo); // cleanup jpeg_destroy_decompress(&cinfo); fclose(infile); delete [] srcLine; return true; }
///////////////////////////////////////////////////////// // really open the file ! (OS dependent) // ///////////////////////////////////////////////////////// bool imageTIFF :: load(std::string filename, imageStruct&result, gem::Properties&props) { ::logpost(NULL, 6, "reading '%s' with libTIFF", filename.c_str()); TIFF *tif = TIFFOpen(filename.c_str(), "r"); if (tif == NULL) { return(NULL); } uint32 width, height; short bits, samps; TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bits); TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samps); int npixels = width * height; result.xsize=width; result.ysize=height; result.upsidedown=true; result.type=GL_UNSIGNED_BYTE; //? bool knownFormat = false; // Is it a gray8 image? if (bits == 8 && samps == 1) { result.setCsizeByFormat(GL_LUMINANCE); knownFormat = true; } // Is it an RGB image? else if (bits == 8 && samps == 3) { result.setCsizeByFormat(GL_RGBA); knownFormat = true; } // Is it an RGBA image? else if (bits == 8 && samps == 4) { result.setCsizeByFormat(GL_RGBA); knownFormat = true; } // can we handle the raw data? if (knownFormat) { unsigned char *buf = new unsigned char [TIFFScanlineSize(tif)]; if (buf == NULL) { error("GemImageLoad(TIFF): can't allocate memory for scanline buffer: %s", filename.c_str()); TIFFClose(tif); return(false); } result.reallocate(); unsigned char *dstLine = result.data; int yStride = result.xsize * result.csize; for (uint32 row = 0; row < height; row++) { unsigned char *pixels = dstLine; if (TIFFReadScanline(tif, buf, row, 0) < 0) { error("GemImageLoad(TIFF): bad image data read on line: %d: %s", row, filename.c_str()); TIFFClose(tif); return false; } unsigned char *inp = buf; if (samps == 1) { for (uint32 i = 0; i < width; i++) { *pixels++ = *inp++; // Gray8 } } else if (samps == 3) { for (uint32 i = 0; i < width; i++) { pixels[chRed] = inp[0]; // Red pixels[chGreen] = inp[1]; // Green pixels[chBlue] = inp[2]; // Blue pixels[chAlpha] = 255; // Alpha pixels += 4; inp += 3; } } else { for (uint32 i = 0; i < width; i++) { pixels[chRed] = inp[0]; // Red pixels[chGreen] = inp[1]; // Green pixels[chBlue] = inp[2]; // Blue pixels[chAlpha] = inp[3]; // Alpha pixels += 4; inp += 4; } } dstLine += yStride; } delete [] buf; } // nope, so use the automatic conversion else { char emsg[1024]; TIFFRGBAImage img; if (TIFFRGBAImageBegin(&img, tif, 0, emsg) == 0) { //error("GemImageLoad(TIFF): Error reading in image file: %s : %s", filename, emsg); TIFFClose(tif); return(false); } uint32*raster = reinterpret_cast<uint32*>(_TIFFmalloc(npixels * sizeof(uint32))); if (raster == NULL) { error("GemImageLoad(TIFF): Unable to allocate memory for image: %s", filename.c_str()); TIFFClose(tif); return(false); } if (TIFFRGBAImageGet(&img, raster, width, height) == 0) { //error("GemImageLoad(TIFF): Error getting image data in file: %s, %s", filename, emsg); _TIFFfree(raster); TIFFClose(tif); return(false); } TIFFRGBAImageEnd(&img); result.setCsizeByFormat(GL_RGBA); result.reallocate(); unsigned char *dstLine = result.data; int yStride = result.xsize * result.csize; // transfer everything over int k = 0; for (uint32 i = 0; i < height; i++) { unsigned char *pixels = dstLine; for (uint32 j = 0; j < width; j++) { pixels[chRed] = static_cast<unsigned char>(TIFFGetR(raster[k])); // Red pixels[chGreen] = static_cast<unsigned char>(TIFFGetG(raster[k])); // Green pixels[chBlue] = static_cast<unsigned char>(TIFFGetB(raster[k])); // Blue pixels[chAlpha] = static_cast<unsigned char>(TIFFGetA(raster[k])); // Alpha k++; pixels += 4; } dstLine += yStride; } _TIFFfree(raster); } TIFFClose(tif); return true; }