From: Mikko Rasa Date: Thu, 10 Oct 2013 17:36:14 +0000 (+0300) Subject: Allow images to have padding between rows X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7f7f3c7494fe10a91215eaff208465636e9979ab;p=libs%2Fgui.git Allow images to have padding between rows --- diff --git a/source/graphics/devil/devilloader.cpp b/source/graphics/devil/devilloader.cpp index cb6a1b6..131b6ba 100644 --- a/source/graphics/devil/devilloader.cpp +++ b/source/graphics/devil/devilloader.cpp @@ -126,7 +126,8 @@ void DevilLoader::load(Image::Data &data) data.width = ilGetInteger(IL_IMAGE_WIDTH); data.height = ilGetInteger(IL_IMAGE_HEIGHT); - unsigned data_size = data.width*data.height*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); + data.stride = data.width*ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); + unsigned data_size = data.stride*data.height; data.data = new char[data_size]; ILubyte *il_data = ilGetData(); copy(il_data, il_data+data_size, data.data); diff --git a/source/graphics/image.h b/source/graphics/image.h index 6905461..29fd288 100644 --- a/source/graphics/image.h +++ b/source/graphics/image.h @@ -19,6 +19,7 @@ public: PixelFormat fmt; unsigned width; unsigned height; + unsigned stride; char *data; Data(); @@ -36,6 +37,7 @@ public: PixelFormat get_format() const { return data.fmt; } unsigned get_width() const { return data.width; } unsigned get_height() const { return data.height; } + unsigned get_stride() const { return data.stride; } const void *get_data() const { return data.data; } }; diff --git a/source/graphics/png/pngloader.cpp b/source/graphics/png/pngloader.cpp index 15684b1..50d58b3 100644 --- a/source/graphics/png/pngloader.cpp +++ b/source/graphics/png/pngloader.cpp @@ -66,10 +66,15 @@ void PngLoader::load(Image::Data &data) int depth; int color; png_get_IHDR(priv->png, priv->info, &width, &height, &depth, &color, 0, 0, 0); - data.width = width; - data.height = height; + unsigned nchans = png_get_channels(priv->png, priv->info); + if(depth!=8) throw unsupported_image_format("depth!=8"); + + data.width = width; + data.height = height; + data.stride = data.width*nchans; + switch(color) { case PNG_COLOR_TYPE_PALETTE: data.fmt = COLOR_INDEX; break; @@ -80,14 +85,12 @@ void PngLoader::load(Image::Data &data) default: throw unsupported_image_format("unknown color type"); } - unsigned nchans = png_get_channels(priv->png, priv->info); if(nchans==4 && data.fmt==RGB) png_set_strip_alpha(priv->png); - unsigned rowstride = data.width*nchans; - data.data = new char[rowstride*data.height]; + data.data = new char[data.stride*data.height]; for(unsigned y=0; ypng, reinterpret_cast(data.data+rowstride*(data.height-1-y)), 0); + png_read_row(priv->png, reinterpret_cast(data.data+data.stride*(data.height-1-y)), 0); png_read_end(priv->png, 0); } diff --git a/source/graphics/quartz/quartzloader.cpp b/source/graphics/quartz/quartzloader.cpp index 23f7abb..a7c610d 100644 --- a/source/graphics/quartz/quartzloader.cpp +++ b/source/graphics/quartz/quartzloader.cpp @@ -93,6 +93,7 @@ void QuartzLoader::load(Image::Data &data) { data.width = CGImageGetWidth(image); data.height = CGImageGetHeight(image); + data.stride = CGImageGetBytesPerRow(image); CGColorSpaceRef color = CGImageGetColorSpace(image); CGColorSpaceModel model = CGColorSpaceGetModel(color); @@ -109,11 +110,9 @@ void QuartzLoader::load(Image::Data &data) else throw unsupported_image_format("unknown colorspace"); - unsigned bytes_per_pixel = (CGImageGetBitsPerPixel(image)+7)/8; - CGDataProviderRef dp = CGImageGetDataProvider(image); CFDataRef image_data = CGDataProviderCopyData(dp); - data.data = new char[data.width*data.height*bytes_per_pixel]; + data.data = new char[data.height*data.stride]; CFDataGetBytes(image_data, CFRangeMake(0, CFDataGetLength(image_data)), reinterpret_cast(data.data)); CFRelease(image_data);