1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
4 | |
5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. |
8 | |
9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: |
12 | |
13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ |
21 | #include "SDL_internal.h" |
22 | |
23 | #include "SDL_stb_c.h" |
24 | |
25 | |
26 | // We currently only support JPEG, but we could add other image formats if we wanted |
27 | #ifdef SDL_HAVE_STB |
28 | #define malloc SDL_malloc |
29 | #define realloc SDL_realloc |
30 | #define free SDL_free |
31 | #undef memcpy |
32 | #define memcpy SDL_memcpy |
33 | #undef memset |
34 | #define memset SDL_memset |
35 | #undef strcmp |
36 | #define strcmp SDL_strcmp |
37 | #undef strncmp |
38 | #define strncmp SDL_strncmp |
39 | #define strtol SDL_strtol |
40 | |
41 | #define abs SDL_abs |
42 | #define pow SDL_pow |
43 | #define ldexp SDL_scalbn |
44 | |
45 | #define STB_IMAGE_STATIC |
46 | #define STBI_NO_THREAD_LOCALS |
47 | #define STBI_FAILURE_USERMSG |
48 | #if defined(SDL_NEON_INTRINSICS) |
49 | #define STBI_NEON |
50 | #endif |
51 | #define STBI_ONLY_JPEG |
52 | #define STBI_NO_GIF |
53 | #define STBI_NO_PNG |
54 | #define STBI_NO_HDR |
55 | #define STBI_NO_LINEAR |
56 | #define STBI_NO_ZLIB |
57 | #define STBI_NO_STDIO |
58 | #define STBI_ASSERT SDL_assert |
59 | #define STB_IMAGE_IMPLEMENTATION |
60 | #include "stb_image.h" |
61 | |
62 | #undef memset |
63 | #endif |
64 | |
65 | #ifdef SDL_HAVE_STB |
66 | static bool SDL_ConvertPixels_MJPG_to_NV12(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch) |
67 | { |
68 | int w = 0, h = 0, format = 0; |
69 | stbi__context s; |
70 | stbi__start_mem(&s, src, src_pitch); |
71 | |
72 | stbi__result_info ri; |
73 | SDL_zero(ri); |
74 | ri.bits_per_channel = 8; |
75 | ri.channel_order = STBI_ORDER_RGB; |
76 | ri.num_channels = 0; |
77 | |
78 | stbi__nv12 nv12; |
79 | nv12.w = width; |
80 | nv12.h = height; |
81 | nv12.pitch = dst_pitch; |
82 | nv12.y = (stbi_uc *)dst; |
83 | nv12.uv = nv12.y + (nv12.h * nv12.pitch); |
84 | |
85 | void *pixels = stbi__jpeg_load(&s, &w, &h, &format, 4, &nv12, &ri); |
86 | if (!pixels) { |
87 | return false; |
88 | } |
89 | return true; |
90 | } |
91 | #endif // SDL_HAVE_STB |
92 | |
93 | bool SDL_ConvertPixels_STB(int width, int height, |
94 | SDL_PixelFormat src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch, |
95 | SDL_PixelFormat dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch) |
96 | { |
97 | #ifdef SDL_HAVE_STB |
98 | if (src_format == SDL_PIXELFORMAT_MJPG && dst_format == SDL_PIXELFORMAT_NV12) { |
99 | return SDL_ConvertPixels_MJPG_to_NV12(width, height, src, src_pitch, dst, dst_pitch); |
100 | } |
101 | |
102 | bool result; |
103 | int w = 0, h = 0, format = 0; |
104 | int len = (src_format == SDL_PIXELFORMAT_MJPG) ? src_pitch : (height * src_pitch); |
105 | void *pixels = stbi_load_from_memory(src, len, &w, &h, &format, 4); |
106 | if (!pixels) { |
107 | return false; |
108 | } |
109 | |
110 | if (w == width && h == height) { |
111 | result = SDL_ConvertPixelsAndColorspace(w, h, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, pixels, width * 4, dst_format, dst_colorspace, dst_properties, dst, dst_pitch); |
112 | } else { |
113 | result = SDL_SetError("Expected image size %dx%d, actual size %dx%d" , width, height, w, h); |
114 | } |
115 | stbi_image_free(pixels); |
116 | |
117 | return result; |
118 | #else |
119 | return SDL_SetError("SDL not built with STB image support" ); |
120 | #endif |
121 | } |
122 | |