-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBIL.h
4434 lines (3939 loc) · 304 KB
/
BIL.h
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************
Basic Image Library (BIL)
2021-2024
Author: Andrea Marchi (diescc@gmail.com)
version: v1.0 (19/08/2024)
****************************************/
#ifndef MRC_IMAGE_HPP
#define MRC_IMAGE_HPP
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#ifndef MRC_BASIC_IMAGE
#define MRC_BASIC_IMAGE
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class BasicImage
{
protected:
unsigned int _width;
unsigned int _height;
std::vector<unsigned char> data; // data in array format (array of bytes)
unsigned int bytes_per_pixel;
unsigned int bytes_per_row;
public:
BasicImage() : _width(0), _height(0), bytes_per_pixel(3) {}
BasicImage(const std::string fileName) : _width(0), _height(0), bytes_per_pixel(0) { load_file(fileName); } // É NECESSARIO CHE SI SETTINO LE VARIABILI INTERNE??? no se lo fa load_file...
BasicImage(const unsigned int width, const unsigned int height);
//setters / getters:
void set_pixel(const unsigned int x, const unsigned int y, const unsigned char red, const unsigned char green, const unsigned char blue);
void get_pixel(const unsigned int x, const unsigned int y, unsigned char& red, unsigned char& green, unsigned char& blue) const;
inline unsigned int width() const { return _width; }
inline unsigned int height() const { return _height; }
// save and load bitmap files
void save_file(std::string fileName, unsigned char quality);
void load_file(std::string fileName);
protected:
//auxiliary functions:
template<typename T> inline void write_to_stream(std::ofstream& stream,const T& t) const { stream.write(reinterpret_cast<const char*>(&t),sizeof(T)); }
template<typename T> inline void read_from_stream(std::ifstream& stream,T& t) { stream.read(reinterpret_cast<char*>(&t),sizeof(T)); }
// inline bool big_endian() const { unsigned int v = 0x01; return (1 != reinterpret_cast<char*>(&v)[0]); }
// inline unsigned short flip(const unsigned short& v) const { return ((v >> 8) | (v << 8)); }
// inline unsigned int flip(const unsigned int& v) const { return ( ((v & 0xFF000000) >> 0x18) | ((v & 0x000000FF) << 0x18) | ((v & 0x00FF0000) >> 0x08) | ((v & 0x0000FF00) << 0x08) ); }
};
// CONSTANTS =================================================================================================================================
// DEFINITIONS ================================================================================================================================
BasicImage::BasicImage(const unsigned int width, const unsigned int height)
: _width(width), _height(height), bytes_per_pixel(3)
{
bytes_per_row = _width * bytes_per_pixel;
data.resize(_height * bytes_per_row);
std::fill(data.begin(), data.end(), 0x00);
}
void BasicImage::set_pixel(const unsigned int x, const unsigned int y, const unsigned char red, const unsigned char green, const unsigned char blue)
{
if( (x<0) || (y<0) || (x>=static_cast<int>(_width)) || (y>=static_cast<int>(_height)) ) return;
// Set pixel color in RGB mode
const unsigned int y_offset = y * bytes_per_row;
const unsigned int x_offset = x * bytes_per_pixel;
const unsigned int offset = y_offset + x_offset;
data[offset + 0] = red;
data[offset + 1] = green;
data[offset + 2] = blue;
}
void BasicImage::get_pixel(const unsigned int x, const unsigned int y, unsigned char& red, unsigned char& green, unsigned char& blue) const
{
if( (x<0) || (y<0) || (x>=static_cast<int>(_width)) || (y>=static_cast<int>(_height)) ) return;
// Get pixel color in RGB mode
const unsigned int y_offset = y * bytes_per_row;
const unsigned int x_offset = x * bytes_per_pixel;
const unsigned int offset = y_offset + x_offset;
red = data[offset + 0];
green = data[offset + 1];
blue = data[offset + 2];
}
void BasicImage::save_file(std::string fileName, unsigned char quality)
{
// To be overloaded...
}
void BasicImage::load_file(std::string fileName)
{
// To be overloaded...
}
// AUXILIARY FUNCTIONS ================================================================================================================================
inline std::size_t file_size(const std::string& file_name)
{
std::ifstream file(file_name.c_str(), std::ios::in | std::ios::binary);
if (!file) return 0;
file.seekg(0, std::ios::end);
return static_cast<std::size_t> (file.tellg());
}
#endif // MRC_BASIC_IMAGE
#ifndef MRC_BITMAP
#define MRC_BITMAP
#include <iostream>
#include <fstream>
#include <cstring> // for "memset"
#include <string>
#include <vector>
class Bitmap : public BasicImage
{
public:
enum channel_mode { rgb_mode = 0, bgr_mode = 1 };
struct bitmap_file_header {
unsigned short type;
unsigned int size;
unsigned short reserved1;
unsigned short reserved2;
unsigned int off_bits;
unsigned int struct_size() const { return sizeof(type) + sizeof(size) + sizeof(reserved1) + sizeof(reserved2) + sizeof(off_bits); }
void clear() { std::memset(this, 0x00, sizeof(bitmap_file_header)); }
};
struct bitmap_information_header {
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short planes;
unsigned short bit_count;
unsigned int compression;
unsigned int size_image;
unsigned int x_pels_per_meter;
unsigned int y_pels_per_meter;
unsigned int clr_used;
unsigned int clr_important;
unsigned int struct_size() const { return sizeof(size) + sizeof(width) + sizeof(height) + sizeof(planes) + sizeof(bit_count) + sizeof(compression) + sizeof(size_image) + sizeof(x_pels_per_meter) + sizeof(y_pels_per_meter) + sizeof(clr_used) + sizeof(clr_important); }
void clear(){ std::memset(this, 0x00, sizeof(bitmap_information_header)); }
};
public:
// using BasicImage::BasicImage; // inherit constructors (C++11)
Bitmap() { _width=0; _height=0; bytes_per_pixel=0; }
Bitmap(const std::string fileName) { _width=0; _height=0; bytes_per_pixel=0; load_file(fileName); } // É NECESSARIO CHE SI SETTINO LE VARIABILI INTERNE??? no se lo fa load_file...
Bitmap(const unsigned int width, const unsigned int height);
void save_file(std::string fileName); // overload
void load_file(std::string fileName); // overload
private:
bitmap_file_header bfh;
bitmap_information_header bih;
// auxilioary functions: QUESTE FUNZIONI FORSE SONO SIMILI A QUELLE IN png_utils.h (CONTROLLARE E SISTEMARE!!!!!!!!!!!!!!!)
inline bool big_endian() const { unsigned int v = 0x01; return (1 != reinterpret_cast<char*>(&v)[0]); }
inline unsigned short flip(const unsigned short& v) const { return ((v >> 8) | (v << 8)); }
inline unsigned int flip(const unsigned int& v) const { return ( ((v & 0xFF000000) >> 0x18) | ((v & 0x000000FF) << 0x18) | ((v & 0x00FF0000) >> 0x08) | ((v & 0x0000FF00) << 0x08) ); }
void read_bfh(std::ifstream& stream, bitmap_file_header& bfh);
void write_bfh(std::ofstream& stream, const bitmap_file_header& bfh) const;
void read_bih(std::ifstream& stream,bitmap_information_header& bih);
void write_bih(std::ofstream& stream, const bitmap_information_header& bih) const;
std::size_t file_size(const std::string& file_name) const;
};
// =================================================================================================================================
Bitmap::Bitmap(const unsigned int width, const unsigned int height)
{
_width = width;
_height = height;
bytes_per_pixel = 3; // un byte per ogni componente RGB
bytes_per_row = _width * bytes_per_pixel;
data.resize(_height * bytes_per_row);
}
void Bitmap::save_file(std::string fileName)
{
std::ofstream file(fileName.c_str(),std::ios::binary);
if (!file){
std::cerr << "bitmap_image::save_image(): Error - Could not open file " << fileName << " for writing!" << std::endl;
return;
}
bitmap_information_header bih;
bih.width = _width;
bih.height = _height;
bih.bit_count = static_cast<unsigned short>(bytes_per_pixel << 3);
bih.clr_important = 0;
bih.clr_used = 0;
bih.compression = 0;
bih.planes = 1;
bih.size = bih.struct_size();
bih.x_pels_per_meter = 0;
bih.y_pels_per_meter = 0;
bih.size_image = (((bih.width * bytes_per_pixel) + 3) & 0x0000FFFC) * bih.height;
bitmap_file_header bfh;
bfh.type = 19778;
bfh.size = bfh.struct_size() + bih.struct_size() + bih.size_image;
bfh.reserved1 = 0;
bfh.reserved2 = 0;
bfh.off_bits = bih.struct_size() + bfh.struct_size();
write_bfh(file, bfh);
write_bih(file, bih);
unsigned int padding = (4 - ((3 * _width) % 4)) % 4;
char padding_data[4] = { 0x00, 0x00, 0x00, 0x00 };
for(unsigned int i=0; i<_height; i++){
const unsigned char* data_ptr = &data[(bytes_per_row * (_height - i - 1))];
file.write(reinterpret_cast<const char*>(data_ptr), sizeof(unsigned char) * bytes_per_pixel * _width);
file.write(padding_data, padding);
}
file.close();
}
void Bitmap::load_file(std::string fileName)
{
std::ifstream stream(fileName.c_str(), std::ios::binary);
if(!stream){
std::cerr << "Bitmap::load_file() ERROR: file " << fileName << " not found!" << std::endl;
return;
}
_width = 0;
_height = 0;
bitmap_file_header bfh;
bitmap_information_header bih;
bfh.clear();
bih.clear();
read_bfh(stream,bfh);
read_bih(stream,bih);
if(bfh.type != 19778){
std::cerr << "Bitmap::load_file() ERROR: Invalid type value " << bfh.type << " expected 19778." << std::endl;
bfh.clear(); bih.clear();
stream.close();
return;
}
if(bih.bit_count != 24){
std::cerr << "Bitmap::load_file() ERROR: Invalid bit depth " << bih.bit_count << " expected 24." << std::endl;
bfh.clear(); bih.clear();
stream.close();
return;
}
if(bih.size != bih.struct_size()){
std::cerr << "Bitmap::load_file() ERROR: Invalid BIH size " << bih.size << " expected " << bih.struct_size() << std::endl;
bfh.clear(); bih.clear();
stream.close();
return;
}
_width = bih.width;
_height = bih.height;
bytes_per_pixel = bih.bit_count >> 3;
bytes_per_row = _width * bytes_per_pixel;
unsigned int padding = (4 - ((3 * _width) % 4)) % 4;
char padding_data[4] = { 0x00, 0x00, 0x00, 0x00 };
std::size_t bitmap_file_size = file_size(fileName);
std::size_t bitmap_logical_size = (_height * bytes_per_row) + (_height * padding) + bih.struct_size() + bfh.struct_size();
if(bitmap_file_size != bitmap_logical_size){
std::cerr << "Bitmap::load_file() ERROR: Mismatch between logical and physical sizes of bitmap. Logical: " << bitmap_logical_size << " Physical: " << bitmap_file_size << std::endl;
bfh.clear(); bih.clear();
stream.close();
return;
}
data.resize(_height * bytes_per_row); // allocate memory
for(unsigned i=0; i<_height; i++){
unsigned char* data_ptr = &data[((_height-i-1) * bytes_per_row)]; // read in inverted row order
stream.read(reinterpret_cast<char*>(data_ptr), sizeof(char) * bytes_per_row);
stream.read(padding_data, padding);
}
}
void Bitmap::read_bfh(std::ifstream& stream, bitmap_file_header& bfh)
{
read_from_stream(stream, bfh.type );
read_from_stream(stream, bfh.size );
read_from_stream(stream, bfh.reserved1);
read_from_stream(stream, bfh.reserved2);
read_from_stream(stream, bfh.off_bits );
if (big_endian()){
bfh.type = flip(bfh.type );
bfh.size = flip(bfh.size );
bfh.reserved1 = flip(bfh.reserved1);
bfh.reserved2 = flip(bfh.reserved2);
bfh.off_bits = flip(bfh.off_bits );
}
}
void Bitmap::write_bfh(std::ofstream& stream, const bitmap_file_header& bfh) const
{
if(big_endian()){
write_to_stream(stream,flip(bfh.type ));
write_to_stream(stream,flip(bfh.size ));
write_to_stream(stream,flip(bfh.reserved1));
write_to_stream(stream,flip(bfh.reserved2));
write_to_stream(stream,flip(bfh.off_bits ));
}else{
write_to_stream(stream,bfh.type );
write_to_stream(stream,bfh.size );
write_to_stream(stream,bfh.reserved1);
write_to_stream(stream,bfh.reserved2);
write_to_stream(stream,bfh.off_bits );
}
}
void Bitmap::read_bih(std::ifstream& stream,bitmap_information_header& bih)
{
read_from_stream(stream,bih.size );
read_from_stream(stream,bih.width );
read_from_stream(stream,bih.height );
read_from_stream(stream,bih.planes );
read_from_stream(stream,bih.bit_count );
read_from_stream(stream,bih.compression );
read_from_stream(stream,bih.size_image );
read_from_stream(stream,bih.x_pels_per_meter);
read_from_stream(stream,bih.y_pels_per_meter);
read_from_stream(stream,bih.clr_used );
read_from_stream(stream,bih.clr_important );
if(big_endian()){
bih.size = flip(bih.size );
bih.width = flip(bih.width );
bih.height = flip(bih.height );
bih.planes = flip(bih.planes );
bih.bit_count = flip(bih.bit_count );
bih.compression = flip(bih.compression );
bih.size_image = flip(bih.size_image );
bih.x_pels_per_meter = flip(bih.x_pels_per_meter);
bih.y_pels_per_meter = flip(bih.y_pels_per_meter);
bih.clr_used = flip(bih.clr_used );
bih.clr_important = flip(bih.clr_important );
}
}
void Bitmap::write_bih(std::ofstream& stream, const bitmap_information_header& bih) const
{
if(big_endian()){
write_to_stream(stream,flip(bih.size ));
write_to_stream(stream,flip(bih.width ));
write_to_stream(stream,flip(bih.height ));
write_to_stream(stream,flip(bih.planes ));
write_to_stream(stream,flip(bih.bit_count ));
write_to_stream(stream,flip(bih.compression ));
write_to_stream(stream,flip(bih.size_image ));
write_to_stream(stream,flip(bih.x_pels_per_meter));
write_to_stream(stream,flip(bih.y_pels_per_meter));
write_to_stream(stream,flip(bih.clr_used ));
write_to_stream(stream,flip(bih.clr_important ));
}else{
write_to_stream(stream,bih.size );
write_to_stream(stream,bih.width );
write_to_stream(stream,bih.height );
write_to_stream(stream,bih.planes );
write_to_stream(stream,bih.bit_count );
write_to_stream(stream,bih.compression );
write_to_stream(stream,bih.size_image );
write_to_stream(stream,bih.x_pels_per_meter);
write_to_stream(stream,bih.y_pels_per_meter);
write_to_stream(stream,bih.clr_used );
write_to_stream(stream,bih.clr_important );
}
}
std::size_t Bitmap::file_size(const std::string& file_name) const
{
std::ifstream file(file_name.c_str(),std::ios::in | std::ios::binary);
if (!file) return 0;
file.seekg (0, std::ios::end);
return static_cast<std::size_t>(file.tellg());
}
#endif // MRC_BITMAP
/************************************************************
ZLIB
ToDo:
- Mettere tutto dentro una struct? con funzioni private e
pubbliche, in modo tale che è semplice capire l'utilizzo
della libreria (ad esempio zlib.deflate() / zlib.inflate())
***********************************************************/
#ifndef MRC_ZLIB
#define MRC_ZLIB
//#include <vector> ?????
#include <assert.h>
#include <string.h>
#define HASH_SIZE 16384
//#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
#define stbiw__sbraw(a) ((int *) (void *) (a) - 2)
#define stbiw__sbm(a) stbiw__sbraw(a)[0]
#define stbiw__sbn(a) stbiw__sbraw(a)[1]
#define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
#define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
#define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
#define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
#define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)
#define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0)
#define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount))
#define stbiw__zlib_add(code,codebits) (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush())
// default huffman tables
#define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c)
#define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8)
#define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9)
#define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7)
#define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8)
#define stbiw__zlib_huff(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n))
#define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n))
#define stbiw__wpng4(o,a,b,c,d) ((o)[0]=(unsigned char)(a & 0xFF),(o)[1]=(unsigned char)(b & 0xFF),(o)[2]=(unsigned char)(c & 0xFF),(o)[3]=(unsigned char)(d & 0xFF),(o)+=4)
#define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v));
#define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3])
#define STBIW_REALLOC(p,newsz) realloc(p,newsz)
#define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz)
static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
{
int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2);
assert(p);
if (p) {
if (!*arr) ((int *) p)[1] = 0;
*arr = (void *) ((int *) p + 2);
stbiw__sbm(*arr) = m;
}
return *arr;
}
static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount)
{
while (*bitcount >= 8) {
stbiw__sbpush(data, (unsigned char)(*bitbuffer & 0xff));
*bitbuffer >>= 8;
*bitcount -= 8;
}
return data;
}
static unsigned int stbiw__zhash(unsigned char *data)
{
unsigned int hash = data[0] + (data[1] << 8) + (data[2] << 16);
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit)
{
int i;
for (i=0; i < limit && i < 258; ++i) if (a[i] != b[i]) break;
return i;
}
static int stbiw__zlib_bitrev(int code, int codebits)
{
int res=0;
while (codebits--) {
res = (res << 1) | (code & 1);
code >>= 1;
}
return res;
}
unsigned int zlib_crc32(unsigned char *buffer, int len)
{
// CRC32B:
// Reflected: 0xEDB88320: &1 >> [128] = poly , [ 8] = poly >> 8 VALID
// Poly: x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1 [0xEDB88320]
static unsigned int crc_table[256] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
unsigned int crc = ~0u;
for(int i=0; i<len; i++) crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xFF)];
return ~crc;
}
unsigned zlib_crc32_slow(unsigned char* data, int length)
{
// lento perché ricalcola ogni volta la tabella dal polinomio
static unsigned* crcTable;
unsigned crc; //32 bit unsigned data
unsigned poly = 0xEDB88320; //polynomial
if(crcTable == nullptr){
crcTable=new unsigned[256];
for(unsigned n=0;n<256;n++){
crc = n;
for(auto k=0;k<8;k++){
if((crc & 1) == 1) crc = poly ^ (crc >> 1);
else crc >>= 1;
}
crcTable[n] = crc;
}
}
crc = ~0u;
for(auto i=0; i<length; i++) crc = (crc >> 8) ^ crcTable[data[i] ^ (crc & 0xFF)]; // Reversed
return ~crc; //same as c ^ 0xFFFFFFFF
}
static void stbiw__wpcrc(unsigned char **data, int len)
{
unsigned int crc = zlib_crc32(*data - len - 4, len+4);
stbiw__wp32(*data, crc);
}
static unsigned char stbiw__paeth(int a, int b, int c)
{
int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c);
if (pa <= pb && pa <= pc) return (unsigned char)(a & 0xFF);
if (pb <= pc) return (unsigned char)(b & 0xFF);
return (unsigned char)(c & 0xFF);
}
// =================================================================================
unsigned char * zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)
{
static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 };
static unsigned char lengtheb[] = { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 };
static unsigned char disteb[] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 };
unsigned int bitbuf = 0;
int bitcount = 0;
unsigned char *out = nullptr;
// unsigned char ***hash_table = (unsigned char***) malloc(zlib_ZHASH * sizeof(unsigned char**)); // OLD
unsigned char ***hash_table = new unsigned char ** [HASH_SIZE * sizeof(unsigned char**)]; // COME MAI TUTTI QUESTI * ??
if (hash_table == nullptr) return nullptr; // return error if it cannot allocate memory
if (quality < 5) quality = 5;
stbiw__sbpush(out, 0x78); // DEFLATE 32K window
stbiw__sbpush(out, 0x5e); // FLEVEL = 1
stbiw__zlib_add(1,1); // BFINAL = 1
stbiw__zlib_add(1,2); // BTYPE = 1 -- fixed huffman
for(int i=0; i < HASH_SIZE; i++) hash_table[i] = nullptr; // reset the hash_table
int count = 0;
while (count < data_len-3) {
// hash next 3 bytes of data to be compressed
int h = stbiw__zhash(data+count)&(HASH_SIZE-1), best=3;
unsigned char *bestloc = 0;
unsigned char **hlist = hash_table[h];
int n = stbiw__sbcount(hlist);
for(int j=0; j<n; ++j){
if(hlist[j]-data > count-32768) { // if entry lies within window
int d = stbiw__zlib_countm(hlist[j], data+count, data_len-count);
if(d >= best) { best=d; bestloc=hlist[j]; }
}
}
// when hash table entry is too long, delete half the entries
if(hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) {
memmove(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality);
stbiw__sbn(hash_table[h]) = quality;
}
stbiw__sbpush(hash_table[h],data+count);
if(bestloc){
// "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal
h = stbiw__zhash(data+count+1)&(HASH_SIZE-1);
hlist = hash_table[h];
n = stbiw__sbcount(hlist);
for(int j=0; j<n; ++j){
if(hlist[j]-data > count-32767) {
int e = stbiw__zlib_countm(hlist[j], data+count+1, data_len-count-1);
if(e > best) { // if next match is better, bail on current match
bestloc = nullptr;
break;
}
}
}
}
if(bestloc){
int temp;
int d = (int) (data+count - bestloc); // distance back
assert(d <= 32767 && best <= 258);
for(temp=0; best > lengthc[temp+1]-1; ++temp);
stbiw__zlib_huff(temp+257); // SCRIVE DOPO IL FOR???
if(lengtheb[temp]) stbiw__zlib_add(best - lengthc[temp], lengtheb[temp]);
for(temp=0; d > distc[temp+1]-1; ++temp);
stbiw__zlib_add(stbiw__zlib_bitrev(temp,5),5);
if(disteb[temp]) stbiw__zlib_add(d - distc[temp], disteb[temp]);
count += best;
}else{
stbiw__zlib_huffb(data[count]);
count++;
}
}
// write out final bytes
for(;count < data_len; ++count) stbiw__zlib_huffb(data[count]);
stbiw__zlib_huff(256); // end of block
// pad with 0 bits to byte boundary
while(bitcount) stbiw__zlib_add(0,1);
//clear hash_table from memory
// for (int i=0; i<HASH_SIZE; ++i) delete [] hash_table[i];
delete [] hash_table;
// store uncompressed instead if compression was worse
if(stbiw__sbn(out) > data_len + 2 + ((data_len+32766)/32767)*5){
stbiw__sbn(out) = 2; // truncate to DEFLATE 32K window and FLEVEL = 1
for(int j = 0; j < data_len;){
int blocklen = data_len - j;
if(blocklen > 32767) blocklen = 32767;
stbiw__sbpush(out, data_len - j == blocklen); // BFINAL = ?, BTYPE = 0 -- no compression
stbiw__sbpush(out, (unsigned char)(blocklen & 0xFF)); // LEN
stbiw__sbpush(out, (unsigned char)((blocklen >> 8) & 0xFF));
stbiw__sbpush(out, (unsigned char)(~blocklen & 0xFF)); // NLEN
stbiw__sbpush(out, (unsigned char)((~blocklen >> 8) & 0xFF));
memcpy(out+stbiw__sbn(out), data+j, blocklen);
stbiw__sbn(out) += blocklen;
j += blocklen;
}
}
{
// compute adler32 on input
unsigned int s1=1, s2=0;
int blocklen = (int) (data_len % 5552);
count = 0;
while (count < data_len) {
for (int i=0; i < blocklen; i++) { s1 += data[count+i]; s2 += s1; }
s1 %= 65521; s2 %= 65521; // 65521 : largest prime smaller than 65536
count += blocklen;
blocklen = 5552;
}
stbiw__sbpush(out, (unsigned char)((s2 >> 8) & 0xFF));
stbiw__sbpush(out, (unsigned char)(s2 & 0xFF));
stbiw__sbpush(out, (unsigned char)((s1 >> 8) & 0xFF));
stbiw__sbpush(out, (unsigned char)(s1 & 0xFF));
}
*out_len = stbiw__sbn(out);
// make returned pointer freeable
memmove(stbiw__sbraw(out), out, *out_len);
return (unsigned char *) stbiw__sbraw(out);
}
#endif // MRC_ZLIB
#ifndef MRC_PNG_UTILS
#define MRC_PNG_UTILS
#include <vector>
#include <fstream>
// Reverse bits to change endianness
template <typename T>
T reverse(T n, unsigned bits_num)
{
T rv = 0;
REVERSE_BITS:
for (int i = 0; i < bits_num; i++)
{
#pragma HLS PIPELINE
rv <<= 1;
rv |= (n & 0x01);
n >>= 1;
}
return rv;
}
// usare sizeof(T) invece di bits_num????
inline std::vector<unsigned char> str2bvec(std::string str)
{
// converte una stringa in un vettore di bytes
std::vector<unsigned char> res;
for(int i=0; i<str.length(); i++) res.push_back(str[i]);
return res;
}
template<typename T>
inline void write_to_stream(std::ofstream& stream, const std::vector<T>& t)
{
for(auto i=0; i<t.size(); i++)
stream.write(reinterpret_cast<const char*>(&t[i]), sizeof(T));
}
template<typename T>
inline void append_to_vector(std::vector<T> &vec, std::vector<T> v)
{
// append the vector v to the vector vec
for(auto i=0; i<v.size(); i++)
vec.push_back(v[i]);
}
inline std::vector<unsigned char> u32bigEndian2vec(int32_t x)
{
// convert a 32 bit integer into a byte vector in Big-Endian notation
std::vector<unsigned char> res;
res.push_back((x & 0xFF000000) >> 24);
res.push_back((x & 0x00FF0000) >> 16);
res.push_back((x & 0x0000FF00) >> 8);
res.push_back((x & 0x000000FF) );
return res;
}
template<typename T>
inline std::vector<unsigned char> bigEndian2vec(T x)
{
// convert an integer into a byte vector in Big-Endian notation
std::vector<unsigned char> res;
if(sizeof(T) == 1){
// 8 bit input
res.push_back(x);
}else
if(sizeof(T) == 2){
// 16 bit input
res.push_back((x & 0xFF00) >> 8);
res.push_back((x & 0x00FF) );
}else
if(sizeof(T) == 4){
// 32 bit input
res.push_back((x & 0xFF000000) >> 24);
res.push_back((x & 0x00FF0000) >> 16);
res.push_back((x & 0x0000FF00) >> 8);
res.push_back((x & 0x000000FF) );
}else
if(sizeof(T) == 8){
// 64 bit input
res.push_back((x & 0xFF00000000000000) >> 56);
res.push_back((x & 0x00FF000000000000) >> 48);
res.push_back((x & 0x0000FF0000000000) >> 40);
res.push_back((x & 0x000000FF00000000) >> 32);
res.push_back((x & 0x00000000FF000000) >> 24);
res.push_back((x & 0x0000000000FF0000) >> 16);
res.push_back((x & 0x000000000000FF00) >> 8);
res.push_back((x & 0x00000000000000FF) );
}
return res;
}
#endif // MRC_PNG_UTILS
/****************************************************
PNG image
ToDo:
- forse dovrei mettere le funzioni di calcolo del
CRC32B nella libreria "zlib"
-
****************************************************/
#ifndef MRC_PNG_IMAGE
#define MRC_PNG_IMAGE
#include <iostream> // TEMPORANEO, PER IL DEBUG!!!!!!!!!!!!!!
#define CHUNK 32768 // 32 Kb
class PNGimage : public BasicImage
{
public:
using BasicImage::BasicImage; // inherit constructors (C++11)
// save and load bitmap files
void save_file(std::string fileName, int stride_bytes);
void load_file(std::string fileName);
private:
//auxiliary functions:
void write_chunk(std::ofstream& stream, std::vector<unsigned char> data, std::string type);
unsigned int crc32(unsigned char *buffer, int len);
void encode_png_line(int y, int stride_bytes, int filter_type, signed char *line_buffer);
};
// CONSTANTS =================================================================================================================================
// DEFINITIONS ================================================================================================================================
void PNGimage::save_file(std::string fileName, int stride_bytes=0) // NON SO CHE COSA SERVE stride_bytes
{
std::ofstream file(fileName, std::ios::binary);
// 1) writes file header:
unsigned char sig[8] = { 0x89,'P','N','G',0x0D,0x0A,0x1A,0x0A }; // header starting combination (8-byte signature)
write_to_stream(file, sig);
// 2) "CHUNKS":
// 2.1) Image Header "IHDR" chunk (13 data bytes total):
std::vector<unsigned char> temp;
// temp.push_back((_width &0xFF000000)>>24); temp.push_back((_width &0x00FF0000)>>16); temp.push_back((_width &0x0000FF00)>>8); temp.push_back((_width &0x000000FF)); //image Width (Big-Endian)
// temp.push_back((_height&0xFF000000)>>24); temp.push_back((_height&0x00FF0000)>>16); temp.push_back((_height&0x0000FF00)>>8); temp.push_back((_height&0x000000FF)); //image Height (Big-Endian)
append_to_vector(temp, bigEndian2vec(_width)); // image Width (Big-Endian)
append_to_vector(temp, bigEndian2vec(_height)); // image Height (Big-Endian)
temp.push_back(8); // bit depth (1 byte, values 1, 2, 4, 8, or 16)
temp.push_back(2); // color type (1 byte, values 0:grayscale, 2:RGB, 3:indexed(palette), 4:grayscale&alpha, or 6:RGBA)
temp.push_back(0); // compression method (1 byte, value 0)
temp.push_back(0); // filter method (1 byte, value 0)
temp.push_back(0); // interlace method (1 byte, values 0 "no interlace" or 1 "Adam7 interlace")
write_chunk(file, temp, "IHDR");
// 2.2) Data "IDAT" chunk:
temp.clear();
// write_chunk(file, {0x08, 0xD7, 0x63, 0xF8, 0xCF, 0xC0, 0x00, 0x00, 0x03, 0x01, 0x01, 0x00}, "IDAT"); // hardcoded red single pixel (ci sta il canale alpha all'inizio??)
/*