diff --git a/Makefile b/Makefile index b5ca8bd..cdee351 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,2 @@ seamless: main.c energy_functions.c - gcc -Wall -Werror -pedantic-errors -std=c99 `sdl-config --cflags --libs` -lSDL_image energy_functions.c pixel_access.c main.c -o seamless -O3 + gcc -Wall -Werror -pedantic-errors -std=c99 `sdl-config --cflags --libs` -lSDL_image energy_functions.c pixel_access.c array_image.c main.c -o seamless -O3 diff --git a/array_image.c b/array_image.c new file mode 100644 index 0000000..11ab10c --- /dev/null +++ b/array_image.c @@ -0,0 +1,86 @@ +#include "array_image.h" + +#include "pixel_access.h" + + +Uint8 * +surface_to_array (SDL_Surface * surface) +{ + if (!surface) + { + return NULL; + } + + if (SDL_MUSTLOCK (surface)) + { + SDL_LockSurface (surface); + } + + Uint8 *array = malloc (sizeof (Uint8) * surface->w * surface->h * 3); + if (!array) + { + return NULL; + } + /* TODO Maybe optimize this. It could be done a little more low level with one loop. + * The question is then: How to generalize it. + */ + Uint8 *pixel = array; + for (int y = 0; y < surface->h; ++y) + { + for (int x = 0; x < surface->w; ++x) + { + SDL_GetRGB (get_pixel (surface, x, y), surface->format, pixel, + pixel + 1, pixel + 2); + pixel += 3; + } + } + + if (SDL_MUSTLOCK (surface)) + { + SDL_UnlockSurface (surface); + } + + return array; +} + + +SDL_Surface * +array_to_surface (Uint8 * array, Uint32 flags, int width, int height, + int bitsPerPixel, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, + Uint32 Amask) +{ + if (!array) + { + return NULL; + } + + SDL_Surface *surface = + SDL_CreateRGBSurface (flags, width, height, bitsPerPixel, Rmask, Gmask, + Bmask, Amask); + if (!surface) + { + return NULL; + } + if (SDL_MUSTLOCK (surface)) + { + SDL_LockSurface (surface); + } + + Uint8 *pixel = array; + for (int y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + put_pixel (surface, x, y, + SDL_MapRGB (surface->format, pixel[0], pixel[1], + pixel[2])); + array += 3; + } + } + + if (SDL_MUSTLOCK (surface)) + { + SDL_UnlockSurface (surface); + } + return surface; +} diff --git a/array_image.h b/array_image.h new file mode 100644 index 0000000..148373c --- /dev/null +++ b/array_image.h @@ -0,0 +1,19 @@ +#ifndef SEAMLESS_ARRAY_IMAGE_H +#define SEAMLESS_ARRAY_IMAGE_H + +#include + + +#define array_p(array, w, x, y) ((array) + 3 * ((w) * (y) + (x))) +#define array_r(array, w, x, y) ((array)[3 * ((w) * (y) + (x)) + 0]) +#define array_g(array, w, x, y) ((array)[3 * ((w) * (y) + (x)) + 1]) +#define array_b(array, w, x, y) ((array)[3 * ((w) * (y) + (x)) + 2]) + +Uint8 *surface_to_array (SDL_Surface * surface); + +SDL_Surface *array_to_surface (Uint8 * array, Uint32 flags, int width, + int height, int bitsPerPixel, Uint32 Rmask, + Uint32 Gmask, Uint32 Bmask, Uint32 Amask); + + +#endif diff --git a/main.c b/main.c index f54c3f3..f2ee7a8 100644 --- a/main.c +++ b/main.c @@ -6,10 +6,12 @@ #include "util.h" #include "pixel_access.h" +#include "array_image.h" extern Uint8 gradient_magnitude (SDL_Surface * image, int x, int y); extern Uint8 steepest_neighbor (SDL_Surface * image, int x, int y); +/* TODO There should be more cleanup here... */ void quit (void) {