-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClmbsImg_Util.c
180 lines (140 loc) · 3.37 KB
/
ClmbsImg_Util.c
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
#include "ClmbsImg_Util.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
int ReadBytes(void* data, size_t size, FILE* fp)
{
if (fread(data, size, 1, fp) != 1) return 0;
return 1;
}
int WriteBytes(const void* data, size_t size, FILE* fp)
{
if (fwrite(data, size, 1, fp) != 1) return 0;
return 1;
}
int ReadUint8(uint8_t* data, FILE* fp)
{
return ReadBytes(data, sizeof(uint8_t), fp);
}
int WriteUint8(const uint8_t* data, FILE* fp)
{
return WriteBytes(data, sizeof(uint8_t), fp);
}
int ReadUint16(uint16_t* data, FILE* fp)
{
return ReadBytes(data, sizeof(uint16_t), fp);
}
int WriteUint16(const uint16_t* data, FILE* fp)
{
return WriteBytes(data, sizeof(uint16_t), fp);
}
int ReadUint32(uint32_t* data, FILE* fp)
{
return ReadBytes(data, sizeof(uint32_t), fp);
}
int WriteUint32(const uint32_t* data, FILE* fp)
{
return WriteBytes(data, sizeof(uint32_t), fp);
}
int ReadInt32(int32_t* data, FILE* fp)
{
return ReadBytes(data, sizeof(int32_t), fp);
}
int WriteInt32(const int32_t* data, FILE* fp)
{
return WriteBytes(data, sizeof(int32_t), fp);
}
int bgr2rgb(uint8_t* data, size_t size)
{
if (data == NULL) return 0;
uint8_t bgr[3];
for (int i = 0; i < size; i += 3)
{
bgr[0] = data[i + 0];
bgr[1] = data[i + 1];
bgr[2] = data[i + 2];
data[i + 0] = bgr[2];
data[i + 1] = bgr[1];
data[i + 2] = bgr[0];
}
return 1;
}
int bgra2rgba(uint8_t* data, size_t size)
{
if (data == NULL) return 0;
uint8_t bgr[3];
for (int i = 0; i < size; i += 4)
{
bgr[0] = data[i + 0];
bgr[1] = data[i + 1];
bgr[2] = data[i + 2];
data[i + 0] = bgr[2];
data[i + 1] = bgr[1];
data[i + 2] = bgr[0];
data[i + 3] = data[i + 3];
}
return 1;
}
int rgb2bgr(uint8_t* data, size_t size)
{
return bgr2rgb(data, size);
}
int rgba2bgra(uint8_t* data, size_t size)
{
return bgra2rgba(data, size);
}
int flipX(uint8_t* aData, size_t aWidth, size_t aHeight, size_t aBPP)
{
if (aData == NULL) return 0;
const size_t stride = aWidth * aBPP;
uint8_t* row;
uint8_t* pixel = (uint8_t*)malloc(aBPP);
for (size_t i = 0; i < aHeight; i++)
{
row = &aData[i * stride];
for (size_t j = 0; j < aWidth / 2; j++)
{
memcpy(pixel, &row[j * aBPP], aBPP);
memcpy(&row[j * aBPP], &row[(aWidth - j) * aBPP], aBPP);
memcpy(&row[(aWidth - j) * aBPP], pixel, aBPP);
}
}
return 1;
}
int flipY(uint8_t* aData, size_t aWidth, size_t aHeight, size_t aBPP)
{
if (aData == NULL) return 0;
const size_t stride = aWidth * aBPP;
uint8_t* row = (uint8_t*)malloc(stride);
for (size_t i = 0; i < aHeight / 2; i++)
{
memcpy(row, &aData[i * stride], stride);
memcpy(&aData[i * stride], &aData[(aHeight - i) * stride], stride);
memcpy(&aData[(aHeight - i) * stride], row, stride);
}
free(row);
return 1;
}
int flipXY(uint8_t* aData, size_t aWidth, size_t aHeight, size_t aBPP)
{
if (aData == NULL) return 0;
const size_t size = aWidth * aHeight;
//uint8_t* pixel = new uint8_t[aBPP];
uint8_t* pixel = (uint8_t*)malloc(sizeof(uint8_t) * aBPP);
for (size_t i = 0; i < size / 2; i++)
{
memcpy(pixel, &aData[i * aBPP], aBPP);
memcpy(&aData[i * aBPP], &aData[(size - i) * aBPP], aBPP);
memcpy(&aData[(size - i) * aBPP], pixel, aBPP);
}
free(pixel);
return 1;
}
#ifdef __cplusplus
}
#endif