Mercurial > hg > dmlib
annotate src/dmimage.c @ 1558:48dcde220a2e
Few fixes in the README.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 13 May 2018 06:04:11 +0300 |
parents | 5e5f75b45f8d |
children | 7a2337dcd1b3 |
rev | line source |
---|---|
0 | 1 /* |
2 * DMLib | |
3 * -- Bitmap image conversion and loading | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
950
88d9440afad0
Use dmzlib module in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
900
diff
changeset
|
5 * (C) Copyright 2012-2015 Tecnic Software productions (TNSP) |
0 | 6 */ |
7 #include "dmimage.h" | |
950
88d9440afad0
Use dmzlib module in stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
900
diff
changeset
|
8 #include "dmzlib.h" |
0 | 9 |
1135
73808dce89e6
Disable formats other than PNG and JPEG from stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
10 // Formats |
73808dce89e6
Disable formats other than PNG and JPEG from stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
11 #define STBI_ONLY_JPEG 1 |
73808dce89e6
Disable formats other than PNG and JPEG from stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
12 #define STBI_ONLY_PNG 1 |
73808dce89e6
Disable formats other than PNG and JPEG from stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
13 //#define STBI_NO_HDR 1 |
73808dce89e6
Disable formats other than PNG and JPEG from stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
14 |
73808dce89e6
Disable formats other than PNG and JPEG from stb_image.
Matti Hamalainen <ccr@tnsp.org>
parents:
1102
diff
changeset
|
15 // Other options, etc. |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
16 #ifndef DM_DEBUG |
900 | 17 #define STBI_NO_FAILURE_STRINGS 1 |
1138
2e728c38a386
Define STBI_ASSERT(x) as dummy if DM_DEBUG is not enabled.
Matti Hamalainen <ccr@tnsp.org>
parents:
1135
diff
changeset
|
18 #define STBI_ASSERT(x) |
1046
7e54b2d08ce7
Add special debug error message function/macro that can be
Matti Hamalainen <ccr@tnsp.org>
parents:
1042
diff
changeset
|
19 #endif |
900 | 20 #define STBI_FAILURE_USERMSG 1 |
21 | |
899
63fd1801f983
Mangle new stb_image to our purposes.
Matti Hamalainen <ccr@tnsp.org>
parents:
870
diff
changeset
|
22 #define STB_IMAGE_IMPLEMENTATION 1 |
0 | 23 #define STBI_NO_STDIO 1 |
899
63fd1801f983
Mangle new stb_image to our purposes.
Matti Hamalainen <ccr@tnsp.org>
parents:
870
diff
changeset
|
24 |
0 | 25 #include "stb_image.c" |
26 | |
27 | |
1154
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
28 static Uint32 |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
30 dmXrmask = 0xff000000, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
31 dmXgmask = 0x00ff0000, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
32 dmXbmask = 0x0000ff00, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
33 dmXamask = 0x000000ff; |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
34 #else |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
35 dmXrmask = 0x000000ff, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
36 dmXgmask = 0x0000ff00, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
37 dmXbmask = 0x00ff0000, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
38 dmXamask = 0xff000000; |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
39 #endif |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
40 |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
41 |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
42 SDL_Surface *dmCreateRGBSurfaceFrom(void *data, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
43 const int width, const int height, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
44 const int depth, const int pitch, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
45 const int rmask, const int gmask, const int bmask, const int amask) |
0 | 46 { |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 // Create source surface from given data |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 SDL_Surface *tmp = SDL_CreateRGBSurfaceFrom(data, width, height, depth, pitch, bmask, gmask, rmask, amask); |
0 | 49 |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
50 // Create result conversion surface |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
51 SDL_Surface |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
52 *result = NULL, |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
53 *pixtmp = SDL_CreateRGBSurface( |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
54 SDL_SWSURFACE, |
1154
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
55 16, 16, 32, |
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
56 dmXrmask, dmXgmask, dmXbmask, dmXamask); |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
57 |
0 | 58 if (tmp != NULL && pixtmp != NULL) |
59 { | |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
60 // Convert surface |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
61 result = SDL_ConvertSurface(tmp, pixtmp->format, SDL_SWSURFACE); |
0 | 62 SDL_FreeSurface(tmp); |
63 } | |
64 if (pixtmp != NULL) | |
65 SDL_FreeSurface(pixtmp); | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1080
diff
changeset
|
66 |
0 | 67 return result; |
68 } | |
69 | |
70 | |
71 SDL_Surface *dmCreatePaletteSurfaceFrom(void *data, const int width, const int height, const int pitch) | |
72 { | |
73 int yc; | |
74 Uint8 *dst, *src; | |
75 SDL_Surface *result; | |
76 result = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0,0,0,0); | |
77 if (result == NULL) | |
78 return NULL; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1080
diff
changeset
|
79 |
0 | 80 dst = result->pixels; |
81 src = data; | |
82 for (yc = 0; yc < height; yc++) | |
83 { | |
84 memcpy(dst, src, width * sizeof(Uint8)); | |
85 dst += result->pitch; | |
86 src += pitch; | |
87 } | |
88 | |
89 return result; | |
90 } | |
91 | |
92 | |
93 static int dmSTBIread(void *user, char *data, int size) | |
94 { | |
95 return dmfread(data, 1, size, (DMResource *) user); | |
96 } | |
97 | |
98 | |
899
63fd1801f983
Mangle new stb_image to our purposes.
Matti Hamalainen <ccr@tnsp.org>
parents:
870
diff
changeset
|
99 static void dmSTBIskip(void *user, int n) |
0 | 100 { |
101 dmfseek((DMResource *) user, n, SEEK_CUR); | |
102 } | |
103 | |
104 | |
105 static int dmSTBIeof(void *user) | |
106 { | |
107 return dmfeof((DMResource *) user); | |
108 } | |
109 | |
110 | |
111 static const stbi_io_callbacks dmSTBICallbacks = | |
112 { | |
113 dmSTBIread, | |
114 dmSTBIskip, | |
115 dmSTBIeof, | |
116 }; | |
117 | |
118 | |
119 SDL_Surface *dmLoadImage(DMResource *file) | |
120 { | |
121 Uint32 rmask, gmask, bmask, amask; | |
122 SDL_Surface *result = NULL; | |
963
1ffc48d0feaf
Initialize variables so that we don't get garbage in error situations.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
123 int width = 0, height = 0, comp = 0; |
0 | 124 Uint8 *data; |
125 | |
1203
2b48b7fe95bc
Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents:
1165
diff
changeset
|
126 dmfreset(file); |
0 | 127 data = stbi_load_from_callbacks(&dmSTBICallbacks, file, &width, &height, &comp, 0); |
128 | |
129 if (data == NULL) | |
130 { | |
1080 | 131 dmErrorDBGMsg( |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
950
diff
changeset
|
132 "Error decoding image resource %p '%s' [%d, %d, %d]: %s\n", |
0 | 133 file, file->filename, width, height, comp, stbi_failure_reason()); |
134 return NULL; | |
135 } | |
136 | |
137 | |
138 switch (comp) | |
139 { | |
140 case 4: | |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
141 result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8, |
1154
494fa5998b65
Move RGBA mask definitions to static global variables that are
Matti Hamalainen <ccr@tnsp.org>
parents:
1138
diff
changeset
|
142 width * comp, dmXrmask, dmXgmask, dmXbmask, dmXamask); |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
143 break; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
144 |
0 | 145 case 3: |
1155
d46966ad3b08
Add note about the 24-bit RGBA masks being different.
Matti Hamalainen <ccr@tnsp.org>
parents:
1154
diff
changeset
|
146 // These masks are different from the dmX*masks |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
147 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
148 rmask = 0x00ff0000; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
149 gmask = 0x0000ff00; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
150 bmask = 0x000000ff; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
151 amask = 0x00000000; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
152 #else |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
153 rmask = 0x000000ff; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
154 gmask = 0x0000ff00; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
155 bmask = 0x00ff0000; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
156 amask = 0x00000000; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
157 #endif |
0 | 158 result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8, |
159 width * comp, rmask, gmask, bmask, amask); | |
160 break; | |
161 | |
162 case 1: | |
163 result = dmCreatePaletteSurfaceFrom(data, width, height, width * comp); | |
164 break; | |
165 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1080
diff
changeset
|
166 |
0 | 167 stbi_image_free(data); |
168 | |
169 if (result == NULL) | |
170 { | |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
171 dmErrorDBGMsg( |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
950
diff
changeset
|
172 "Format conversion failed for image resource %p '%s' [%d, %d, %d].\n", |
0 | 173 file, file->filename, width, height, comp); |
174 } | |
175 | |
176 return result; | |
177 } | |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
178 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
179 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
180 SDL_Texture *dmLoadImageToTexture(DMResource *file, SDL_Renderer *renderer) |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
181 { |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
182 SDL_Surface *tmp; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
183 SDL_Texture *texture; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
184 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
185 // Sanity checks |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
186 if (renderer == NULL || file == NULL) |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
187 return NULL; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
188 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
189 if ((tmp = dmLoadImage(file)) == NULL) |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
190 return NULL; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
191 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
192 // Attempt to create the texture |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
193 texture = SDL_CreateTextureFromSurface(renderer, tmp); |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
194 SDL_FreeSurface(tmp); |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
195 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
196 return texture; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
197 } |