- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
bool bitmap_to_24bit_string(bitmap *bmp, char **str, uint32_t *len)
{
if (!bmp || !bmp->pixels)
return false;
int I, J;
uint32_t size = ((bmp->width * 24 + 31) / 32) * 4 * bmp->height;
rgb24 *pixels = malloc(size);
if (pixels)
{
for (I = 0; I < bmp->height; ++I)
{
for (J = 0; J < bmp->width; ++J)
{
pixels[I * bmp->width + J].b = bmp->pixels[I * bmp->width + J].r;
pixels[I * bmp->width + J].g = bmp->pixels[I * bmp->width + J].g;
pixels[I * bmp->width + J].r = bmp->pixels[I * bmp->width + J].b;
}
}
uint32_t destlen = compressBound(size);
*str = malloc(destlen);
if (*str)
{
if (compress((Bytef *)*str, (uLongf *)&destlen, (Bytef *)pixels, size) == Z_OK)
{
free(pixels);
pixels = NULL;
char *b64str;
uint32_t b64_len;
if (base64encode((const uint8_t *)*str, destlen, &b64str, &b64_len))
{
free(*str);
*str = b64str;
*len = b64_len + 2;
b64str = malloc(*len);
if (b64str)
{
b64str[0] = 'm';
strncpy(&b64str[1], *str, b64_len);
free(*str);
*str = b64str;
(*str)[b64_len + 1] = '';
return true;
}
}
}
free(*str);
*len = 0;
*str = NULL;
}
free(pixels);
}
return false;
}
gost 02.12.2014 14:13 # 0