goat3dgfx

diff src/image.cc @ 6:3d96734fd477

cubemap loading and cubemap example program
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 17 Nov 2013 08:20:13 +0200
parents 1873dfd13f2d
children 7d6b667821cf
line diff
     1.1 --- a/src/image.cc	Sun Nov 17 03:22:40 2013 +0200
     1.2 +++ b/src/image.cc	Sun Nov 17 08:20:13 2013 +0200
     1.3 @@ -1,4 +1,11 @@
     1.4  #include <string.h>
     1.5 +
     1.6 +#ifndef _MSC_VER
     1.7 +#include <alloca.h>
     1.8 +#else
     1.9 +#include <malloc.h>
    1.10 +#endif
    1.11 +
    1.12  #include "imago2.h"
    1.13  #include "image.h"
    1.14  #include "logger.h"
    1.15 @@ -50,12 +57,39 @@
    1.16  	return true;
    1.17  }
    1.18  
    1.19 -bool Image::set_pixels(int x, int y, void *pixels, Format fmt)
    1.20 +bool Image::set_pixels(int xsz, int ysz, void *pixels, Format fmt)
    1.21  {
    1.22 -	if(!create(x, y, fmt)) {
    1.23 +	if(!create(xsz, ysz, fmt)) {
    1.24  		return false;
    1.25  	}
    1.26 -	memcpy(this->pixels, pixels, x * y * pixel_size(fmt));
    1.27 +	memcpy(this->pixels, pixels, xsz * ysz * pixel_size(fmt));
    1.28 +	return true;
    1.29 +}
    1.30 +
    1.31 +bool Image::set_pixels(int xsz, int ysz, void *pixels, int scan_width, Format fmt)
    1.32 +{
    1.33 +	return set_pixels(xsz, ysz, pixels, 0, 0, scan_width, fmt);
    1.34 +}
    1.35 +
    1.36 +bool Image::set_pixels(int xsz, int ysz, void *pixels, int x, int y, int scan_width, Format fmt)
    1.37 +{
    1.38 +	if(scan_width <= 0) {
    1.39 +		scan_width = xsz;
    1.40 +	}
    1.41 +
    1.42 +	if(!create(xsz, ysz, fmt)) {
    1.43 +		return false;
    1.44 +	}
    1.45 +
    1.46 +	int pixsz = pixel_size(fmt);
    1.47 +
    1.48 +	unsigned char *dest = (unsigned char*)this->pixels;
    1.49 +	unsigned char *src = (unsigned char*)pixels + (y * scan_width + x) * pixsz;
    1.50 +	for(int i=0; i<ysz; i++) {
    1.51 +		memcpy(dest, src, xsz * pixsz);
    1.52 +		dest += xsz * pixsz;
    1.53 +		src += scan_width * pixsz;
    1.54 +	}
    1.55  	return true;
    1.56  }
    1.57  
    1.58 @@ -64,6 +98,53 @@
    1.59  	return pixels;
    1.60  }
    1.61  
    1.62 +void Image::flip_horizontal()
    1.63 +{
    1.64 +	int pixsz = pixel_size(fmt);
    1.65 +
    1.66 +	unsigned char *tmppix = (unsigned char*)alloca(pixsz);
    1.67 +
    1.68 +	unsigned char *scan = (unsigned char*)pixels;
    1.69 +	for(int i=0; i<height; i++) {
    1.70 +		unsigned char *dest = scan;
    1.71 +		unsigned char *src = scan + (width - 1) * pixsz;
    1.72 +
    1.73 +		while(src > dest) {
    1.74 +			memcpy(tmppix, src, pixsz);
    1.75 +			memcpy(src, dest, pixsz);
    1.76 +			memcpy(dest, tmppix, pixsz);
    1.77 +			dest += pixsz;
    1.78 +			src -= pixsz;
    1.79 +		}
    1.80 +
    1.81 +		scan += width * pixsz;
    1.82 +	}
    1.83 +}
    1.84 +
    1.85 +void Image::flip_vertical()
    1.86 +{
    1.87 +	int pixsz = pixel_size(fmt);
    1.88 +
    1.89 +	unsigned char *tmpscan = (unsigned char*)alloca(width * pixsz);
    1.90 +
    1.91 +	unsigned char *dest = (unsigned char*)pixels;
    1.92 +	unsigned char *src = (unsigned char*)pixels + (height - 1) * width * pixsz;
    1.93 +
    1.94 +	while(src > dest) {
    1.95 +		memcpy(tmpscan, src, width * pixsz);
    1.96 +		memcpy(src, dest, width * pixsz);
    1.97 +		memcpy(dest, tmpscan, width * pixsz);
    1.98 +		dest += width * pixsz;
    1.99 +		src -= width * pixsz;
   1.100 +	}
   1.101 +}
   1.102 +
   1.103 +void Image::rotate_180()
   1.104 +{
   1.105 +	flip_vertical();
   1.106 +	flip_horizontal();
   1.107 +}
   1.108 +
   1.109  bool Image::load(const char *fname)
   1.110  {
   1.111  	struct img_pixmap pixmap;