This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented transformation from surface to array.
- Loading branch information
1 parent
fb84390
commit 9af5d12
Showing
4 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters