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
/
array_image.c
86 lines (74 loc) · 1.57 KB
/
array_image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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]));
pixel += 3;
}
}
if (SDL_MUSTLOCK (surface))
{
SDL_UnlockSurface (surface);
}
return surface;
}