diff options
author | Stijn Buys <ingar@osirion.org> | 2008-03-26 23:24:26 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-03-26 23:24:26 +0000 |
commit | 3d993058e5078fbdfd92d479281ad93bb40a4bc6 (patch) | |
tree | fd578d5a44246a90fc08cdf6b9a18f02313ce431 /src/render/image.cc | |
parent | 2b9f068e7ee4c0d249c715f9eb5a3c2c8a11e6f8 (diff) |
improved TGA handling
Diffstat (limited to 'src/render/image.cc')
-rw-r--r-- | src/render/image.cc | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/render/image.cc b/src/render/image.cc new file mode 100644 index 0000000..7ce8113 --- /dev/null +++ b/src/render/image.cc @@ -0,0 +1,64 @@ +/* + render/image.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include <stdlib.h> +#include <string.h> + +#include "render/image.h" + +namespace render +{ + +Image::Image(unsigned int width, unsigned int height, unsigned int channels) +{ + image_width = width; + image_height = height; + image_channels = channels; + + image_data = (unsigned char *) malloc(size()); + clear(); +} + +Image::~Image() +{ + free(image_data); +} + +void Image::clear() +{ + memset(image_data, 0, size()); +} + +void Image::swap_channels() +{ + for (size_t y =0; y < image_height; y++) { + for (size_t x = 0; x < image_width; x++) { + size_t offset = y * image_width * image_channels + x * image_channels; + unsigned char tmp = image_data[offset]; + image_data[offset] = image_data[offset + 2]; + image_data[offset + 2] = tmp; + } + } +} + +void Image::flip() +{ + unsigned char line[image_width*image_channels]; + for (size_t y=0; y < image_height /2; y++) { + memcpy(line, + &image_data[y*image_width*image_channels], + image_width*image_channels); + memcpy(&image_data[y*image_width*image_channels], + &image_data[(image_height-1-y)*image_width*image_channels], + image_width*image_channels); + memcpy(&image_data[(image_height-1-y)*image_width*image_channels], + line, + image_width*image_channels); + } +} + +} + |