-
Notifications
You must be signed in to change notification settings - Fork 0
/
xwd.cc
1927 lines (1677 loc) · 57.2 KB
/
xwd.cc
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
/* xwd.cc - class to represent XWD images */
static const char *dxwd_cc_RCSID="$Id: xwd.cc,v 1.13 2019/03/01 23:15:23 dkl Exp $";
#define c_plusplus
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <dirent.h>
#include "zlib.h"
#include "xwd.h"
#include "font.h"
#include "ppm.h"
#include "rgb.h"
unsigned long swaptest = 1;
//
// Constructor
//
xwd::xwd (const char *in_name) {
image = NULL; colors = NULL; ncolors = 0; header = NULL;
name = new char [strlen(in_name)+1];
strcpy (name, in_name);
font = new char [strlen(DEFAULT_FONT)+1];
strcpy (font, DEFAULT_FONT);
fontPath = new char [strlen(DEFAULT_FONT_PATH)+1];
strcpy (fontPath, DEFAULT_FONT_PATH);
fontDPI = new char[strlen(DEFAULT_FONT_DPI)+1];
sprintf (fontDPI, "%s", DEFAULT_FONT_DPI);
family = NULL;
pt = NULL;
style = NULL;
ReadXWD(0);
}
//
// Constructor with no name
//
xwd::xwd () {
image = NULL; colors = NULL; ncolors = 0; header = NULL;
name = new char [strlen(NEW_IMAGE_NAME)+4+1];
sprintf (name, "%s.xwd", NEW_IMAGE_NAME);
font = new char [strlen(DEFAULT_FONT)+1];
strcpy (font, DEFAULT_FONT);
fontPath = new char [strlen(DEFAULT_FONT_PATH)+1];
strcpy (fontPath, DEFAULT_FONT_PATH);
fontDPI = new char[strlen(DEFAULT_FONT_DPI)+1];
sprintf (fontDPI, "%s", DEFAULT_FONT_DPI);
family = NULL;
pt = NULL;
style = NULL;
// Since we are not going to read it in, initialize it to
// having two colors and being 8x8 and fill it with white
DontReadXWD();
}
//
// Destructor
//
xwd::~xwd () {
if (font) delete font;
if (pt) delete pt;
if (family) delete family;
if (fontDPI) delete fontDPI;
if (fontPath) delete fontPath;
if (colors) delete colors;
if (header) delete header;
if (image) if (image->data) delete image->data;
if (image) delete image;
if (name) delete name;
}
//
// SetName sets the name
//
void xwd::SetName (char *newname) {
if (!newname) return;
if (name) delete name;
name = new char [strlen(newname)+1];
strcpy (name, newname);
}
// SetPath sets the path
void xwd::SetPath (char *newpath) {
if (!newpath) return;
if (path) delete path;
path = new char [strlen(newpath)+1];
strcpy (path, newpath);
}
// SetFont sets the font
void xwd::SetFont (char *newfont) {
if (!newfont) return;
if (font) delete font;
font = new char [strlen(newfont)+1];
strcpy (font, newfont);
}
// SetFontPath sets the path to the fonts
void xwd::SetFontPath (char *newfontpath) {
if (!newfontpath) return;
if (fontPath) delete fontPath;
fontPath = new char [strlen(newfontpath)+1];
strcpy (fontPath, newfontpath);
}
// SetFontDPI sets the fontDPI
void xwd::SetFontDPI (char *newdpi) {
if (!newdpi) return;
if (fontDPI) delete fontDPI;
fontDPI = new char [strlen(newdpi)+1];
strcpy (fontDPI, newdpi);
}
// SetFontFamily sets the font family
void xwd::SetFontFamily (char *newfam) {
if (!newfam) return;
if (family) delete family;
family = new char [strlen(newfam)+1];
strcpy (family, newfam);
if (!strcasecmp(family,"courier")) sprintf (family, "%s", "cour");
if (!strcasecmp(family,"character")) sprintf (family, "%s", "char");
if (!strcasecmp(family,"lucida")) sprintf (family, "%s", "lu");
if (!strcasecmp(family,"helvetica")) sprintf (family, "%s", "helv");
if ((!strcasecmp(family,"lucidabright")) ||
(!strcasecmp(family,"lucida bright"))) sprintf (family, "%s", "lub");
if (!strcasecmp(family,"newcentury") ||
!strcasecmp(family,"new century ") ||
!strcasecmp(family,"new century")) sprintf (family, "%s", "ncen");
if (!strcasecmp(family,"times")) sprintf (family, "%s", "tim");
if (family && style && pt) {
if (font) delete font;
font = new char[strlen(family)+strlen(style)+strlen(pt)+1];
sprintf (font, "%s%s%s.bdf", family, style, pt);
}
}
// SetFontPT sets the font point size
void xwd::SetFontPT (char *newpt) {
if (!newpt) return;
if (pt) delete pt;
pt = new char [strlen(newpt)+1];
strcpy (pt, newpt);
if (family && style && pt) {
if (font) delete font;
font = new char[strlen(family)+strlen(style)+strlen(pt)+1];
sprintf (font, "%s%s%s.bdf", family, style, pt);
}
}
// SetFontStyle sets the font style
void xwd::SetFontStyle (char *newStyle) {
if (!newStyle) return;
if (style) delete style;
style = new char [strlen(newStyle)+1];
strcpy (style, newStyle);
if (family && style && pt) {
if (font) delete font;
font = new char[strlen(family)+strlen(style)+strlen(pt)+1];
sprintf (font, "%s%s%s.bdf", family, style, pt);
}
}
//
// Recursively list all the files in the tree with specified root
//
int xwd::ListPath (char *path) {
//static int depth=0;
static int depth=-1, indent=0;
struct dirent *de; // Pointer for directory entry
depth++;
//indent = 2 * (depth-1);
//indent = (depth) * 2;
indent = depth*2;
if (debug) printf("in ListPath, depth=%d, indent=%d\n", depth, indent);
// opendir() returns a pointer of DIR type.
DIR *dr = opendir(path);
// opendir returns NULL if couldn't open directory
if (dr == NULL) {
fprintf(stderr,"Error: Could not open font path %s\n", fontPath );
depth--;
indent=(depth)*2;
return 1;
}
while ((de = readdir(dr)) != NULL) {
if (strcmp(de->d_name,".") && strcmp(de->d_name,"..")) {
int newlen = strlen(de->d_name)+strlen(fontPath)+2;
char *newname = new char [strlen(de->d_name)+strlen(fontPath)+2];
sprintf (newname, "%s/%s", path,de->d_name);
// is it a directory or a file? Try opening it.
if ( DIR *dr2 = opendir(newname)) {
// yes it did. close it now that it's open, and recurse into it.
closedir(dr2);
// tired of wrestling with printf
if (verbose) for (int i=0;i<indent;i++) { printf (" "); }
if (verbose) printf("%s:\n", de->d_name);
if (debug) if (verbose)
printf("%d %d %*s:\n", depth, indent, indent, de->d_name);
ListPath(newname);
} else {
if (strstr(de->d_name,".bdf")) {
if (verbose) for (int i=0;i<indent;i++) { printf (" "); }
if (verbose) {
printf("%s", de->d_name);
} else{
char *shortname = new char [strlen(de->d_name)+1];
strcpy(shortname,de->d_name);
shortname [strlen(de->d_name)-4] = '\0';
printf("%s", shortname);
delete [] shortname;
}
printf("\n" );
}
if (debug) if (verbose)
printf("%d %d %*s\n", depth, indent, indent, de->d_name);
}
delete [] newname;
}
}
closedir(dr);
depth--;
indent=(depth)*2;
return 0;
}
// ShowFonts lists the fonts found at $fontpath
int xwd::ShowFonts () {
if (fontPath)
ListPath(fontPath);
else
ListPath((char *)"");
return 0;
}
//
// GetClosestColor takes in rgb values from 0 - 255
//
int xwd::GetClosestColor (int r, int g, int b) {
int i, j, closest=0;
double best=0.0;
// X uses 16 bits for color
r *= 255; g *= 255; b *= 255;
for (i=0; i<ncolors; i++)
if ((colors[i].red==r) && (colors[i].green==g) && (colors[i].blue==b))
return i;
for (i=0; i<ncolors; i++) {
double redDistance = (double)(abs(colors[i].red - r));
double greenDistance = (double)(abs(colors[i].green - g));
double blueDistance = (double)(abs(colors[i].blue - b));
double redGreenDistance =
sqrt((redDistance * redDistance) * (greenDistance * greenDistance)); double distance =
sqrt ((blueDistance * blueDistance) +
(redGreenDistance * redGreenDistance));
if (i == 0) { best = distance; closest = i; }
else if (distance < best) { best = distance; closest = i; }
}
return closest;
}
//
// Dump the contents of the cmap
//
void xwd::DumpCmap (int cells) {
int i, j, closest, distance;
printf ("Number of colors: %d whitePixel: %d blackPixel: %d\n",
ncolors, (int)whitePixel, (int)blackPixel);
printf ("\t16b\t16b\t16b\tR-MSB\tR-LSB\tG-MSB\tG-LSB\tB-MSB\tB-LSB\n" );
for (i=0; i< min(cells,ncolors); i++)
printf ("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", i,
//colors[i], colors[i+16], colors[i+32], colors[i+48],
colors[i].red, colors[i].green, colors[i].blue,
colors[i].red/256, colors[i].red%256, colors[i].green/256, colors[i].green%256,
colors[i].blue / 256, colors[i].blue % 256);
}
//
// takes in rgb values from 0 - 255
//
int xwd::GetFarthestColor (int r, int g, int b) {
int i, j, farthest, distance;
// X uses 16 bits for color
r *= 255; g *= 255; b *= 255;
farthest = 0; distance = 0;
for (i=0; i<ncolors; i++) {
int d = abs(colors[i].red - r) + abs(colors[i].green - g) +
abs(colors[i].blue - b);
if (d > distance) { distance = d; farthest = i; }
}
return farthest;
}
//
// mapTo remaps a xwd's cmap to that of toMe as necessary
//
void xwd::MapTo (xwd *toMe) {
unsigned char *map;
XColor *newColors;
double *distances;
int i, j, closest, needed=0;
int *mappings;
if (debug)
printf ("%s: cmap has %d cells, target has %d cells\n", name, ncolors, toMe->ncolors);
distances = new double [toMe->ncolors];
mappings = new int [ncolors];
// does anything need to be done at all?
if (toMe->ncolors < ncolors) needed = 1; // it does if one is smaller
else for (i=0; i<ncolors; i++) // or if they don't match %100
if ((colors[i].red != toMe->colors[i].red) ||
(colors[i].green != toMe->colors[i].green) ||
(colors[i].blue != toMe->colors[i].blue))
needed=1;
if (needed) {
// for each color in the old cmap, find the closest match in the new
for (i=0; i<ncolors; i++) {
closest = 0;
for (j=0; j<min(toMe->ncolors,16); j++) {
//for (j=0; j<toMe->ncolors; j++) {
distances[j] = abs(colors[i].red - toMe->colors[j].red) +
abs(colors[i].green - toMe->colors[j].green) +
abs(colors[i].blue - toMe->colors[j].blue);
/*
distances[j] = sqrt
((abs(colors[i].red - toMe->colors[j].red) * abs(colors[i].red - toMe->colors[j].red)) +
(abs(colors[i].green - toMe->colors[j].green) * abs(colors[i].green - toMe->colors[j].green)) +
(abs(colors[i].blue - toMe->colors[j].blue) * abs(colors[i].blue - toMe->colors[j].blue)) );
*/
if (distances[j] < distances[closest]) closest = j;
}
mappings[i] = closest;
if (verbose)
fprintf (stderr,"%s: mapping cell %d to %d\n", name, i, closest);
}
// remap each pixel
for (int x=0; x < image->width; x++) {
for (int y=0; y < image->height; y++) {
Pixel pix;
pix = GetPixel(x, y);
if (pix != (Pixel)mappings[(unsigned char)pix]) {
PutPixel (x, y, (Pixel)mappings[(unsigned char)pix]);
}
}
}
newColors = new XColor [toMe->ncolors];
memcpy (newColors, toMe->colors, toMe->ncolors * sizeof(XColor));
delete colors;
colors = newColors;
header->ncolors = ncolors = toMe->ncolors;
// now must recalibrate whitePixel and blackPixel
whitePixel = toMe->whitePixel; blackPixel = toMe->blackPixel;
/*
whitePixel = 0; blackPixel = 1;
for (i = ncolors-1; i >= 0; i--) {
if ((colors[i].red==0) && (colors[i].green==0) && (colors[i].blue==0))
blackPixel = i;
//if ((colors[i].red/256==256) && (colors[i].green/256==256) && (colors[i].blue/256==256))
//whitePixel = i;
if ((colors[i].red==65535) && (colors[i].green==65535) && (colors[i].blue==65535))
whitePixel = i;
if ((colors[i].red==65280) && (colors[i].green==65280) && (colors[i].blue==65280))
whitePixel = i;
if (debug)fprintf(stderr,"ReadXWD: whitePixel=%d, blackPixel=%d\n",
whitePixel,blackPixel);
}
*/
}
fg = blackPixel; bg = whitePixel;
delete mappings;
delete distances;
}
//
// HasColor returns 1 if the color exists in the colormap or else 0
//
int xwd::HasColor (XColor *hasMe) {
XColor *newcolors;
int i;
for (i=0; i<ncolors; i++)
if ((colors[i].red == hasMe->red) &&
(colors[i].green == hasMe->green) &&
(colors[i].blue == hasMe->blue))
return 1;
return 0;
}
// GetColor returns the pixel value of the desired color
// or ncolors if it does not exist in the colormap
Pixel xwd::GetColor (int r, int g, int b) {
int n=0, found=0;
while ((n<ncolors) && (!found)) {
found = ((colors[n].red==r) &&
(colors[n].green==g) &&
(colors[n].blue==b));
n++;
}
// return Pixel value or -1 if not found
return found ? n : -1;
}
// GetColor returns the pixel value of the desired color
// or ncolors if it does not exist in the colormap
Pixel xwd::GetColor (char *colorname) {
int n=0, found=0;
int r,g,b;
//GetRGB (colorname, &r, &g, &b);
while ((n<ncolors) && (!found)) {
found = ((colors[n].red==r*256+r) &&
(colors[n].green==g*256+g) &&
(colors[n].blue==(b<<8)+3));
if (!found) n++;
}
return found ? n : -1;
}
// ReadRGB() reads r, g, and b values from rgb.txt
// returns 0 for success, -1 for error
int xwd::ReadRGB (char *ofMe, int *r, int *g, int *b) {
char buf[512], color[1024], c1[1024], c2[1024];
FILE *rgb;
int w;
if ((rgb = fopen("rgb.txt", "rb"))) {
while ((w=fscanf (rgb, "%[^\n]\n", buf))==1) {
if (sscanf(buf, "%d %d %d\t\t%s %s\n", r, g, b, c1, c2)==5)
continue;
if (sscanf(buf, "%d %d %d\t\t%s\n", r, g, b, color)==4) {
if (strcmp(ofMe, color)==0) {
fclose(rgb);
return 0;
}
}
}
fclose(rgb);
}
return -1;
}
// numRGB returns the enumber of RGB specifications in our table
int xwd::NumRGBs () {
//if (verbose) fprintf (stderr, "number of rgbs = %d / %d \n", sizeof(rgbs) , sizeof(rgbSpec));
return sizeof(rgbs) / sizeof(rgbSpec);
}
// FindRGBByName finds an RGB spec in the table
// and returns 0 for success, -1 for error
int xwd::FindRGBByName (char *ofMe) {
FILE *rgb;
int i=0,numrgbs;
Bool found=False;
numrgbs = NumRGBs();
i=0; while ((i<numrgbs) && strcasecmp(ofMe, rgbs[i].name)) {
i++;
}
return (i<numrgbs) ? i : -1;
}
// lsColors
int xwd::lsColors () {
int i=0;
for (i=0; i<NumRGBs(); i++) {
printf ("%3d %3d %3d\t%s\n", rgbs[i].r,rgbs[i].g,rgbs[i].b,rgbs[i].name);
}
return NumRGBs();
}
// AddColor attempts to add the named color to the colormap
// and returns the number of colors added
int xwd::AddColor (char *addMe) {
int i=0;
Bool found=False;
int numrgbs = NumRGBs();
while (strcasecmp(rgbs[i].name, addMe)!=0) {
i++;
}
if (i<NumRGBs())
return AddColor(rgbs[i].r, rgbs[i].g, rgbs[i].b);
else
return 0;
}
// addColor adds a color to the colormap & returns the number of colors added
int xwd::AddColor (int r, int g, int b) {
XColor nc;
if (ncolors == 255) return 0;
nc.pixel=(Pixel)ncolors;
nc.red=(r*256)+r;
nc.green=(g*256)+g;
nc.blue=(b*256)+b;
nc.flags=DoRed|DoGreen|DoBlue;
return (AddColor(&nc));
}
// addColor adds a color to the colormap & returns the number of colors added
int xwd::AddColor (XColor *addMe) {
XColor *newcolors;
if (ncolors == 255) return 0;
newcolors = new XColor [ncolors+1];
memcpy (newcolors, colors, ncolors * sizeof(XColor));
delete colors;
colors = newcolors;
memcpy (newcolors+ncolors, addMe, sizeof(XColor));
ncolors++;
return 1;
}
// MergeCmap merges the cmap into that of intoMe
void xwd::MergeCmap (xwd *intoMe) {
int i;
for (i=0; i<ncolors; i++)
if (! intoMe->HasColor(colors+i)) {
intoMe->AddColor(colors+i);
if (debug)
printf ("MergeCmap: merging color %d from %s\n",i,intoMe->name);
}
}
// uniq returns the number of uniq colors
int xwd::Uniq () {
int i, j, numdups=0, unique=0;
unsigned char *map;
map = new unsigned char [ncolors];
for (i=0; i<ncolors; i++) map[i]=0;
for (i=0; i<ncolors; i++) {
for (j=i+1; j<ncolors; j++) {
if (map[j] != 99)
if ((colors[i].red == colors[j].red) &&
(colors[i].green == colors[j].green) &&
(colors[i].blue == colors[j].blue)) {
numdups++;
map[j]=99; // so we don't count it twice
}
}
}
unique = ncolors - numdups;
return unique;
}
// SetWhitePixel sets the white pixel to the given value
// returns 0 for success and -1 for error
int xwd::SetWhitePixel (Pixel pix) {
if (pix >= ncolors) return -1;
whitePixel = pix;
colors[whitePixel].red=colors[whitePixel].green=colors[whitePixel].blue=65535;
return 0;
}
//
// blackPixel sets the blackPixel to the given value
//
int xwd::SetBlackPixel (Pixel pix) {
if (pix >= ncolors) return -1;
blackPixel = pix;
colors[blackPixel].red=colors[blackPixel].green=colors[blackPixel].blue=0;
return 0;
}
//
// SwapBW()
//
// SwapBW swaps BlackPixel and whitePixel and remaps the pixels
//
// Usually blackPixel is 0 and whitePixel is 1. If an image comes
// along that has white at 0 and black at 1, it might best to straighten
// it out before going any further with it
//
int xwd::SwapBW () {
register int x, y;
Pixel oldWhitePixel = whitePixel;
Pixel oldBlackPixel = blackPixel;
SetBlackPixel(oldWhitePixel);
SetWhitePixel(oldBlackPixel);
for (y=0; y < image->height ; y++) {
for (x = 0; x < image->width; x++) {
if (GetPixel(x,y) == (Pixel) oldWhitePixel) PutPixel(x,y,whitePixel);
else if (GetPixel(x,y) == (Pixel) oldBlackPixel) PutPixel(x,y,blackPixel);
}
}
return 0;
}
//
// SetForeground sets the foreround to the given pixel value
//
int xwd::SetForeground (Pixel pix) {
if (pix >= ncolors) return -1;
fg = pix; return 0;
}
// SetForeground sets the foreround to the named color,
// adding that color to the colormap only if necessary
int xwd::SetForeground (char *colorname) {
Pixel p = GetColor(colorname);
if (p < ncolors) { fg = p; return 0; }
if (AddColor(colorname) != -1)
p = GetColor(colorname);
if (p < ncolors) { fg = p; return 0; }
else return 1;
}
// SetBackground sets the backround to the given pixel value
int xwd::SetBackground (Pixel pix) {
if (pix >= ncolors) return -1;
bg = pix; return 0;
}
//
// TruncCmap()
//
// TruncCmap truncates the cmap to the specified # of cells
//
int xwd::TruncCmap (int new_ncolors) {
int i, j, k, numdups, unique_colors;
Bool mapped;
XColor *newcolors;
// if the number to trunc to is not smaller than the number
// of colors in the image, do nothing and return error
if (new_ncolors >= ncolors) return 1;
if (new_ncolors == 0) {
// an image with 0 colors? Ok we will try
if (colors) delete colors;
colors = NULL; // No Colors!!!
ncolors = header->ncolors = 0;
}
// it is > zero and < the current number of colors in the image
if (new_ncolors > 0) {
XColor *newColors = new XColor [new_ncolors];
memset (newColors, 0, new_ncolors * sizeof(XColor));
memcpy (newColors, colors, new_ncolors * sizeof(XColor));
delete colors;
colors = newColors;
ncolors = header->ncolors = new_ncolors;
}
return 0;
}
// squishCmap squishes out duplicate cells from the cmap
int xwd::SquishCmap () {
int i, j, k, numdups, unique_colors;
Bool mapped;
XColor *newcolors;
unsigned char *map;
if (debug) {
printf ("squishCmap: before: \n");
for (i=0; i<ncolors; i++)
printf ("\tcell %d = %d, %d, %d, %d\n", i, colors[i].pixel,
colors[i].red, colors[i].green, colors[i].blue);
}
/* create a mapping of cells to duplicate cells for image 1
and mark unused cells by setting values to 99 */
map = new unsigned char [ncolors];
for (i=0; i < ncolors; i++) {
map[i] = (unsigned char) i; /* initialize to straight mapping */
for (mapped=False,j=0; (mapped==False) && (j<i); j++) {
if ((colors[i].red == colors[j].red) &&
(colors[i].green == colors[j].green) &&
(colors[i].blue == colors[j].blue)) {
map[i] = (unsigned char) j; mapped=True;
if (debug) printf("cell %d in mapped to cell %d\n",i,j);
colors[i].red = 999;
colors[i].green = 999;
colors[i].blue = 999;
}
}
}
// count duplicate cells
for (numdups=0, i=1; i<ncolors; i++)
if (colors[i].red == 999 && colors[i].red == 999 && colors[i].red == 999)
numdups++;
unique_colors = ncolors - numdups;
newcolors = new XColor [unique_colors];
// copy contents of cells which are not duplicates into new map
for (i=0,k=0; i<ncolors; i++) {
if ((colors[i].red != 999) ||
(colors[i].green != 999) ||
(colors[i].blue != 999)) {
newcolors[k].red = colors[i].red;
newcolors[k].green = colors[i].green;
newcolors[k].blue = colors[i].blue;
newcolors[k].flags = colors[i].flags;
newcolors[k].pixel = k;
map[i] = (unsigned char) k;
k++;
} else // it was previously mapped above
map[i] = map[map[i]];
}
delete colors;
colors = newcolors;
header->ncolors = ncolors = unique_colors;
if (debug) {
printf ("squishCmap: after: \n");
for (i=0; i<ncolors; i++)
printf ("\tcell %d = %d, %d, %d, %d, %d\n",i,colors[i].pixel, map[i],
colors[i].red, colors[i].green, colors[i].blue);
}
if (numdups) {
for (int x=0; x < image->width; x++) {
for (int y=0; y < image->height; y++) {
unsigned long pix;
pix = GetPixel(x, y);
PutPixel (x, y, (Pixel) map[pix]);
if (debug)
if (debug) printf("resetting pix value from %d to %d\n", pix, map[pix]);
}
}
}
return 0;
}
int xwd::DrawLines (char *linesName) {
typedef struct lineStruct {
int x0, y0, x1, y1;
struct lineStruct *next;
} lineType;
int numLines = 0, lineNo, l;
char s[256];
lineType *theLines;
FILE *lines;
/* open file to get number of lines */
if (!(lines = fopen(linesName, "rb"))) {
fprintf (stderr, "Error opening lines file\n"); return(1);
}
while (fgets (s, 256, lines)) numLines++;
fclose(lines);
/* malloc the storage for the endpoints */
if ((theLines = new lineType [numLines]) == NULL) {
fprintf (stderr, "Can't malloc lines buffer."); return(1);
}
if (!(lines = fopen(linesName, "rb"))) {
fprintf (stderr, "Error opening lines file\n"); return(1);
}
/* read in $numLines lines */
lineNo = 0;
/*while (lineNo < numLines) {
fgets (s, 256, lines);
if (sscanf (s, "Line #%d from ( %d, %d) to (%d, %d)\n", &l,
&theLines[lineNo].x0, &theLines[lineNo].y0,
&theLines[lineNo].x1, &theLines[lineNo].y1) == 5 ) {
lineNo++;
}
} */
while (sscanf (s, "Line #%d from ( %d, %d) to (%d, %d)\n", &l,
&theLines[lineNo].x0, &theLines[lineNo].y0,
&theLines[lineNo].x1, &theLines[lineNo].y1) == 5 ) {
lineNo++;
}
fclose(lines);
/* draw the lines */
for (lineNo = 0; lineNo < numLines; lineNo++) {
fprintf (stderr, "Line %d: %d,%d to %d,%d\n", lineNo,
theLines[lineNo].x0, theLines[lineNo].y0,
theLines[lineNo].x1, theLines[lineNo].y1);
DrawLine (theLines[lineNo].x0, theLines[lineNo].y0,
theLines[lineNo].x1, theLines[lineNo].y1);
}
return 0;
}
//
// Clear fills the entire image with black
//
void xwd::Clear () {
register int x, y;
for (y=0; y < image->height; y++) {
for (x = 0; x < image->width; x++) {
image->data[y*image->width+x] = blackPixel;
}
}
}
//
// Set fills the entire image with white
//
void xwd::Set () {
register int x, y;
for (y=0; y < image->height; y++) {
for (x = 0; x < image->width; x++) {
image->data[y*image->width+x] = whitePixel;
}
}
}
// Fill fills the specified rect with the foreground color
int xwd::Fill (int x1, int y1, int x2, int y2) {
register int x, y;
// first, put the corners in the correct order
// x1, x2 is the northwest corner, x2, y2 is the southeast
if (x1 > x2) { int swap = x1; x1 = x2; x2 = swap; }
if (y1 > y2) { int swap = y1; y1 = y2; y2 = swap; }
// check bounds
x1 = max(x1,0); x2 = min(x2,image->width);
y1 = max(y1,0); y2 = min(y2,image->height);
for (y=y1; y <= y2 ; y++) {
for (x = x1; x <= x2; x++) {
image->data[y*image->width+x] = fg;
}
}
return 1; // TODO: use or discard this
}
// Crop clips the image to the specified rect
int xwd::Crop (int x1, int y1, int x2, int y2) {
unsigned long pix;
int newWidth, newHeight;
register int x, y;
char *newdata;
// first, put the corners in the correct order
// x1, x2 is the northwest corner, x2, y2 is the southeast
//if (x1 > x2) { int swap = x1; x1 = x2; x2 = swap; }
//if (y1 > y2) { int swap = y1; y1 = y2; y2 = swap; }
// if x1 == image->width, this comes from TrimLeft(); nothing to trim
if (x1 > image->width-1) return 1;
// if x2 == -1, this probably comes from TrimRight(); nothing to trim
if (x2 < 0) return 1;
// if y1 == image->height, this comes from TrimTop(); nothing to trim
if (y1 > image->height-1) return 1;
// if y2 == -1, this comes from TrimBottom(); nothing to trim
if (y2 < 0) return 1;
// these out-of-bounds cases should probably also be ignored
if (x1<0) return 1;
if (x2>image->width-1) return 1;
if (y1<0) return 1;
if (y2>image->height-1) return 1;
// set up distances. Add one to operate inclusively
newWidth = (x2 - x1) + 1; newHeight = (y2 - y1) + 1;
//newimg = new XImage;
newdata = new char [newWidth * newHeight];
memset (newdata, 0, newWidth * newHeight);
for (y=0; y < newHeight ; y++) {
for (x = 0; x < newWidth; x++) {
pix = GetPixel (x + x1, y + y1);
newdata[(y*newWidth)+x] = pix;
}
}
image->width = newWidth;
image->height = newHeight;
image->bytes_per_line = newWidth;
header->pixmap_width = (CARD32) newWidth;
header->pixmap_height = (CARD32) newHeight;
header->window_width = (CARD32) newWidth;
header->window_height = (CARD32) newHeight;
header->bytes_per_line = (CARD32) newWidth;
delete image->data;
image->data = newdata;
return 0;
}
//
// Halve halves the width and height of the image
//
int xwd::Halve () {
char *newdata;
register int x, y;
int half_w = (image->width +1) / 2;
int half_h = (image->height +1) / 2;
for (y=0; y < image->height; y+=2) {
for (x = 0; x < image->width; x+=2) {
int cur = (y * image->width) + x; // current position
// we are halving the image, so we could do it by sampling,
// and just taking one out of every four pixels. But it is grainy.
// Look for the darkes of the four instewad and use that.
Pixel pel[4];
// the four pixels are this one, the one to thr right,
// the one below and the one below and to the right.
pel[0] = GetPixel (x,y);
pel[1] = GetPixel (x+1,y);
pel[3] = GetPixel (x, y+1);
pel[4] = GetPixel (x+1, y+1);
// We now have four pixel values. Find the closest to black.
// Black is (0, 0, 0) so the smallest is closest.
for (int i = 1; i < 4; i++) {
unsigned short r, g, b;
r=colors[pel[0]].red;g=colors[pel[0]].green;b=colors[pel[0]].blue;
int dist = (int) sqrt((r*r) + (b*b) + (g*g));
r=colors[pel[i]].red, g=colors[pel[i]].green; b=colors[pel[i]].blue;
int dist2 = (int) sqrt((r*r) + (b*b) + (g*g));
if ( dist2 < dist ) {
pel[0] = pel[i];
}
}
image->data[cur]=(char)pel[0];
}
}
image->width = half_w;
image->height = half_h;
image->bytes_per_line = half_w;
header->pixmap_width = (CARD32) half_w;
header->pixmap_height = (CARD32) half_h;
header->window_width = (CARD32) half_w;
header->window_height = (CARD32) half_h;
header->bytes_per_line = (CARD32) half_w;
delete image->data;
image->data = newdata;
return 0;
}
//
// Sample halves the width and height of the image
//
int xwd::Sample () {
register int x, y;
char *newdata, *newPtr;
int half_w = (image->width +1) / 2;
int half_h = (image->height +1) / 2;
newPtr = newdata = new char[half_w * half_h];
memset (newdata, 0, half_w * half_h);