Mercurial > hg > dmlib
annotate src/dmimage.c @ 2572:92b93a12c014
Make some casts explicit instead of implicit, fixes some -pedantic warnings.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 10 Mar 2022 22:36:48 +0200 |
parents | 0e355702435d |
children |
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 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
|
29 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
|
30 const int depth, const int pitch, |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
31 const Uint32 format) |
0 | 32 { |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
33 // Create source surface from given data |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
34 SDL_Surface |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
35 *result = NULL, |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
36 *tmp = SDL_CreateRGBSurfaceWithFormatFrom(data, width, height, depth, pitch, format); |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
37 |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
38 // Create result conversion surface |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
39 if (tmp != NULL) |
0 | 40 { |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
41 result = SDL_ConvertSurfaceFormat(tmp, SDL_PIXELFORMAT_RGBA32, 0); |
0 | 42 SDL_FreeSurface(tmp); |
43 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1080
diff
changeset
|
44 |
0 | 45 return result; |
46 } | |
47 | |
48 | |
49 SDL_Surface *dmCreatePaletteSurfaceFrom(void *data, const int width, const int height, const int pitch) | |
50 { | |
51 Uint8 *dst, *src; | |
52 SDL_Surface *result; | |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
53 result = SDL_CreateRGBSurfaceWithFormat(0, width, height, 8, SDL_PIXELFORMAT_INDEX8); |
0 | 54 if (result == NULL) |
55 return NULL; | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1080
diff
changeset
|
56 |
0 | 57 dst = result->pixels; |
58 src = data; | |
2334 | 59 for (int yc = 0; yc < height; yc++) |
0 | 60 { |
61 memcpy(dst, src, width * sizeof(Uint8)); | |
62 dst += result->pitch; | |
63 src += pitch; | |
64 } | |
65 | |
66 return result; | |
67 } | |
68 | |
69 | |
70 static int dmSTBIread(void *user, char *data, int size) | |
71 { | |
72 return dmfread(data, 1, size, (DMResource *) user); | |
73 } | |
74 | |
75 | |
899
63fd1801f983
Mangle new stb_image to our purposes.
Matti Hamalainen <ccr@tnsp.org>
parents:
870
diff
changeset
|
76 static void dmSTBIskip(void *user, int n) |
0 | 77 { |
78 dmfseek((DMResource *) user, n, SEEK_CUR); | |
79 } | |
80 | |
81 | |
82 static int dmSTBIeof(void *user) | |
83 { | |
84 return dmfeof((DMResource *) user); | |
85 } | |
86 | |
87 | |
88 static const stbi_io_callbacks dmSTBICallbacks = | |
89 { | |
90 dmSTBIread, | |
91 dmSTBIskip, | |
92 dmSTBIeof, | |
93 }; | |
94 | |
95 | |
96 SDL_Surface *dmLoadImage(DMResource *file) | |
97 { | |
98 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
|
99 int width = 0, height = 0, comp = 0; |
0 | 100 Uint8 *data; |
101 | |
1203
2b48b7fe95bc
Use dmfreset() instead of dmfseek() to seek to stream start.
Matti Hamalainen <ccr@tnsp.org>
parents:
1165
diff
changeset
|
102 dmfreset(file); |
0 | 103 data = stbi_load_from_callbacks(&dmSTBICallbacks, file, &width, &height, &comp, 0); |
104 | |
105 if (data == NULL) | |
106 { | |
1080 | 107 dmErrorDBGMsg( |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
950
diff
changeset
|
108 "Error decoding image resource %p '%s' [%d, %d, %d]: %s\n", |
2572
92b93a12c014
Make some casts explicit instead of implicit, fixes some -pedantic warnings.
Matti Hamalainen <ccr@tnsp.org>
parents:
2334
diff
changeset
|
109 (void *) file, file->filename, width, height, comp, stbi_failure_reason()); |
0 | 110 return NULL; |
111 } | |
112 | |
113 | |
114 switch (comp) | |
115 { | |
116 case 4: | |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
117 result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8, width * comp, |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
118 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
119 SDL_PIXELFORMAT_ABGR8888 |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
120 #else |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
121 SDL_PIXELFORMAT_ARGB8888 |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
122 #endif |
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
123 ); |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
124 break; |
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
125 |
0 | 126 case 3: |
1155
d46966ad3b08
Add note about the 24-bit RGBA masks being different.
Matti Hamalainen <ccr@tnsp.org>
parents:
1154
diff
changeset
|
127 // These masks are different from the dmX*masks |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
128 result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8, width * comp, |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
129 #if SDL_BYTEORDER == SDL_BIG_ENDIAN |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
130 SDL_PIXELFORMAT_BGR888 |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
131 #else |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
132 SDL_PIXELFORMAT_RGB888 |
757
dd59a650a318
Fix 24-bit surface conversions in image loader.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
133 #endif |
1560
7a2337dcd1b3
Cleanup some remnants of SDL1 code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1557
diff
changeset
|
134 ); |
0 | 135 break; |
136 | |
137 case 1: | |
138 result = dmCreatePaletteSurfaceFrom(data, width, height, width * comp); | |
139 break; | |
140 } | |
1102
e06abfde6c39
Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
1080
diff
changeset
|
141 |
0 | 142 stbi_image_free(data); |
143 | |
144 if (result == NULL) | |
145 { | |
1048
509e6ed3a24e
Finishing touches to the DM_DEBUG stuff.
Matti Hamalainen <ccr@tnsp.org>
parents:
1046
diff
changeset
|
146 dmErrorDBGMsg( |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
950
diff
changeset
|
147 "Format conversion failed for image resource %p '%s' [%d, %d, %d].\n", |
2572
92b93a12c014
Make some casts explicit instead of implicit, fixes some -pedantic warnings.
Matti Hamalainen <ccr@tnsp.org>
parents:
2334
diff
changeset
|
148 (void *) file, file->filename, width, height, comp); |
0 | 149 } |
150 | |
151 return result; | |
152 } | |
1557
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
153 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
154 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
155 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
|
156 { |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
157 SDL_Surface *tmp; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
158 SDL_Texture *texture; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
159 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
160 // Sanity checks |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
161 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
|
162 return NULL; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
163 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
164 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
|
165 return NULL; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
166 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
167 // 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
|
168 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
|
169 SDL_FreeSurface(tmp); |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
170 |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
171 return texture; |
5e5f75b45f8d
Initial port to SDL2. Many things will not work now.
Matti Hamalainen <ccr@tnsp.org>
parents:
1203
diff
changeset
|
172 } |