-
Notifications
You must be signed in to change notification settings - Fork 2
/
raw2bmp.cpp
321 lines (264 loc) · 10.5 KB
/
raw2bmp.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h> // uint16_t uint32_t
#include <string.h> // memset()
int gnMaxDepth = 1000; // max number of iterations == # of pixels to plot per complex number
int gnWidth = 1024; // image width
int gnHeight = 768; // image height
int gnScale = 10;
bool gbAutoBrightness = false;
// Default MaxDepth = 1000 @ 1042x768 has a maximum greyscale intensity = 5010 -> 230/5010 = filter out bottom 4.590808% of image as black
int gnGreyscaleBias = -230; // color pixel = (greyscale pixel + bias) * scale = 5010 - 230 = 4780
float gnScaleR = 0.09; // Default: (5010 - 230) * 0.09 = 430.2
float gnScaleG = 0.11; // Default: (5010 - 230) * 0.11 = 525.8
float gnScaleB = 0.18; // Default: (5010 - 230) * 0.18 = 860.4
// Output
uint16_t *gpGreyscaleTexels = NULL; // [ height ][ width ] 16-bit greyscale
uint8_t *gpChromaticTexels = NULL; // [ height ][ width ] 24-bit RGB
// ========================================================================
void AllocImageMemory( const int width, const int height )
{
const size_t area = width * height;
const size_t greyscaleBytes = area * sizeof( uint16_t );
gpGreyscaleTexels = (uint16_t*) malloc( greyscaleBytes ); // 1x 16-bit channel: K
memset( gpGreyscaleTexels, 0, greyscaleBytes );
const size_t chromaticBytes = area * 3 * sizeof( uint8_t ); // 3x 8-bit channels: R,G,B
gpChromaticTexels = (uint8_t*) malloc( chromaticBytes );
memset( gpChromaticTexels, 0, chromaticBytes );
}
// ========================================================================
void BMP_WriteColor24bit( const char * filename, const uint8_t *texelsRGB, const int width, const int height )
{
uint32_t headers[13]; // 54 bytes == 13 x int32
FILE * pFileSave;
int x, y, i;
// Stupid Windows BMP must have each scanline width padded to 4 bytes
int nExtraBytes = (width * 3) % 4;
int nPaddedSize = (width * 3 + nExtraBytes) * height;
uint32_t nPlanes = 1 ; // 1 plane
uint32_t nBitcount = 24 << 16; // 24-bit RGB; 32-bit packed for writing
// Header: Note that the "BM" identifier in bytes 0 and 1 is NOT included in these "headers".
headers[ 0] = nPaddedSize + 54; // bfSize (total file size)
headers[ 1] = 0; // bfReserved1 bfReserved2
headers[ 2] = 54; // bfOffbits
headers[ 3] = 40; // biSize BITMAPHEADER
headers[ 4] = width; // biWidth
headers[ 5] = height; // biHeight
headers[ 6] = nBitcount | nPlanes; // biPlanes, biBitcount
headers[ 7] = 0; // biCompression
headers[ 8] = nPaddedSize; // biSizeImage
headers[ 9] = 0; // biXPelsPerMeter
headers[10] = 0; // biYPelsPerMeter
headers[11] = 0; // biClrUsed
headers[12] = 0; // biClrImportant
pFileSave = fopen(filename, "wb");
if( pFileSave )
{
// Output Headers
fprintf(pFileSave, "BM");
for( i = 0; i < 13; i++ )
{
fprintf( pFileSave, "%c", ((headers[i]) >> 0) & 0xFF );
fprintf( pFileSave, "%c", ((headers[i]) >> 8) & 0xFF );
fprintf( pFileSave, "%c", ((headers[i]) >> 16) & 0xFF );
fprintf( pFileSave, "%c", ((headers[i]) >> 24) & 0xFF );
}
// Stupid Windows BMP are written upside down
for( y = height - 1; y >= 0; y-- )
{
const uint8_t* scanline = &texelsRGB[ y*width*3 ];
for( x = 0; x < width; x++ )
{
// swizzle rgb -> brg
uint8_t r = *scanline++;
uint8_t g = *scanline++;
uint8_t b = *scanline++;
// Stupid Windows BMP are written BGR
fprintf( pFileSave, "%c", b );
fprintf( pFileSave, "%c", g );
fprintf( pFileSave, "%c", r );
}
if( nExtraBytes ) // See above - BMP lines must be of lengths divisible by 4 bytes.
for( i = 0; i < nExtraBytes; i++ )
fprintf( pFileSave, "%c", 0 );
}
fclose( pFileSave );
}
}
// Scan all pixels and return the maximum brightness
// ========================================================================
uint16_t
Image_Greyscale16bitMaxValue( const uint16_t *texels, const int width, const int height )
{
const uint16_t *pSrc = texels;
const int nLen = width * height;
/* */ int nMax = *pSrc;
for( int iPix = 0; iPix < nLen; iPix++ )
{
if( nMax < *pSrc )
nMax = *pSrc;
pSrc++;
}
return nMax;
}
// ========================================================================
uint16_t
Image_Greyscale16bitToBrightnessBias( int* bias_, float* scaleR_, float* scaleG_, float* scaleB_ )
{
uint16_t nMaxBrightness = Image_Greyscale16bitMaxValue( gpGreyscaleTexels, gnWidth, gnHeight );
printf( "Max brightness: %d\n", nMaxBrightness );
if( gbAutoBrightness )
{
if( nMaxBrightness < 256)
*bias_ = 0;
// TODO: if bright < 256 should this be adjusted?
*bias_ = (int)(-0.045 * nMaxBrightness); // low-pass noise filter; if greyscale pixel < bias then greyscale pixel = 0
*scaleR_ = 430. / (float)nMaxBrightness;
*scaleG_ = 525. / (float)nMaxBrightness;
*scaleB_ = 860. / (float)nMaxBrightness;
}
return nMaxBrightness;
}
// @param greyscale Source greyscale texels to read
// @param chromatic_ Destination chromatic texels to write
// ========================================================================
void
Image_Greyscale16bitToColor24bit(
const uint16_t* greyscale, const int width, const int height,
/* */ uint8_t * chromatic_,
const int bias, const double scaleR, const double scaleG, const double scaleB )
{
const int nLen = width * height;
const uint16_t *pSrc = greyscale;
uint8_t *pDst = chromatic_;
for( int iPix = 0; iPix < nLen; iPix++ )
{
int i = *pSrc++ + bias ; // low pass noise filter
int r = (int)(i * scaleR);
int g = (int)(i * scaleG);
int b = (int)(i * scaleB);
if (r > 255) r = 255; if (r < 0) r = 0;
if (g > 255) g = 255; if (g < 0) g = 0;
if (b > 255) b = 255; if (b < 0) b = 0;
*pDst++ = r;
*pDst++ = g;
*pDst++ = b;
}
}
// ========================================================================
void
RAW_ReadGreyscale16bit( const char *filename, uint16_t *texels_, const int width, const int height )
{
FILE *file = fopen( filename, "rb" );
if( file )
{
const size_t area = width * height;
const size_t read = fread( texels_, sizeof( uint16_t ), area, file );
if (read != area)
printf( "Warning: Only read %d / %d bytes\n", (int)read, (int)area );
fclose( file );
}
}
// ========================================================================
void
RAW_WriteGreyscale16bit( const char *filename, const uint16_t *texels, const int width, const int height )
{
FILE *file = fopen( filename, "wb" );
if( file )
{
const size_t area = width * height;
fwrite( texels, sizeof( uint16_t ), area, file );
fclose( file );
}
}
// ========================================================================
inline bool isDigit( const char c )
{
return ((c >= '0') && (c <= '9'));
}
const char* textFind( const char *text, const char needle )
{
while( *text && *text != needle )
text++;
return text;
}
const char* textSkipDigits( const char *text )
{
while( *text && isDigit( *text ) )
text++;
return text;
}
// ========================================================================
void FindWidthHeight( const char *filename )
{
const size_t nLen = strlen( filename );
const char *pSrc = filename;
const char *pEnd = filename + nLen;
// *_#x#_#*.raw
// W H D
while( pSrc < pEnd )
{
if( *pSrc == '_' )
{
pSrc++;
if( isDigit( *pSrc ) )
{
gnWidth = atoi( pSrc );
pSrc = textSkipDigits( pSrc+1 );
while( pSrc < pEnd )
{
if (*pSrc == 'x' )
{
pSrc++;
if( isDigit( *pSrc ) )
{
gnHeight = atoi( pSrc );
pSrc = textSkipDigits( pSrc+1 );
while( pSrc < pEnd )
{
if( *pSrc == '_' )
{
pSrc++;
gnMaxDepth = atoi( pSrc );
pSrc = textSkipDigits( pSrc+1 );
}
pSrc++;
}
}
return;
}
pSrc++;
}
}
}
pSrc++;
}
}
void Raw2Bmp( const char *filenameRAW, const int width, const int height, const int depth )
{
printf( "Auto detect ... %d x %d @ %d\n", width, height, depth );
AllocImageMemory( width, height );
RAW_ReadGreyscale16bit( filenameRAW, gpGreyscaleTexels, width, height );
printf( "Loaded RAW: %s\n", filenameRAW );
char filenameBMP[ 256 ];
sprintf( filenameBMP, "buddhabrot_%dx%d_depth_%d_colorscaling_%d_scale_%dx.bmp", gnWidth, gnHeight, gnMaxDepth, (int)gbAutoBrightness, gnScale );
Image_Greyscale16bitToBrightnessBias( &gnGreyscaleBias, &gnScaleR, &gnScaleG, &gnScaleB ); // don't need max brightness
Image_Greyscale16bitToColor24bit( gpGreyscaleTexels, width, height, gpChromaticTexels, gnGreyscaleBias, gnScaleR, gnScaleG, gnScaleB );
BMP_WriteColor24bit( filenameBMP, gpChromaticTexels, width, height );
printf( "Saved BMP: %s\n", filenameBMP );
}
int main( int nArg, char *aArg[] )
{
if( nArg > 1 )
{
char *pFileName = aArg[1];
FindWidthHeight( pFileName );
if ((gnWidth > 0) && (gnHeight > 0))
{
Raw2Bmp( pFileName, gnWidth, gnHeight, gnMaxDepth );
}
}
return 0;
}