1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #define SG_BUILD_LIBRARY |
16 | #include <siege/graphics/bitmap.h> |
17 | |
18 | #include <stdlib.h> |
19 | #include <stdio.h> |
20 | |
21 | #include "../internal/stb/stb_image.h" |
22 | |
23 | static int f_read(void* data, char* ptr, int size) |
24 | { |
25 | SGStream* stream = data; |
26 | return stream->read(stream->data, ptr, 1, size); |
27 | } |
28 | static void f_skip(void* data, unsigned n) |
29 | { |
30 | SGStream* stream = data; |
31 | stream->seek(stream->data, n, SG_SEEK_CUR1); |
32 | } |
33 | static int f_eof(void* data) |
34 | { |
35 | SGStream* stream = data; |
36 | return stream->eof(stream->data); |
37 | } |
38 | |
39 | static stbi_io_callbacks imgCallbacks; |
40 | |
41 | SGbool SG_CALL__cdecl _sgBitmapInit(void) |
42 | { |
43 | imgCallbacks.read = f_read; |
44 | imgCallbacks.skip = f_skip; |
45 | imgCallbacks.eof = f_eof; |
46 | return SG_TRUE1; |
47 | } |
48 | SGbool SG_CALL__cdecl _sgBitmapDeinit(void) |
49 | { |
50 | return SG_TRUE1; |
51 | } |
52 | |
53 | SGBitmap* SG_CALL__cdecl sgBitmapCreateStream(SGStream* stream, SGbool delstream) |
54 | { |
55 | SGBitmap* bmp = NULL((void*)0); |
56 | if(!stream) goto err; |
| |
57 | bmp = malloc(sizeof(SGBitmap)); |
| 5 | | Uninitialized value stored to field 'deldata' | |
|
58 | if(!bmp) goto err; |
| 6 | | Assuming 'bmp' is non-null | |
|
| |
59 | |
60 | |
61 | |
62 | int w, h, n; |
63 | bmp->data = stbi_load_from_callbacks(&imgCallbacks, stream, &w, &h, &n, 4); |
64 | if(!bmp->data) |
| |
65 | goto err; |
| 9 | | Control jumps to line 72 | |
|
66 | bmp->deldata = SG_TRUE1; |
67 | bmp->width = w; |
68 | bmp->height = h; |
69 | bmp->bpp = 32; |
70 | return bmp; |
71 | err: |
72 | fprintf(stderr(&_iob[2]), "Could not load image\n"); |
73 | if(bmp) |
| |
74 | { |
75 | if(bmp->deldata && bmp->data) |
| 11 | | Branch condition evaluates to a garbage value |
|
76 | free(bmp->data); |
77 | free(bmp); |
78 | } |
79 | return NULL((void*)0); |
80 | } |
81 | SGBitmap* SG_CALL__cdecl sgBitmapCreateFile(const char* fname) |
82 | { |
83 | SGStream* stream = sgStreamCreateFile(fname, "r"); |
84 | if(!stream) |
| 1 | Assuming 'stream' is non-null | |
|
| |
85 | fprintf(stderr(&_iob[2]), "Could not load image %s\n", fname); |
86 | return sgBitmapCreateStream(stream, SG_TRUE1); |
| 3 | | Calling 'sgBitmapCreateStream' | |
|
87 | } |
88 | SGBitmap* SG_CALL__cdecl sgBitmapCreateData(size_t width, size_t height, SGenum bpp, void* data) |
89 | { |
90 | SGBitmap* bmp = malloc(sizeof(SGBitmap)); |
91 | bmp->width = width; |
92 | bmp->height = height; |
93 | bmp->bpp = bpp; |
94 | bmp->data = data; |
95 | bmp->deldata = SG_FALSE0; |
96 | |
97 | return bmp; |
98 | } |
99 | SGBitmap* SG_CALL__cdecl sgBitmapCreate(size_t width, size_t height, SGenum bpp) |
100 | { |
101 | return sgBitmapCreateData(width, height, bpp, NULL((void*)0)); |
102 | } |
103 | void SG_CALL__cdecl sgBitmapDestroy(SGBitmap* bmp) |
104 | { |
105 | if(!bmp) return; |
106 | |
107 | if(bmp->deldata) |
108 | free(bmp->data); |
109 | free(bmp); |
110 | } |
111 | |
112 | |
113 | |
114 | |
115 | void SG_CALL__cdecl sgBitmapGetData(SGBitmap* bmp, size_t* width, size_t* height, SGenum* bpp, void** data) |
116 | { |
117 | if(width) *width = bmp->width; |
118 | if(height) *height = bmp->height; |
119 | if(bpp) *bpp = bmp->bpp; |
120 | if(data) *data = bmp->data; |
121 | } |
122 | |
123 | void SG_CALL__cdecl sgBitmapGetSize(SGBitmap* bmp, size_t* width, size_t* height) |
124 | { |
125 | if(width) *width = bmp->width; |
126 | if(height) *height = bmp->height; |
127 | } |
128 | size_t SG_CALL__cdecl sgBitmapGetWidth(SGBitmap* bmp) |
129 | { |
130 | return bmp->width; |
131 | } |
132 | size_t SG_CALL__cdecl sgBitmapGetHeight(SGBitmap* bmp) |
133 | { |
134 | return bmp->height; |
135 | } |
136 | |
137 | SGenum SG_CALL__cdecl sgBitmapGetBPP(SGBitmap* bmp) |
138 | { |
139 | return bmp->bpp; |
140 | } |