Repository navigation
Expand file tree
/
Copy pathstr_io.c
More file actions
206 lines (176 loc) · 5.67 KB
/
Copy pathstr_io.c
File metadata and controls
206 lines (176 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "str_io.h"
#include "zlib_util.h"
#include "base64.h"
#include "snprintf.h" /* snprintf() */
#include <stdio.h> /* fputs() */
#include <ctype.h> /* isdigit() */
#include <stdlib.h> /* atoi() */
#include <string.h> /* strlen() */
#include <assert.h>
#if defined(_MSC_VER)
#include "ms_stdbool.h"
#else
#include <stdbool.h>
#endif
#define STR_BITS_PER_PIXEL 24
#define STR_BYTES_PER_PIXEL ((STR_BITS_PER_PIXEL) / 8)
#define MAX_DIMENSION_LEN 5 /* Maximum length for [width] or [height]
* in string. */
const char *MMBitmapStringErrorString(MMBMPStringError err)
{
switch (err) {
case kMMBMPStringInvalidHeaderError:
return "Invalid header for string";
case kMMBMPStringDecodeError:
return "Error decoding string";
case kMMBMPStringDecompressError:
return "Error decompressing string";
case kMMBMPStringSizeError:
return "String not of expected size";
case MMMBMPStringEncodeError:
return "Error encoding string";
case kMMBMPStringCompressError:
return "Error compressing string";
default:
return NULL;
}
}
/* Parses beginning of string in the form of "[width],[height],*".
*
* If successful, |width| and |height| are set to the appropropriate values,
* |len| is set to the length of [width] + the length of [height] + 2,
* and true is returned; otherwise, false is returned.
*/
static bool getSizeFromString(const uint8_t *buf, size_t buflen,
size_t *width, size_t *height,
size_t *len);
MMBitmapRef createMMBitmapFromString(const uint8_t *buffer, size_t buflen,
MMBMPStringError *err)
{
uint8_t *decoded, *decompressed;
size_t width, height;
size_t len, bytewidth;
if (*buffer++ != 'b' || !getSizeFromString(buffer, --buflen,
&width, &height, &len)) {
if (err != NULL) *err = kMMBMPStringInvalidHeaderError;
return NULL;
}
buffer += len;
buflen -= len;
decoded = base64decode(buffer, buflen, NULL);
if (decoded == NULL) {
if (err != NULL) *err = kMMBMPStringDecodeError;
return NULL;
}
decompressed = zlib_decompress(decoded, &len);
free(decoded);
if (decompressed == NULL) {
if (err != NULL) *err = kMMBMPStringDecompressError;
return NULL;
}
bytewidth = width * STR_BYTES_PER_PIXEL; /* Note that bytewidth is NOT
* aligned to a padding. */
if (height * bytewidth != len) {
if (err != NULL) *err = kMMBMPStringSizeError;
return NULL;
}
return createMMBitmap(decompressed, width, height,
bytewidth, STR_BITS_PER_PIXEL, STR_BYTES_PER_PIXEL);
}
/* Returns bitmap data suitable for encoding to a string; that is, 24-bit BGR
* bitmap with no padding and 3 bytes per pixel.
*
* Caller is responsible for free()'ing returned buffer. */
static uint8_t *createRawBitmapData(MMBitmapRef bitmap);
uint8_t *createStringFromMMBitmap(MMBitmapRef bitmap, MMBMPStringError *err)
{
uint8_t *raw, *compressed;
uint8_t *ret, *encoded;
size_t len, retlen;
assert(bitmap != NULL);
raw = createRawBitmapData(bitmap);
if (raw == NULL) {
if (err != NULL) *err = kMMBMPStringGenericError;
return NULL;
}
compressed = zlib_compress(raw,
bitmap->width * bitmap->height *
STR_BYTES_PER_PIXEL,
9, &len);
free(raw);
if (compressed == NULL) {
if (err != NULL) *err = kMMBMPStringCompressError;
return NULL;
}
encoded = base64encode(compressed, len - 1, &retlen);
free(compressed);
if (encoded == NULL) {
if (err != NULL) *err = MMMBMPStringEncodeError;
return NULL;
}
retlen += 3 + (MAX_DIMENSION_LEN * 2);
ret = calloc(sizeof(char), (retlen + 1));
snprintf((char *)ret, retlen, "b%lu,%lu,%s", (unsigned long)bitmap->width,
(unsigned long)bitmap->height,
encoded);
ret[retlen] = '\0';
free(encoded);
return ret;
}
static uint32_t parseDimension(const uint8_t *buf, size_t buflen,
size_t *numlen);
static bool getSizeFromString(const uint8_t *buf, size_t buflen,
size_t *width, size_t *height,
size_t *len)
{
size_t numlen;
assert(buf != NULL);
assert(width != NULL);
assert(height != NULL);
if ((*width = parseDimension(buf, buflen, &numlen)) == 0) {
return false;
}
*len = numlen + 1;
if ((*height = parseDimension(buf + *len, buflen, &numlen)) == 0) {
return false;
}
*len += numlen + 1;
return true;
}
/* Parses one dimension from string as described in getSizeFromString().
* Returns dimension on success, or 0 on error. */
static uint32_t parseDimension(const uint8_t *buf, size_t buflen,
size_t *numlen)
{
char num[MAX_DIMENSION_LEN + 1];
size_t i;
assert(buf != NULL);
assert(len != NULL);
for (i = 0; i < buflen && buf[i] != ',' && buf[i] != '\0'; ++i) {
if (!isdigit(buf[i]) || i > MAX_DIMENSION_LEN) return 0;
num[i] = buf[i];
}
num[i] = '\0';
*numlen = i;
return (uint32_t)atoi(num);
}
static uint8_t *createRawBitmapData(MMBitmapRef bitmap)
{
uint8_t *raw = calloc(STR_BYTES_PER_PIXEL, bitmap->width * bitmap->height);
size_t y;
for (y = 0; y < bitmap->height; ++y) {
/* No padding is added to string bitmaps. */
const size_t rowOffset = y * bitmap->width * STR_BYTES_PER_PIXEL;
size_t x;
for (x = 0; x < bitmap->width; ++x) {
/* Copy in BGR format. */
const size_t colOffset = x * STR_BYTES_PER_PIXEL;
uint8_t *dest = raw + rowOffset + colOffset;
MMRGBColor *srcColor = MMRGBColorRefAtPoint(bitmap, x, y);
dest[0] = srcColor->blue;
dest[1] = srcColor->green;
dest[2] = srcColor->red;
}
}
return raw;
}