0
|
1 /*
|
|
2 * DMLib
|
|
3 * -- Bitmap image conversion and loading
|
|
4 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
5 * (C) Copyright 2012 Tecnic Software productions (TNSP)
|
|
6 */
|
|
7 #include "dmimage.h"
|
|
8
|
|
9
|
|
10 #define STBI_FAILURE_USERMSG 1
|
|
11 #define STBI_NO_STDIO 1
|
|
12 #define STBI_NO_HDR 1
|
|
13 #include "stb_image.c"
|
|
14
|
|
15
|
|
16 SDL_Surface *dmCreateRGBSurfaceFrom(void *data, const int width, const int height, const int depth, const int pitch, const int rmask, const int gmask, const int bmask, const int amask)
|
|
17 {
|
|
18 SDL_Surface *tmp, *pixtmp, *result = NULL;
|
|
19
|
|
20 tmp = SDL_CreateRGBSurfaceFrom(data, width, height, depth, pitch, bmask, gmask, rmask, amask);
|
|
21
|
|
22 pixtmp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 10, 10, 32, rmask, gmask, bmask, amask);
|
|
23 if (tmp != NULL && pixtmp != NULL)
|
|
24 {
|
|
25 result = SDL_ConvertSurface(tmp, pixtmp->format, SDL_SWSURFACE | SDL_SRCALPHA);
|
|
26 SDL_FreeSurface(tmp);
|
|
27 }
|
|
28 if (pixtmp != NULL)
|
|
29 SDL_FreeSurface(pixtmp);
|
|
30
|
|
31 return result;
|
|
32 }
|
|
33
|
|
34
|
|
35 SDL_Surface *dmCreatePaletteSurfaceFrom(void *data, const int width, const int height, const int pitch)
|
|
36 {
|
|
37 int yc;
|
|
38 Uint8 *dst, *src;
|
|
39 SDL_Surface *result;
|
|
40 result = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0,0,0,0);
|
|
41 if (result == NULL)
|
|
42 return NULL;
|
|
43
|
|
44 dst = result->pixels;
|
|
45 src = data;
|
|
46 for (yc = 0; yc < height; yc++)
|
|
47 {
|
|
48 memcpy(dst, src, width * sizeof(Uint8));
|
|
49 dst += result->pitch;
|
|
50 src += pitch;
|
|
51 }
|
|
52
|
|
53 return result;
|
|
54 }
|
|
55
|
|
56
|
|
57
|
|
58 static int dmSTBIread(void *user, char *data, int size)
|
|
59 {
|
|
60 return dmfread(data, 1, size, (DMResource *) user);
|
|
61 }
|
|
62
|
|
63
|
|
64 static void dmSTBIskip(void *user, unsigned int n)
|
|
65 {
|
|
66 dmfseek((DMResource *) user, n, SEEK_CUR);
|
|
67 }
|
|
68
|
|
69
|
|
70 static int dmSTBIeof(void *user)
|
|
71 {
|
|
72 return dmfeof((DMResource *) user);
|
|
73 }
|
|
74
|
|
75
|
|
76 static const stbi_io_callbacks dmSTBICallbacks =
|
|
77 {
|
|
78 dmSTBIread,
|
|
79 dmSTBIskip,
|
|
80 dmSTBIeof,
|
|
81 };
|
|
82
|
|
83
|
|
84 SDL_Surface *dmLoadImage(DMResource *file)
|
|
85 {
|
|
86 Uint32 rmask, gmask, bmask, amask;
|
|
87 SDL_Surface *result = NULL;
|
|
88 int width, height, comp;
|
|
89 Uint8 *data;
|
|
90
|
|
91 data = stbi_load_from_callbacks(&dmSTBICallbacks, file, &width, &height, &comp, 0);
|
|
92
|
|
93 if (data == NULL)
|
|
94 {
|
|
95 dmError("Error decoding image resource %p '%s' [%d, %d, %d]: %s\n",
|
|
96 file, file->filename, width, height, comp, stbi_failure_reason());
|
|
97 return NULL;
|
|
98 }
|
|
99
|
|
100 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
|
|
101 rmask = 0xff000000;
|
|
102 gmask = 0x00ff0000;
|
|
103 bmask = 0x0000ff00;
|
|
104 amask = 0x000000ff;
|
|
105 #else
|
|
106 rmask = 0x000000ff;
|
|
107 gmask = 0x0000ff00;
|
|
108 bmask = 0x00ff0000;
|
|
109 amask = 0xff000000;
|
|
110 #endif
|
|
111
|
|
112 switch (comp)
|
|
113 {
|
|
114 case 4:
|
|
115 case 3:
|
|
116 result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8,
|
|
117 width * comp, rmask, gmask, bmask, amask);
|
|
118 break;
|
|
119
|
|
120 case 1:
|
|
121 result = dmCreatePaletteSurfaceFrom(data, width, height, width * comp);
|
|
122 break;
|
|
123 }
|
|
124
|
|
125 stbi_image_free(data);
|
|
126
|
|
127 if (result == NULL)
|
|
128 {
|
|
129 dmError("Format conversion failed for image resource %p '%s' [%d, %d, %d].\n",
|
|
130 file, file->filename, width, height, comp);
|
|
131 }
|
|
132
|
|
133 return result;
|
|
134 }
|