void Image::setPixel( int x, int y, Color c ){ assert( width > 0 && height > 0); ensureBuffer(); if(x>=0&&y>=0&&x<width&&y<height){ int index = ( x + y * width ) * 4; buffer[index] = (unsigned char) (c.r*255); buffer[index+1] = (unsigned char) (c.g*255); buffer[index+2] = (unsigned char) (c.b*255); buffer[index+3] = (unsigned char) (c.a*255); } modified_pixel_num += 1; }
bool Image::loadPNG( const char *path, bool multiply_color_by_alpha ) { const char *cpath = platformCStringPath(path); FILE *fp = fopen(cpath,"rb"); if(!fp) { print( "Image::loadPNG: can't open file:%s", cpath ); return false; } unsigned error; unsigned char* image_data; unsigned w, h; error = lodepng_decode32_file(&image_data, &w, &h, cpath ); if(error) { fprintf(stderr, "decoder error %u: %s\n", error, lodepng_error_text(error) ); FREE(image_data); fclose(fp); return false; } width = w; height = h; if( multiply_color_by_alpha ) multiplyAlphaRGBA(image_data,width,height); ensureBuffer(); #define IMAGE_BUFFER_COPY \ for(int i=0;i<width*height;i++){ \ int x = i % width; \ int y = (i / width); \ int ii = y * width + x; \ buffer[ii*4+0] = image_data[i*4+0]; /*r*/ \ buffer[ii*4+1] = image_data[i*4+1]; /*g*/ \ buffer[ii*4+2] = image_data[i*4+2]; /*b*/ \ buffer[ii*4+3] = image_data[i*4+3]; /*a*/ \ } IMAGE_BUFFER_COPY; // clean up FREE(image_data); fclose(fp); strncpy( last_load_file_path, path, sizeof(last_load_file_path) ); return true; }
bool Image::loadPNGMem( unsigned char *ptr, size_t sz, bool multiply_color_by_alpha ) { unsigned error; unsigned char* image_data = (unsigned char*) MALLOC( 2048 * 2048 * 4 ); unsigned w, h; error = lodepng_decode32( &image_data, &w, &h, ptr,sz ); if(error) { fprintf(stderr, "decoder error %u: %s\n", error, lodepng_error_text(error) ); FREE(image_data); return false; } width = w; height = h; if( multiply_color_by_alpha ) multiplyAlphaRGBA(image_data,width,height); ensureBuffer(); IMAGE_BUFFER_COPY; FREE(image_data); return true; }
void writeDouble(double value) { ensureBuffer(8); memcpy(&data[offset], &value, sizeof(double)); offset += 8; }
void writeFloat(float value) { ensureBuffer(4); memcpy(&data[offset], &value, sizeof(float)); offset += 4; }
void writeInt(unsigned int value) { ensureBuffer(4); memcpy(&data[offset], &value, sizeof(unsigned int)); offset += 4; }
void writeBytes(const char* value, UTsize size) { ensureBuffer(size); memcpy(data + sizeof(unsigned char)*offset, value, size); offset += size; }
void writeByte(unsigned char value) { ensureBuffer(1); data[offset] = value; offset++; }
void Image::setSize(int w, int h ) { width = w; height = h; ensureBuffer(); }