-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathimgkap.c
More file actions
2704 lines (2300 loc) · 76.3 KB
/
Copy pathimgkap.c
File metadata and controls
2704 lines (2300 loc) · 76.3 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
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
/*
* imgkap.c - Convert kap a file from/to a image file and kml to kap
*
* Copyright:
* 2011 M'dJ
* 2016 H.N
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As the original author is not available, please post patches and
* report issues at https://github.com/nohal/imgkap
*
* Use open source FreeImage and gnu gcc
* Thank to freeimage, libsb and opencpn
*
*/
#define VERS "1.16"
#include <stdint.h>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for strncmp() */
#include <time.h> /* for date in kap */
#include <FreeImage.h>
/* color type and mask */
typedef union
{
RGBQUAD q;
uint32_t p;
} Color32;
#define RGBMASK 0x00FFFFFF
/* --- Copy this part in header for use imgkap functions in programs and define LIBIMGKAP*/
#define METTERS 0
#define FATHOMS 1
#define FEET 2
#define NORMAL 0
#define OLDKAP 1
#define COLOR_NONE 1
#define COLOR_IMG 2
#define COLOR_MAP 3
#define COLOR_KAP 4
#define FIF_KAP 1024
#define FIF_NO1 1025
#define FIF_TXT 1026
#define FIF_KML 1027
int imgtokap(int typein,char *filein, double lat0, double lon0, int pixpos0x, int pixpos0y, double lat1, double lon1, int pixpos1x, int pixpos1y, int optkap,int color,char *title, int units, char *sd, int optionwgs84, char *optframe, char *fileout, char *gd, char *pr, int optionscale, char* edition, char* updated);
int imgheadertokap(int typein,char *filein,int typeheader,int optkap,int optrc,int color,char *title,char *fileheader,char *fileout);
int kaptoimg(int typein,char *filein,int typeheader,char *fileheader,int typeout,char *fileout,char *optionpal);
int findfiletype(char *file);
int writeimgkap(FILE *out,FIBITMAP **bitmap,int optkap,int colors,Color32 *pal,uint16_t widthin,uint16_t heightin,uint16_t widthout,uint16_t heightout);
int bsb_uncompress_row(int typein, FILE *in, uint8_t *buf_out, uint16_t bits_in,uint16_t bits_out,uint16_t width);
int bsb_compress_row(const uint8_t *buf_in, uint8_t *buf_out, uint16_t bits_out, uint16_t line, uint16_t widthin,uint16_t widthout);
/* --- End copy */
static const double WGSinvf = 298.257223563; /* WGS84 1/f */
static const double WGSexentrik = 0.081819; /* e = 1/WGSinvf; e = sqrt(2*e -e*e) ;*/
struct structlistoption
{
char const *name;
int val;
} ;
static struct structlistoption imagetype[] =
{
{"KAP",FIF_KAP},
{"NO1",FIF_NO1},
{"KML",FIF_KML},
{"TXT",FIF_TXT},
{NULL, FIF_UNKNOWN},
} ;
static struct structlistoption listoptcolor[] =
{
{"NONE",COLOR_NONE},
{"KAP",COLOR_KAP},
{"IMG",COLOR_IMG},
{"MAP",COLOR_MAP},
{NULL,COLOR_NONE}
} ;
int findoptlist(struct structlistoption *liste,char *name)
{
while (liste->name != NULL)
{
if (!strcasecmp(liste->name,name)) return liste->val;
liste++;
}
return liste->val;
}
int findfiletype(char *file)
{
char *s ;
s = file + strlen(file)-1;
while ((s > file) && (*s != '.')) s--;
s++;
return findoptlist(imagetype,s);
}
static double postod(double lat0, double lon0, double lat1, double lon1)
{
double x,v,w;
lat0 = lat0 * M_PI / 180.;
lat1 = lat1 * M_PI / 180.;
lon0 = lon0 * M_PI / 180.;
lon1 = lon1 * M_PI / 180.;
v = sin((lon0 - lon1)/2.0);
w = cos(lat0) * cos(lat1) * v * v;
x = sin((lat0 - lat1)/2.0);
return (180. * 60. / M_PI) * 2.0 * asinl(sqrtl(x*x + w));
}
static inline double lontox(double l)
{
return l*M_PI/180;
}
static inline double lattoy_WS84(double l)
{
double e = WGSexentrik;
double s = sinl(l*M_PI/180.0);
return log(tan(M_PI/4 + l * M_PI/360)*pow((1. - e * s)/(1. + e * s), e/2.));
}
/*------------------ Single Memory algorithm ------------------*/
#define MYBSIZE 1572880
typedef struct smymemory
{
struct smymemory *next;
uint32_t size;
} mymemory;
static mymemory *mymemoryfirst = 0;
static mymemory *mymemorycur = 0;
void * myalloc(int size)
{
void *s = NULL;
mymemory *mem = mymemorycur;
if (mem && ((mem->size + size) > MYBSIZE)) mem = 0;
if (!mem)
{
mem = (mymemory *)calloc(MYBSIZE,1);
if (mem == NULL) return 0;
mem->size = sizeof(mymemory);
if (mymemorycur) mymemorycur->next = mem;
mymemorycur = mem;
if (!mymemoryfirst) mymemoryfirst = mem;
}
s = ((int8_t *)mem + mem->size);
mem->size += size;
return s;
}
void myfree(void)
{
struct smymemory *mem, *next;
mem = mymemoryfirst;
while (mem)
{
next = mem->next;
free(mem);
mem = next;
}
mymemoryfirst = 0;
mymemorycur = 0;
}
/*------------------ Histogram algorithm ------------------*/
typedef struct
{
Color32 color;
uint32_t count;
int16_t num;
} helem;
typedef struct shistogram
{
Color32 color;
uint32_t count;
int16_t num;
int16_t used;
struct shistogram *child ;
} histogram;
#define HistIndex2(p,l) ((((p.q.rgbRed >> l) & 0x03) << 4) | (((p.q.rgbGreen >> l) & 0x03) << 2) | ((p.q.rgbBlue >> l) & 0x03) )
#define HistSize(l) (l?sizeof(histogram):sizeof(helem))
#define HistInc(h,l) (histogram *)(((char *)h)+HistSize(l))
#define HistIndex(h,p,l) (histogram *)((char *)h+HistSize(l)*HistIndex2(p,l))
static histogram *HistAddColor (histogram *h, Color32 pixel )
{
char level;
for (level=6;level>=0;level -=2)
{
h = HistIndex(h,pixel,level) ;
if (h->color.p == pixel.p) break;
if (!h->count && !h->num)
{
h->color.p = pixel.p;
break;
}
if (!h->child)
{
h->child = (histogram *)myalloc(HistSize(level)*64);
if (h->child == NULL) return 0;
}
h = h->child;
}
h->count++;
return h;
}
static int HistGetColorNum (histogram *h, Color32 pixel)
{
char level;
for (level=6;level>=0;level -=2)
{
/* 0 < index < 64 */
h = HistIndex(h,pixel,level) ;
if (h->color.p == pixel.p) break;
if (!level)
break; // erreur
if (!h->child) break;
h = h->child;
}
if (h->num < 0) return -1-h->num;
return h->num-1;
}
#define HistColorsCount(h) HistColorsCountLevel(h,6)
static int32_t HistColorsCountLevel (histogram *h,int level)
{
int i;
uint32_t count = 0;
for (i=0;i<64;i++)
{
if (h->count) count++;
if (level)
{
if(h->child) count += HistColorsCountLevel(h->child,level-2);
}
h = HistInc(h,level);
}
return count;
}
/*--------------- reduce begin -------------*/
typedef struct
{
histogram *h;
int32_t nbin;
int32_t nbout;
int32_t colorsin;
int32_t colorsout;
int nextcote;
int maxcote;
int limcote[8];
uint64_t count;
uint64_t red;
uint64_t green;
uint64_t blue;
} reduce;
static inline int HistDist(Color32 a, Color32 b)
{
int c,r;
c = a.q.rgbRed - b.q.rgbRed;
r = c*c;
c = a.q.rgbGreen - b.q.rgbGreen;
r += c*c;
c = a.q.rgbBlue - b.q.rgbBlue;
r += c*c;
return sqrt(r);
}
static int HistReduceDist(reduce *r, histogram *h, histogram *e, int cote, int level)
{
int i;
int used = 1;
int curcote;
int limcote = r->limcote[level];
for (i=0;i<64;i++)
{
if (h->count && !h->num)
{
curcote = HistDist((Color32)e->color,(Color32)h->color);
if (curcote <= cote)
{
uint64_t c;
c = h->count;
r->count += c;
r->red += c * (uint64_t)((Color32)h->color).q.rgbRed ;
r->green += c * (uint64_t)((Color32)h->color).q.rgbGreen;
r->blue += c * (uint64_t)((Color32)h->color).q.rgbBlue;
h->num = r->nbout;
h->count = 0;
r->nbin++;
}
else
{
if (r->nextcote > curcote)
r->nextcote = curcote;
used = 0;
}
}
if (level && h->child && !h->used)
{
limcote += cote ;
curcote = HistDist((Color32)e->color,(Color32)h->color);
if (curcote <= limcote)
h->used = HistReduceDist(r,h->child,e,cote,level-2);
if (!h->used)
{
if ((curcote > limcote) && (r->nextcote > limcote))
r->nextcote = curcote ;
used = 0;
}
limcote -= cote ;
}
h = HistInc(h,level);
}
return used;
}
static void HistReduceLevel(reduce *r, histogram *h, int level)
{
int i;
for (i=0;i<64;i++)
{
if (level && h->child && !h->used)
{
HistReduceLevel(r, h->child,level-2);
if (!r->colorsout) break;
}
if (h->count && !h->num)
{
int32_t cote = 0;
int32_t nbcolors;
int32_t curv;
r->count = r->red = r->green = r->blue = 0;
r->nbin = 0;
r->nextcote = 0;
r->nbout++;
cote = (int32_t)(pow((double)((1<<24)/(double)r->colorsout),1.0/3.0)/2); //-1;
r->maxcote = sqrt(3*cote*cote);
curv = 0;
nbcolors = (r->colorsin +r->colorsout -1)/r->colorsout;
while (r->nbin < nbcolors)
{
curv += nbcolors - r->nbin;
cote = (int32_t)(pow(curv,1.0/3.0)/2); // - 1;
cote = sqrt(3*cote*cote);
if (r->nextcote > cote)
cote = r->nextcote;
r->nextcote = r->maxcote+1;
if (cote >= r->maxcote)
break;
h->used = HistReduceDist(r,r->h,h,cote,6);
if (!r->count)
{
fprintf(stderr,"Erreur quantize\n");
return;
}
}
r->colorsin -= r->nbin;
r->colorsout--;
{
histogram *e;
Color32 pixel ;
uint64_t c,cc;
c = r->count; cc = c >> 1 ;
pixel.q.rgbRed = (uint8_t)((r->red + cc) / c);
pixel.q.rgbGreen = (uint8_t)((r->green + cc) / c);
pixel.q.rgbBlue = (uint8_t)((r->blue + cc) / c);
pixel.q.rgbReserved = 0;
e = HistAddColor(r->h,pixel);
e->count = r->count;
e->num = -r->nbout;
}
if (!r->colorsout) break;
}
h = HistInc(h,level);
}
}
static int HistReduce(histogram *h, int colorsin, int colorsout)
{
reduce r;
r.h = h;
r.nbout = 0;
if (!colorsout || !colorsin) return 0;
if (colorsout > 0x7FFF) colorsout = 0x7FFF;
if (colorsout > colorsin) colorsout = colorsin;
r.colorsin = colorsin;
r.colorsout = colorsout;
r.limcote[2] = sqrt(3*3*3) ;
r.limcote[4] = sqrt(3*15*15) ;
r.limcote[6] = sqrt(3*63*63) ;
HistReduceLevel(&r,h,6);
return r.nbout;
}
/*--------------- reduce end -------------*/
static int _HistGetList(histogram *h,helem **e,int nbcolors,char level)
{
int i;
int nb;
nb = 0;
for (i=0;i<64;i++)
{
if (h->count && (h->num < 0))
{
e[-1-h->num] = (helem *)h;
nb++;
}
if (level && h->child) nb += _HistGetList(h->child,e,nbcolors-nb,level-2);
if (nb > nbcolors)
return 0;
h = HistInc(h,level);
}
return nb;
}
static int HistGetPalette(uint8_t *colorskap,uint8_t *colors,Color32 *palette,histogram *h,int nbcolors, int nb, int optcolors, Color32 *imgpal,int maxpal)
{
int i,j;
helem *t,*e[128];
uint8_t numpal[128];
/* get colors used */
if ((i= _HistGetList(h,e,nbcolors,6)) != nbcolors)
{
fprintf(stderr, "Can't process the palette, reduce it before using imgkap.\n");
return 0;
}
/* load all color in final palette */
memset(numpal,0,sizeof(numpal));
if (!imgpal)
{
for (i=0;i<nbcolors;i++)
{
if (!(palette[i].q.rgbReserved & 1)) palette[i].p = e[i]->color.p;
palette[i].q.rgbReserved |= 1;
colors[i] = i;
numpal[i] = i;
}
palette->q.rgbReserved |= 8;
maxpal = nbcolors;
}
else
{
for (i=maxpal-1;i>=0;i--)
{
j = HistGetColorNum(h,imgpal[i]);
if (j>=0)
{
if (!(palette[i].q.rgbReserved & 1))
palette[i].p = imgpal[i].p;
palette[i].q.rgbReserved |= 1;
numpal[j] = i;
colors[i] = j;
}
}
palette->q.rgbReserved |= 8;
}
/* sort palette desc count */
for (i=0;i<nbcolors;i++)
{
for (j=i+1;j<nbcolors;j++)
if (e[j]->count > e[i]->count)
{
t = e[i];
e[i] = e[j];
e[j] = t;
}
}
/* if palkap 0 put first in last */
if (nb)
{
nb=1;
t = e[0];
e[0] = e[nbcolors-1];
e[nbcolors-1] = t;
}
/* get kap palette colors */
colorskap[0] = 0;
for (i=0;i<nbcolors;i++)
colorskap[i+nb] = numpal[-1-e[i]->num];
/* get num colors in kap palette */
for (i=0;i<maxpal;i++)
{
for (j=0;j<nbcolors;j++)
if (colors[i] == (-1 - e[j]->num))
break;
colors[i] = j+nb;
}
/* taitement img && map sur colorskap */
if ((optcolors == COLOR_IMG) || (optcolors == COLOR_MAP))
{
for(i=0;i<maxpal;i++)
{
palette[256+i].q.rgbRed = palette[i].q.rgbRed ;
palette[256+i].q.rgbGreen = palette[i].q.rgbGreen ;
palette[256+i].q.rgbBlue = palette[i].q.rgbBlue ;
palette[512+i].q.rgbRed = (palette[i].q.rgbRed)/2 ;
palette[512+i].q.rgbGreen = (palette[i].q.rgbGreen)/2 ;
palette[512+i].q.rgbBlue = (palette[i].q.rgbBlue)/2 ;
palette[768+i].q.rgbRed = (palette[i].q.rgbRed)/4 ;
palette[768+i].q.rgbGreen = (palette[i].q.rgbGreen)/4 ;
palette[768+i].q.rgbBlue = (palette[i].q.rgbBlue)/4 ;
palette[768+i].q.rgbReserved = palette[512+i].q.rgbReserved = palette[256+i].q.rgbReserved = palette[i].q.rgbReserved ;
}
if ((optcolors == COLOR_MAP) && (nbcolors < 64))
{
Color32 *p = palette+768;
for(i=0;i<maxpal;i++)
{
if ((p->q.rgbRed <= 4) && (p->q.rgbGreen <= 4) && (p->q.rgbBlue <= 4))
p->q.rgbRed = p->q.rgbGreen = p->q.rgbBlue = 55;
if ((p->q.rgbRed >= 60) && (p->q.rgbGreen >= 60) && (p->q.rgbBlue >= 60))
p->q.rgbRed = p->q.rgbGreen = p->q.rgbBlue = 0;
p++;
}
}
}
/*
for (i=0;i<nbcolors;i++)
{
printf("eorder %d rgb %d %d %d\n",i,e[i]->color.q.rgbRed,e[i]->color.q.rgbGreen,e[i]->color.q.rgbBlue);
}
for (i=0;i<maxpal;i++)
{
printf("palette %d rgb %d %d %d\n",i,palette[i].q.rgbRed,palette[i].q.rgbGreen,palette[i].q.rgbBlue);
}
for (i=0;i<nbcolors+nb;i++)
{
j = colorskap[i];
printf("palkap %d rgb %d %d %d\n",i,palette[j].q.rgbRed,palette[j].q.rgbGreen,palette[j].q.rgbBlue);
}
for (i=0;i<maxpal;i++)
{
printf("indexcol %i colors : %d\n",i,colors[i]);
}
*/
nbcolors += nb;
return nbcolors;
}
#define HistFree(h) myfree()
/*------------------ End of Histogram ------------------*/
typedef struct shsv
{
double hue;
double sat;
double val;
} HSV;
/* read in kap file */
//* si size < 0 lit jusqu'a \n (elimine \r) et convertie NO1 int r = (c - 9) & 0xFF; */
static inline int fgetkapc(int typein, FILE *in)
{
int c;
c = getc(in);
if (typein == FIF_NO1) return (c - 9) & 0xff;
return c;
}
static int fgetkaps(char *s, int size, FILE *in, int typein)
{
int i,c;
i = 0;
while (((c = getc(in)) != EOF) && size)
{
if (typein == FIF_NO1) c = (c - 9) & 0xff ;
if (size > 0)
{
s[i++] = (char)c ;
size--;
continue;
}
if (c == '\r') continue;
if (c == '\n')
{
s[i] = 0;
size++;
break;
}
s[i++] = (char)c;
size++;
if (!c) break;
}
return i;
}
/* function read and write kap index */
static int bsb_write_index(FILE *fp, uint16_t height, uint32_t *index)
{
uint8_t l;
/* Write index table */
while (height--)
{
/* Indices must be written as big-endian */
l = (*index >> 24) & 0xff;
fputc(l, fp);
l = (*index >> 16) & 0xff;
fputc(l, fp);
l = (*index >> 8) & 0xff;
fputc(l, fp);
l = *index & 0xff;
fputc(l, fp);
index++;
}
return 1;
}
static uint32_t *bsb_read_index(int typein,FILE *in,uint16_t height)
{
uint32_t l,end;
uint32_t *index;
int i;
index = NULL;
fseek(in,-4,SEEK_END);
end = ftell(in);
l = ((uint32_t)fgetkapc(typein,in)<<24)+((uint32_t)fgetkapc(typein,in)<<16)+((uint32_t)fgetkapc(typein,in)<<8)+((uint32_t)fgetkapc(typein,in));
if (((end - l)/4) != height) return NULL;
index = (uint32_t *)malloc(height*sizeof(uint32_t));
if (index == NULL) return NULL;
fseek(in,l,SEEK_SET);
/* Read index table */
for (i=0; i < height; i++)
{
index[i] = ((uint32_t)fgetkapc(typein,in)<<24)+((uint32_t)fgetkapc(typein,in)<<16)+((uint32_t)fgetkapc(typein,in)<<8)+((uint32_t)fgetkapc(typein,in));
}
return index;
}
/* bsb compress number, not value 0 at first write */
static uint16_t bsb_compress_nb(uint8_t *p, uint16_t nb, uint8_t pixel, uint16_t max)
{
uint16_t count = 0;
if (nb > max)
{
count = bsb_compress_nb(p,nb>>7,pixel|0x80,max);
p[count++] = (nb & 0x7F) | (pixel & 0x80);
return count;
}
pixel |= nb ;
if (!pixel) p[count++] = 0x80;
p[count++] = pixel ;
return count;
}
/* write line bsb */
int bsb_compress_row(const uint8_t *buf_in, uint8_t *buf_out, uint16_t bits_out, uint16_t line, uint16_t widthin, uint16_t widthout)
{
uint16_t ibuf,run_length ;
uint16_t ipixelin,ipixelout,xout;
uint8_t last_pix;
uint16_t dec, max;
dec = 7-bits_out;
max = (1<<dec) -1;
/* write the line number */
ibuf = bsb_compress_nb(buf_out,line,0,0x7F);
ipixelin = ipixelout = 0;
while ( ipixelin < widthin )
{
last_pix = buf_in[ipixelin];
ipixelin++;
ipixelout++;
/* Count length of same pixel */
run_length = 0;
if (ipixelin == 1592)
ipixelin = 1592;
while ( (ipixelin < widthin) && (buf_in[ipixelin] == last_pix) )
{
ipixelin++;
ipixelout++;
run_length++;
}
/* Extend, like but faster (total time/2) than xout = round((double)ipixelin*widthout/widthin); */
xout = ((uint32_t)((ipixelin<<1)+1)*widthout)/((uint32_t)widthin<<1);
if (xout > ipixelout)
{
run_length += xout - ipixelout;
ipixelout = xout;
}
/* write pixel*/
ibuf += bsb_compress_nb(buf_out+ibuf,run_length,last_pix<<dec,max);
}
buf_out[ibuf++] = 0;
return ibuf;
}
/* bsb uncompress number */
static uint16_t bsb_uncompress_nb(int typein,FILE *in, uint8_t *pixel, uint8_t decin, uint8_t maxin)
{
uint8_t c;
uint16_t count;
c = fgetkapc(typein,in);
count = (c & 0x7f);
*pixel = count >> decin;
count &= maxin;
while (c & 0x80)
{
c = fgetkapc(typein,in);
count = (count << 7) + (c & 0x7f);
}
return count+1;
}
/* read line bsb */
int bsb_uncompress_row(int typein, FILE *in, uint8_t *buf_out, uint16_t bits_in,uint16_t bits_out, uint16_t width)
{
uint16_t count;
uint8_t pixel;
uint8_t decin, maxin;
uint16_t xout = 0;
decin = 7-bits_in;
maxin = (1<<decin) - 1;
/* read the line number */
count = bsb_uncompress_nb(typein, in,&pixel,0,0x7F);
/* no test count = line number */
switch (bits_out)
{
case 1:
while ( width )
{
count = bsb_uncompress_nb(typein,in,&pixel, decin,maxin);
if (count > width) count = width;
width -= count;
while (count)
{
buf_out[xout>>3] |= pixel<<(7-(xout&0x7));
xout++;
count--;
}
}
break;
case 4:
while ( width )
{
count = bsb_uncompress_nb(typein,in,&pixel, decin,maxin);
if (count > width) count = width;
width -= count;
while (count)
{
buf_out[xout>>1] |= pixel<<(4-((xout&1)<<2));
xout++;
count--;
}
}
break;
case 8:
while ( width )
{
count = bsb_uncompress_nb(typein,in,&pixel, decin,maxin);
if (count > width) count = width;
width -= count;
while (count)
{
*buf_out++ = pixel;
count--;
}
}
break;
}
/* read last byte (0) */
getc(in);
return 0;
}
static void read_line(uint8_t *in, uint16_t bits, int width, uint8_t *colors, histogram *hist, uint8_t *out)
{
int i;
uint8_t c = 0;
switch (bits)
{
case 1:
for (i=0;i<width;i++)
{
out[i] = colors[(in[i>>3] >> (7-(i & 0x7))) & 1];
}
return;
case 4:
for (i=0;i<width;i++)
{
c = in[i >> 1];
out[i++] = colors[(c>>4) & 0xF];
out[i] = colors[c & 0xF];
}
return;
case 8:
for (i=0;i<width;i++)
{
out[i] = colors[in[i]];
}
return;
case 24:
{
Color32 cur, last;
last.p = 0xFFFFFFFF;
for (i=0;i<width;i++)
{
cur.p = ( *(uint32_t *)in & RGBMASK);
if (last.p != cur.p)
{
c = colors[HistGetColorNum(hist, cur)];
last = cur;
}
out[i] = c;
in += 3;
}
}
}
}
static uint32_t GetHistogram(FIBITMAP *bitmap,uint32_t bits,uint16_t width,uint16_t height,Color32 *pal,histogram *hist)
{
uint32_t i,j;
Color32 cur;
uint8_t *line,k;
histogram *h = hist;
switch (bits)
{
case 1:
HistAddColor (hist, pal[0]);
h = HistAddColor (hist, pal[1]);
h->count++;
break;
case 4:
for (i=0;i<height;i++)
{
line = FreeImage_GetScanLine(bitmap, i);
cur.p = (width+1)>>1;
for (j=0;j<cur.p;j++)
{
k = (*line++)>>4;
if (h->color.p == pal[k].p)
{
h->count++;
continue;
}
h = HistAddColor (hist, pal[k]);
}
line = FreeImage_GetScanLine(bitmap, i);