Skip to content
This repository has been archived by the owner on Jul 20, 2020. It is now read-only.

Commit

Permalink
Implemented transformation from surface to array.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKniephoff committed Jan 15, 2011
1 parent fb84390 commit 9af5d12
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
86 changes: 86 additions & 0 deletions array_image.c
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions array_image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SEAMLESS_ARRAY_IMAGE_H
#define SEAMLESS_ARRAY_IMAGE_H

#include <SDL.h>


#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
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 9af5d12

Please sign in to comment.