| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- /*
- Copyright (C) 2011 by Ivan Safrin
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- #include "png.h"
- #include <math.h>
- #include "PolyImage.h"
- #include "PolyString.h"
- #include "PolyLogger.h"
- #include "OSBasics.h"
- #include "PolyPerlin.h"
- #include <algorithm>
- #include <stdlib.h>
- #include "rgbe.h"
- using namespace Polycode;
- void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
- OSFILE *file = (OSFILE*)png_get_io_ptr(png_ptr);
- OSBasics::read(data, length, 1, file);
- }
- Image::Image(const String& fileName) : imageData(NULL) {
- setPixelType(IMAGE_RGBA);
- loaded = false;
- if(!loadImage(fileName)) {
- Logger::log("Error loading %s\n", fileName.c_str());
- } else {
- loaded = true;
- // Logger::log("image loaded!");
- }
- }
- Image *Image::BlankImage(int width, int height, int type) {
- return new Image(width, height, type);
- }
- void Image::setPixelType(int type) {
- imageType = type;
- switch(imageType) {
- case IMAGE_RGB:
- pixelSize = 3;
- break;
- case IMAGE_RGBA:
- pixelSize = 4;
- break;
- case IMAGE_FP16:
- pixelSize = 6;
- break;
- default:
- pixelSize = 4;
- break;
- }
- }
- bool Image::isLoaded() const {
- return loaded;
- }
- Image::Image(int width, int height, int type) : imageData(NULL) {
- setPixelType(type);
- createEmpty(width, height);
- }
- Image::Image(Image *copyImage) {
- setPixelType(copyImage->getType());
- width = copyImage->getWidth();
- height = copyImage->getHeight();
- imageData = (char*)malloc(width*height*pixelSize);
- memcpy(imageData, copyImage->getPixels(), width*height*pixelSize);
- }
- Image::Image(char *data, int width, int height, int type) {
- setPixelType(type);
- imageData = (char*)malloc(width*height*pixelSize);
- memcpy(imageData, data, width*height*pixelSize);
- this->width = width;
- this->height = height;
- }
- void Image::pasteImage(Image *image, int x, int y, int blendingMode , Number blendAmount, Color blendColor ) {
- for(int iy=0; iy<image->getHeight(); iy++) {
- for(int ix=0; ix<image->getWidth(); ix++) {
- Color src = image->getPixel(ix,iy);
- Color destColor = getPixel(x+ix, y+iy);
- Color finalColor = destColor.blendColor(src, blendingMode, blendAmount, blendColor);
- setPixel(x+ix, y+iy, finalColor);
- }
- }
- }
- Image::Image() {
- imageData = NULL;
- }
- Image::~Image() {
- free(imageData);
- }
- char *Image::getPixels() {
- return imageData;
- }
- char *Image::getPixelsInRect(int x, int y, int width, int height) {
- transformCoordinates(&x, &y, &width, &height);
- char *retBuf = (char*) malloc(pixelSize * width * height);
- memset(retBuf, 0, pixelSize * width * height);
-
- if(x < this->width-1 && y < this->height-1) {
- width = std::min(width, this->width - x);
- height = std::min(height, this->height - y);
- for(int i=0; i < height; i++) {
- long srcOffset = ((pixelSize*this->width) * (y+i)) + (pixelSize*x);
- long dstOffset = (pixelSize*width) * i;
- memcpy(retBuf + dstOffset, imageData+srcOffset, pixelSize * width);
- }
- }
-
- return retBuf;
- }
- Image *Image::getImagePart(Rectangle subRect) {
- char *newData = getPixelsInRect( (int) subRect.x, (int) subRect.y, (int) subRect.w, (int) subRect.h);
- return new Image(newData, subRect.w, subRect.h, this->imageType);
- }
- Color Image::getPixel(int x, int y) {
- transformCoordinates(&x, &y);
- if(x < 0 || x >= width || y < 0 || y >= height)
- return Color(0,0,0,0);
- unsigned int *imageData32 = (unsigned int*)imageData;
- unsigned int hex = imageData32[x+(y*width)];
- int ta = (hex >> 24) & 0xFF;
- int tb = (hex >> 16) & 0xFF;
- int tg = (hex >> 8) & 0xFF;
- int tr = (hex ) & 0xFF;
-
- return Color(((Number)tr)/255.0f, ((Number)tg)/255.0f, ((Number)tb)/255.0f,((Number)ta)/255.0f);
- }
- int Image::getWidth() const {
- return width;
- }
- int Image::getHeight() const {
- return height;
- }
- void Image::createEmpty(int width, int height) {
- free(imageData);
-
- imageData = (char*)malloc(width*height*pixelSize);
- this->width = width;
- this->height = height;
-
- fill(Color(0,0,0,0));
- }
- void Image::perlinNoise(int seed, bool alpha) {
- Perlin perlin = Perlin(12,33,1,seed);
- unsigned int *imageData32 = (unsigned int*)imageData;
- Color pixelColor;
- Number noiseVal;
-
- for(int i=0; i < width*height;i++) {
- noiseVal = fabs(1.0f/perlin.Get( 0.1+(0.9f/((Number)width)) * (i%width), (1.0f/((Number)height)) * (i - (i%width))));
- if(alpha)
- pixelColor.setColor(noiseVal, noiseVal, noiseVal, noiseVal);
- else
- pixelColor.setColor(noiseVal, noiseVal, noiseVal, 1.0f);
- imageData32[i] = pixelColor.getUint();
- }
- }
- void Image::fillRect(int x, int y, int w, int h, Color col) {
- for(int i=0; i < w; i++) {
- for(int j=0; j < h; j++) {
- setPixel(x+i,y+j,col);
- }
- }
- }
- void Image::setPixel(int x, int y, Color col) {
- transformCoordinates(&x, &y);
- if(x < 0 || x >= width || y < 0 || y >= height)
- return;
- unsigned int *imageData32 = (unsigned int*)imageData;
- imageData32[x+(y*width)] = col.getUint();
- }
- void Image::moveBrush(int x, int y) {
- brushPosX += x;
- brushPosY -= y;
- }
- void Image::moveBrushTo(int x, int y) {
- brushPosX = x;
- brushPosY = y;
- }
- int Image::getBrushX() const {
- return brushPosX;
- }
- int Image::getBrushY() const {
- return brushPosY;
- }
- void Image::drawLineTo(int x, int y, Color col) {
- drawLine(brushPosX, brushPosY, brushPosX+x, brushPosY+y, col);
- }
- void Image::setPixel(int x, int y, Number r, Number g, Number b, Number a) {
- transformCoordinates(&x, &y);
- if(x < 0 || x > width || y < 0 || y > height)
- return;
- Color color;
- color.setColor(r,g,b,a);
- unsigned int *imageData32 = (unsigned int*)imageData;
- imageData32[x+(y*width)] = color.getUint();
- }
- void Image::fastBlurHor(int blurSize) {
- if(blurSize == 0)
- return;
-
- unsigned char *blurImage = (unsigned char*)malloc(width*height*pixelSize);
- int total_r;
- int total_g;
- int total_b;
- int total_a;
-
- unsigned int *imageData32 = (unsigned int*)imageData;
- unsigned char *pixel;
- int amt;
-
- for (int y = 1; y < height; y++) {
- for (int x = 0; x < width; x++) {
- total_r = 0;
- total_g = 0;
- total_b = 0;
- total_a = 0;
- amt = 0;
- for (int kx = -blurSize; kx <= blurSize; kx++) {
- if((x+kx > 0 && x+kx < width) && (x+kx+((y)*width) > 0 && x+kx+((y)*width) < width*height)) {
- pixel = (unsigned char*)&(imageData32[(x+kx)+((y)*width)]);
- total_r += pixel[0];
- total_g += pixel[1];
- total_b += pixel[2];
- total_a += pixel[3];
- amt++;
- }
- }
-
- // Logger::log("%d / %d = %d\n",total_r, amt, (total_r/amt));
-
- blurImage[((x+(y*width))*pixelSize)] = (total_r/amt);
- blurImage[((x+(y*width))*pixelSize)+1] = (total_g / amt);
- blurImage[((x+(y*width))*pixelSize)+2] = (total_b / amt);
- blurImage[((x+(y*width))*pixelSize)+3] = (total_a / amt);
-
- }
- }
- free(imageData);
- imageData = (char*)blurImage;
- // free(imageData32);
- }
- void Image::fastBlurVert(int blurSize) {
- if(blurSize == 0)
- return;
- unsigned char *blurImage = (unsigned char*)malloc(width*height*pixelSize);
- int total_r;
- int total_g;
- int total_b;
- int total_a;
-
- unsigned int *imageData32 = (unsigned int*)imageData;
- unsigned char *pixel;
- int amt;
-
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- total_r = 0;
- total_g = 0;
- total_b = 0;
- total_a = 0;
- amt = 0;
- for (int ky = -blurSize; ky <= blurSize; ky++) {
- if((y+ky > 0 && y+ky < height) && (x+((y+ky)*width) > 0 && x+((y+ky)*width) < width*height)) {
- pixel = (unsigned char*)&(imageData32[(x)+((y+ky)*width)]);
- total_r += pixel[0];
- total_g += pixel[1];
- total_b += pixel[2];
- total_a += pixel[3];
- amt++;
- }
- }
-
- //Logger::log("%d / %d = %d\n",total_r, amt, (total_r/amt));
- blurImage[((x+(y*width))*pixelSize)] = (total_r/amt);
- blurImage[((x+(y*width))*pixelSize)+1] = (total_g / amt);
- blurImage[((x+(y*width))*pixelSize)+2] = (total_b / amt);
- blurImage[((x+(y*width))*pixelSize)+3] = (total_a / amt);
-
- }
- }
- free(imageData);
- imageData = (char*)blurImage;
- // free(imageData32);
- }
- void Image::fastBlur(int blurSize) {
- fastBlurHor(blurSize);
- fastBlurVert(blurSize);
- }
- void Image::swap(int *v1, int *v2) {
- int tv = *v1;
- *v1 = *v2;
- *v2 = tv;
- }
- void Image::drawLine(int x0, int y0, int x1, int y1, Color col) {
- bool steep = abs(y1 - y0) > abs(x1 - x0);
- if(steep) {
- swap(&x0, &y0);
- swap(&x1, &y1);
- }
- if(x0 > x1) {
- swap(&x0, &x1);
- swap(&y0, &y1);
- }
-
- int deltax = x1 - x0;
- int deltay = abs(y1 - y0);
- Number error = 0;
- Number deltaerr = ((Number)deltay) / ((Number)deltax);
- int ystep;
- int y = y0;
- if(y0 < y1)
- ystep = 1;
- else
- ystep = -1;
-
- for(int x=x0; x < x1;x++) {
- if(steep) {
- setPixel(y,x,col);
- } else {
- setPixel(x,y,col);
- }
- error = error + ((Number)deltaerr);
- if(error >= 0.5) {
- y = y + ystep;
- error = error - 1.0;
- }
- }
- }
- void Image::fill(Color color) {
- unsigned int val = color.getUint();
- unsigned int *imageData32 = (unsigned int*) imageData;
- for(int i=0; i< width*height; i++) {
- imageData32[i] = val;
- }
- }
- bool Image::saveImage(const String &fileName) {
- return savePNG(fileName);
- }
- void Image::premultiplyAlpha() {
- unsigned int *imageData32 = (unsigned int*)imageData;
-
- for(int x=0; x < width; x++) {
- for(int y=0; y < height; y++) {
-
- unsigned int hex = imageData32[x+(y*width)];
-
- int ta = (hex >> 24) & 0xFF;
- int tb = (hex >> 16) & 0xFF;
- int tg = (hex >> 8) & 0xFF;
- int tr = (hex ) & 0xFF;
-
- Number r = ((Number)tr)/255.0f;
- Number g = ((Number)tg)/255.0f;
- Number b = ((Number)tb)/255.0f;
- Number a = ((Number)ta)/255.0f;
- r *= a;
- g *= a;
- b *= a;
-
- unsigned int ir = 255.0f*r;
- unsigned int ig = 255.0f*g;
- unsigned int ib = 255.0f*b;
- unsigned int ia = 255.0f*a;
-
- unsigned int newVal = ((ia & 0xFF) << 24) | ((ib & 0xFF) << 16) | ((ig & 0xFF) << 8) | (ir & 0xFF);
-
- imageData32[x+(y*width)] = newVal;
- }
- }
- }
- bool Image::savePNG(const String &fileName) {
- printf("Saving %dx%d image\n", width, height);
- FILE *fp;
- png_structp png_ptr;
- png_infop info_ptr;
-
- fp = fopen(fileName.c_str(), "wb");
- if (fp == NULL)
- return false;
- png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
- if (png_ptr == NULL)
- {
- fclose(fp);
- return false;
- }
- info_ptr = png_create_info_struct(png_ptr);
- if (info_ptr == NULL)
- {
- fclose(fp);
- png_destroy_write_struct(&png_ptr, NULL);
- return false;
- }
- if (setjmp(png_jmpbuf(png_ptr)))
- {
- /* If we get here, we had a problem writing the file */
- fclose(fp);
- png_destroy_write_struct(&png_ptr, &info_ptr);
- return false;
- }
- png_init_io(png_ptr, fp);
- png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
- PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-
- png_write_info(png_ptr, info_ptr);
- png_uint_32 k;
- png_bytep *row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
- if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
- png_error (png_ptr, "Image is too tall to process in memory");
- for (k = 0; k < height; k++)
- row_pointers[height-k-1] = (png_byte*)(imageData + k*width*4);
- /* One of the following output methods is REQUIRED */
- png_write_image(png_ptr, row_pointers);
- free(row_pointers);
- png_free(png_ptr, 0);
- png_destroy_write_struct(&png_ptr, &info_ptr);
- fclose(fp);
- return true;
- }
- bool Image::loadImage(const String& fileName) {
-
- String extension;
- size_t found;
- found=fileName.rfind(".");
- if (found != -1) {
- extension = fileName.substr(found+1);
- } else {
- extension = "";
- }
- if(extension == "png") {
- return loadPNG(fileName);
- } else if(extension == "hdr") {
- return loadHDR(fileName);
- } else {
- Logger::log("Error: Invalid image format.\n");
- return false;
- }
- }
- inline hfloat Image::convertFloatToHFloat(float f) {
- float _f = f;
- uint32_t x = *(uint32_t *)(&_f);
- uint32_t sign = (uint32_t)(x >> 31);
- uint32_t mantissa;
- uint32_t exp;
- hfloat hf;
-
- // get mantissa
- mantissa = x & ((1 << 23) - 1);
- // get exponent bits
- exp = x & FLOAT_MAX_BIASED_EXP;
- if (exp >= HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP)
- {
- // check if the original single precision float number is a NaN
- if (mantissa && (exp == FLOAT_MAX_BIASED_EXP))
- {
- // we have a single precision NaN
- mantissa = (1 << 23) - 1;
- }
- else
- {
- // 16-bit half-float representation stores number as Inf
- mantissa = 0;
- }
- hf = (((hfloat)sign) << 15) | (hfloat)(HALF_FLOAT_MAX_BIASED_EXP) |
- (hfloat)(mantissa >> 13);
- }
- // check if exponent is <= -15
- else if (exp <= HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP)
- {
-
- // store a denorm half-float value or zero
- exp = (HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP - exp) >> 23;
- mantissa >>= (14 + exp);
-
- hf = (((hfloat)sign) << 15) | (hfloat)(mantissa);
- }
- else
- {
- hf = (((hfloat)sign) << 15) |
- (hfloat)((exp - HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP) >> 13) |
- (hfloat)(mantissa >> 13);
- }
-
- return hf;
- }
- TokenArray Image::readTokens(char *line, const char *tokenString) {
- char **tokens = (char**)malloc(sizeof(void*));
- char *pch;
- int numTokens = 0;
- pch = strtok (line, tokenString);
- while (pch != NULL) {
- numTokens++;
- tokens = (char**)realloc(tokens, sizeof(void*) *numTokens);
- tokens[numTokens-1] = (char*) malloc(strlen(pch)+1);
- memcpy(tokens[numTokens-1], pch, strlen(pch)+1);
- pch = strtok (NULL, tokenString);
- }
-
- TokenArray ta;
- ta.size = numTokens;
- ta.tokens = tokens;
- return ta;
- }
- void Image::freeTokens(TokenArray tokens) {
- int i;
- for(i =0; i < tokens.size; i++) {
- free(tokens.tokens[i]);
- }
- free(tokens.tokens);
- }
- bool Image::loadHDR(const String &fileName) {
-
- printf("Loading HDR image\n");
-
- setPixelType(IMAGE_FP16);
- FILE *file = fopen (fileName.c_str(), "rb");
- if(!file) {
- return false;
- }
- if(RGBE_ReadHeader(file, &width, &height, NULL) != RGBE_RETURN_SUCCESS) {
-
- // if ReadHeader failed, try reading info manually.
-
- char line [ 128 ];
- Number exposure;
-
- fseek(file, 0, SEEK_SET);
-
- while ( fgets ( line, sizeof(line), file ) != NULL ) {
- TokenArray ta = readTokens(line, "=");
- if(strcmp(ta.tokens[0], "EXPOSURE") == 0) {
- exposure = atof(ta.tokens[1]);
- }
- if(strcmp(ta.tokens[0], "FORMAT") == 0) {
- //printf("format is %s\n", ta.tokens[1]);
- }
- freeTokens(ta);
-
- ta = readTokens(line, " ");
- if(strcmp(ta.tokens[0], "-Y") == 0) {
- //printf("image size is %d x %d\n", atoi(ta.tokens[1]), atoi(ta.tokens[3]));
- width = atoi(ta.tokens[1]);
- height = atoi(ta.tokens[3]);
- freeTokens(ta);
- break;
- }
- freeTokens(ta);
- }
- }
-
- float *data = (float *)malloc(sizeof(float)*3*width*height);
-
- printf("Reading fp pixels (%d x %d)\n", width, height);
-
- RGBE_ReadPixels_RLE(file, data, width, height);
- float *hFloatImageData = (float*) malloc(sizeof(float)*3*width*height);
- /*
- for(int i=0; i < width*height; i++) {
- hFloatImageData[i*3] = convertFloatToHFloat(data[i*3]);
- hFloatImageData[(i*3)+1] = convertFloatToHFloat(data[(i*3)+1]);
- hFloatImageData[(i*3)+2] = convertFloatToHFloat(data[(i*3)+2]);
- }
- */
- imageData = (char*) data; //(char*)hFloatImageData;
-
- // free(data);
- fclose(file);
- return true;
- }
- bool Image::loadPNG(const String& fileName) {
- OSFILE *infile;
-
- png_structp png_ptr;
- png_infop info_ptr;
- char *image_data;
- char sig[8];
- int bit_depth;
- int color_type;
- png_uint_32 width;
- png_uint_32 height;
- unsigned int rowbytes;
- image_data = NULL;
- int i;
- png_bytepp row_pointers = NULL;
-
- infile = OSBasics::open(fileName.c_str(), "rb");
- if (!infile) {
- Logger::log("Error opening png file (\"%s\")\n", fileName.c_str());
- return false;
- }
-
- OSBasics::read(sig, 1, 8, infile);
-
- if (!png_check_sig((unsigned char *) sig, 8)) {
- Logger::log("Error reading png signature (\"%s\")\n", fileName.c_str());
- OSBasics::close(infile);
- return false;
- }
- png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
- if (!png_ptr) {
- Logger::log("Error creating png struct (\"%s\")\n", fileName.c_str());
- OSBasics::close(infile);
- return false; /* out of memory */
- }
-
- info_ptr = png_create_info_struct(png_ptr);
- if (!info_ptr) {
- Logger::log("Error creating info struct (\"%s\")\n", fileName.c_str());
- png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
- OSBasics::close(infile);
- return false; /* out of memory */
- }
-
- if (setjmp(png_jmpbuf(png_ptr))) {
- Logger::log("Error setting jump thingie (\"%s\")\n", fileName.c_str());
- png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
- OSBasics::close(infile);
- return false;
- }
- //png_init_io(png_ptr, infile);
- png_set_read_fn(png_ptr, (png_voidp)infile, user_read_data);
-
- png_set_sig_bytes(png_ptr, 8);
- png_read_info(png_ptr, info_ptr);
- png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
- &color_type, NULL, NULL, NULL);
- this->width = width;
- this->height = height;
-
- /* Set up some transforms. */
- if (color_type & PNG_COLOR_MASK_ALPHA) {
- // png_set_strip_alpha(png_ptr);
- }
- if (bit_depth > 8) {
- png_set_strip_16(png_ptr);
- }
- if (color_type == PNG_COLOR_TYPE_GRAY ||
- color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
- png_set_gray_to_rgb(png_ptr);
- }
- if (color_type == PNG_COLOR_TYPE_PALETTE) {
- png_set_palette_to_rgb(png_ptr);
- }
-
-
- if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
- png_set_add_alpha(png_ptr, 0xffffff, PNG_FILLER_AFTER);
- }
-
- /* Update the png info struct.*/
- png_read_update_info(png_ptr, info_ptr);
-
- /* Rowsize in bytes. */
- rowbytes = png_get_rowbytes(png_ptr, info_ptr);
-
- /* Allocate the image_data buffer. */
- if ((image_data = (char *) malloc(rowbytes * height))==NULL) {
- Logger::log("Error allocating image memory (\"%s\")\n", fileName.c_str());
- png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
- return false;
- }
-
- if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
- Logger::log("Error allocating image memory (\"%s\")\n", fileName.c_str());
- png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
- free(image_data);
- image_data = NULL;
- return false;
- }
-
- for (i = 0; i < height; ++i)
- row_pointers[height - 1 - i] = (png_byte*)image_data + i*rowbytes;
-
- png_read_image(png_ptr, row_pointers);
-
- free(row_pointers);
- png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
- OSBasics::close(infile);
-
- imageData = image_data;
- return true;
- }
- void Image::transformCoordinates(int *x, int *y) {
- *y = this->height - *y - 1;
- }
- void Image::transformCoordinates(int *x, int *y, int *w, int *h) {
- *y = this->height - *h - *y - 1;
- }
|