This repository was archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacker.cpp
More file actions
83 lines (78 loc) · 2.39 KB
/
packer.cpp
File metadata and controls
83 lines (78 loc) · 2.39 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
/**
* @package Byte-packing encoder/decoder
* @file Implementation file
* Projekt GKIM
* Grzegorz Kowalski
* Bartosz Zielnik
* Piotr Mańkowski
* Dariusz Szyszlak
* version 1 | 01.2016
*/
#include "packer.h"
/**
* Convert conv_bmp into byte-packed array
* @param new_bmp structure which holds BMP essentials
* @param size pointer to variable to hold the size of packed data
* @return packed array
*/
uint8_t *pack(conv_bmp* new_bmp, int *size)
{
uint8_t *out = (uint8_t*)malloc(new_bmp->height*new_bmp->width*3);
Uint8 doublecolor;
int i, j, z=0;
for(i=0;i<new_bmp->height;i++) //write pixel colors in order RGB
{
for(j=0;j<new_bmp->width;j++)
{
doublecolor = new_bmp->red_color[i][j] << 4;
doublecolor = doublecolor | new_bmp->green_color[i][j];
out[z++] = doublecolor;
doublecolor = new_bmp->blue_color[i][j] << (Uint8) 4;
doublecolor = doublecolor | new_bmp->red_color[i][j+1];
out[z++] = doublecolor;
doublecolor = new_bmp->green_color[i][j+1] << 4;
doublecolor = doublecolor | new_bmp->blue_color[i][j+1];
out[z++] = doublecolor;
j++;
}
}
*size = z; // size of packed data
return out;
}
/**
* Unpack byte-packed array into the conv_bmp structure
* @param width width of the image
* @param height height of the image
* @param data packed data
* @return unpacked conv_bmp structure
*/
conv_bmp *unpack(int width, int height, uint8_t *data)
{
Uint8 doublecolor;
conv_bmp *new_bmp = (conv_bmp*)malloc(sizeof(conv_bmp));
new_bmp->width = width;
new_bmp->height = height;
new_bmp->red_color = (Uint8**)malloc(sizeof(Uint8*) * height); //allocate memory for colors
new_bmp->green_color = (Uint8**)malloc(sizeof(Uint8*) * height);
new_bmp->blue_color = (Uint8**)malloc(sizeof(Uint8*) * height);
for(int i=0;i<height;i++)
{
new_bmp->red_color[i] = (Uint8*)malloc(sizeof(Uint8) * width);
new_bmp->green_color[i] = (Uint8*)malloc(sizeof(Uint8) * width);
new_bmp->blue_color[i] = (Uint8*)malloc(sizeof(Uint8) * width);
for(int j=0; j<width;j++)
{
doublecolor = *(data++);
new_bmp->red_color[i][j] = doublecolor >> 4;
new_bmp->green_color[i][j] = doublecolor & 15;
doublecolor = *(data++);
new_bmp->blue_color[i][j] = doublecolor >> 4;
new_bmp->red_color[i][j+1] = doublecolor & 15;
doublecolor = *(data++);
new_bmp->green_color[i][j+1] = doublecolor >> 4;
new_bmp->blue_color[i][j+1] = doublecolor & 15;
j++;
}
}
return new_bmp;
}