-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrscope.c
1397 lines (1150 loc) · 34 KB
/
rscope.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
// ResampleScope
// Copyright (C) 2011-2017 Jason Summers
// 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/>.
#ifdef _WIN32
#define RS_WINDOWS
#endif
#ifdef RS_WINDOWS
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#endif
#ifdef RS_WINDOWS
#include <windows.h>
#include <io.h> // For _setmode
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef RS_WINDOWS
#define BGDWIN32 1 // For gd
#define NONDLL 1 // For gd
#endif
#include <gd.h>
#include <gdfonts.h>
#ifdef RS_WINDOWS
#define my_snprintf my_snprintf_win
#else
#define my_snprintf snprintf
#endif
#define RS_VERSION "1.2"
// Ideally, DOTIMG_SRC_WIDTH should be a prime number, 2 larger than an easily-typed number.
#define DOTIMG_SRC_WIDTH 557
// DOTIMG_HPIXELSPAN should be an odd number. Larger numbers make the source image larger,
// but allow for larger downscaling factors, and filters with larger radii.
#define DOTIMG_HPIXELSPAN 25
#define DOTIMG_NUMSTRIPS DOTIMG_HPIXELSPAN
#define DOTIMG_HCENTER ((DOTIMG_HPIXELSPAN-1)/2)
// DOTIMG_STRIPHEIGHT should be an odd number, at least 9 or 11. Larger numbers make the
// source image larger.
#define DOTIMG_STRIPHEIGHT 11
#define DOTIMG_VCENTER ((DOTIMG_STRIPHEIGHT-1)/2)
#define DOTIMG_SRC_HEIGHT (DOTIMG_NUMSTRIPS*DOTIMG_STRIPHEIGHT)
#define DOTIMG_DST_WIDTH (DOTIMG_SRC_WIDTH-2)
#define DOTIMG_DST_HEIGHT DOTIMG_SRC_HEIGHT
// LINEIMG_SRC_WIDTH should be an odd number, at least 9 or 11. Larger numbers allow for analysis
// of filters with larger radii, but may require you to upscale to larger sizes.
#define LINEIMG_SRC_WIDTH 15
// LINEIMG_SRC_HEIGHT should be an odd number, big enough to keep the middle rows safe from
// the effects of the top and bottom edges of the image.
#define LINEIMG_SRC_HEIGHT 15
// LINEIMG_DST_WIDTH is purely a recommendation to the user, and has no effect on the program.
// For the smoothest graphs, it should be an odd multiple of LINEIMG_SRC_WIDTH. Ideally, it
// should be easy to type.
#define LINEIMG_DST_WIDTH 555
#define LINEIMG_DST_HEIGHT LINEIMG_SRC_HEIGHT
#define PATTERN_LINEIMG 1
#define PATTERN_DOTIMG 2
struct infile_info {
const char *fn;
const char *name;
double scale_factor_req; // The scale factor requested by the user.
int scale_factor_req_set;
double scale_fudge_factor_req; // Multiply the default scale factor by this fudge factor.
int scale_fudge_factor_req_set;
int thicklines;
int color_r, color_g, color_b;
#define CCMETHOD_LINEAR 0
#define CCMETHOD_SRGB 2
int color_correction_method;
};
struct context {
int rotated;
// Size of the input image.
int w, h;
// The scale factor (of the image features, not necessarily of the image itself)
// that we believe was used when the input image was created.
double scale_factor;
// The scale factor based on the number of pixels in the images.
double natural_scale_factor;
gdImagePtr im_in;
FILE *im_in_fp;
// Preferred color to use to for information about the current input image.
int curr_color;
int border_color;
// Persistent information about each input file.
struct infile_info inf[2];
// Information about the output image coordinates.
int gr_width, gr_height;
double gr_zero_x, gr_zero_y;
double gr_unit_x, gr_unit_y;
const char *outfn;
gdImagePtr im_out;
// Tracks how many graphs we've plotted on this output image.
int graph_count;
int include_logo;
int expandrange;
// Temporary space for the samples being analyzed.
// (Currently only used when with lineimg.)
double *samples;
// Used by the line drawing function
int lastpos_set;
int lastpos_x, lastpos_y;
double lastpos_x_dbl, lastpos_y_dbl;
double srgb50_as_lin1, srgb_250_as_lin1; // Cached calculated values
};
#ifdef RS_WINDOWS
static wchar_t *de_utf8_to_utf16_strdup(const char *src)
{
WCHAR *dst;
int dstlen;
int ret;
// Calculate the size required by the target string.
ret = MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0);
if(ret<1) {
return NULL;
}
dstlen = ret;
dst = malloc(dstlen*sizeof(WCHAR));
ret = MultiByteToWideChar(CP_UTF8, 0, src, -1, dst, dstlen);
if(ret<1) {
free(dst);
return NULL;
}
return dst;
}
static char *utf16_to_utf8_strdup(const wchar_t *src)
{
char *dst;
int dstlen;
int ret;
// Calculate the size required by the target string.
ret = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL);
if(ret<1) return NULL;
dstlen = ret;
dst = malloc(dstlen);
ret = WideCharToMultiByte(CP_UTF8, 0, src, -1, dst, dstlen, NULL, NULL);
if(ret<1) {
free(dst);
return NULL;
}
return dst;
}
static void my_snprintf_win(char *buf, size_t buflen, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_vsnprintf_s(buf, buflen, _TRUNCATE, fmt, ap);
va_end(ap);
}
#endif
#ifdef RS_WINDOWS
static void printmsg(struct context *c, const char *fmt, ...)
{
va_list ap;
char buf[500];
WCHAR bufW[1000];
va_start(ap, fmt);
_vsnprintf_s(buf, sizeof(buf), _TRUNCATE, fmt, ap);
va_end(ap);
// Convert from UTF-8 to UTF-16
MultiByteToWideChar(CP_UTF8, 0,
buf, -1,
bufW, sizeof(bufW)/sizeof(WCHAR));
fputws(bufW, stderr);
}
#else
static void printmsg(struct context *c, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
#endif
#ifdef RS_WINDOWS
static FILE* my_fopen(const char *fn, const char *mode)
{
FILE *f = NULL;
errno_t errcode;
WCHAR *fnW;
WCHAR *modeW;
fnW = de_utf8_to_utf16_strdup(fn);
modeW = de_utf8_to_utf16_strdup(mode);
errcode = _wfopen_s(&f, fnW, modeW);
free(fnW);
free(modeW);
if(errcode!=0) {
f=NULL;
}
return f;
}
#else
static FILE *my_fopen(const char *file, const char *mode)
{
return fopen(file, mode);
}
#endif
static unsigned char unicode_to_latin2_char(unsigned int uchar)
{
size_t i;
static const unsigned short latin2table[96] = {
0x00A0,0x0104,0x02D8,0x0141,0x00A4,0x013D,0x015A,0x00A7, // 160-167
0x00A8,0x0160,0x015E,0x0164,0x0179,0x00AD,0x017D,0x017B, // 168-176
0x00B0,0x0105,0x02DB,0x0142,0x00B4,0x013E,0x015B,0x02C7, // ...
0x00B8,0x0161,0x015F,0x0165,0x017A,0x02DD,0x017E,0x017C,
0x0154,0x00C1,0x00C2,0x0102,0x00C4,0x0139,0x0106,0x00C7,
0x010C,0x00C9,0x0118,0x00CB,0x011A,0x00CD,0x00CE,0x010E,
0x0110,0x0143,0x0147,0x00D3,0x00D4,0x0150,0x00D6,0x00D7,
0x0158,0x016E,0x00DA,0x0170,0x00DC,0x00DD,0x0162,0x00DF,
0x0155,0x00E1,0x00E2,0x0103,0x00E4,0x013A,0x0107,0x00E7,
0x010D,0x00E9,0x0119,0x00EB,0x011B,0x00ED,0x00EE,0x010F,
0x0111,0x0144,0x0148,0x00F3,0x00F4,0x0151,0x00F6,0x00F7,
0x0159,0x016F,0x00FA,0x0171,0x00FC,0x00FD,0x0163,0x02D9 // 248-255
};
for(i=0; i<96; i++) {
if((unsigned int)latin2table[i]==uchar) {
return (unsigned char)(160+i);
}
}
return '?';
}
static void utf8_to_latin2_string(unsigned char *src, unsigned char *dst, size_t dstlen)
{
size_t srcpos, dstpos;
unsigned char ch;
unsigned int pending_char;
int bytes_expected;
srcpos = 0;
dstpos = 0;
pending_char = 0;
bytes_expected = 0;
while(1) {
if(dstpos >= dstlen-1) {
dst[dstlen-1] = '\0';
break;
}
ch = src[srcpos++];
if(ch<128) { // Only byte of a 1-byte sequence
dst[dstpos++] = ch;
if(ch=='\0') break;
}
else if(ch<0xc0) { // Continuation byte
if(bytes_expected>0) {
pending_char = (pending_char<<6)|(ch&0x3f);
bytes_expected--;
if(bytes_expected<1) {
dst[dstpos++] = unicode_to_latin2_char(pending_char);
}
}
}
else if(ch<0xe0) { // 1st byte of a 2-byte sequence
pending_char = ch&0x1f;
bytes_expected=1;
}
else if(ch<0xf0) { // 1st byte of a 3-byte sequence
pending_char = ch&0x0f;
bytes_expected=2;
}
else if(ch<0xf8) { // 1st byte of a 4-byte sequence
pending_char = ch&0x07;
bytes_expected=3;
}
}
}
static void my_gdImageString(gdImagePtr im, gdFontPtr f, int x, int y,
unsigned char *src_utf8, int color)
{
unsigned char *src_latin2;
size_t src_latin2_len;
src_latin2_len = strlen((const char*)src_utf8) + 1;
src_latin2 = malloc(src_latin2_len);
// The 'gdFontSmall' font we're using has Latin-2 encoding.
// That's not very useful if you're not Eastern European.
// TODO: The fix would presumably be to use gd's FreeType features.
utf8_to_latin2_string(src_utf8, src_latin2, src_latin2_len);
gdImageString(im, f, x, y, src_latin2, color);
free(src_latin2);
}
static void gr_init(struct context *c)
{
c->gr_width = 600;
c->gr_zero_x = 230.0;
c->gr_unit_x = 90.0;
c->gr_height = 300;
if(c->expandrange==2) {
c->gr_zero_y = 260.0;
c->gr_unit_y = -90.0;
}
else if(c->expandrange==1) {
c->gr_zero_y = 240.0;
c->gr_unit_y = -150.0;
}
else {
c->gr_zero_y = 220.0;
c->gr_unit_y = -200.0;
}
c->im_out = gdImageCreate(c->gr_width,c->gr_height);
gdImageFilledRectangle(c->im_out,0,0,c->gr_width-1,c->gr_height-1,
gdImageColorResolve(c->im_out,255,255,255));
}
static void gr_done(struct context *c)
{
FILE *w;
w = my_fopen(c->outfn,"wb");
if(!w) return;
gdImagePng(c->im_out,w);
fclose(w);
gdImageDestroy(c->im_out);
}
static int point_is_visible(struct context *c, int x, int y)
{
if(x<0 || y<0) return 0;
if(x>=c->gr_width || y>=c->gr_height) return 0;
return 1;
}
// Convert from logical coordinates to output-image coordinates.
static int xcoord(struct context *c, double ix)
{
double physx;
physx = c->gr_zero_x + (ix*c->gr_unit_x);
return (int)(0.5+physx);
}
static int ycoord(struct context *c, double iy)
{
double physy;
physy = c->gr_zero_y + (iy*c->gr_unit_y);
return (int)(0.5+physy);
}
static void gr_draw_grid(struct context *c)
{
int clr;
int i;
char tbuf[20];
clr = gdImageColorResolve(c->im_out,192,192,192);
for(i= -10; i<=10; i++) {
// Draw lines for half-integers
gdImageDashedLine(c->im_out,xcoord(c,0.5+(double)i),0,xcoord(c,0.5+(double)i),c->gr_height,clr);
gdImageDashedLine(c->im_out,0,ycoord(c,0.5+(double)i),c->gr_width,ycoord(c,0.5+(double)i),clr);
}
// Draw lines for integers
clr = gdImageColorResolve(c->im_out,192,192,192);
for(i= -10; i<=10; i++) {
gdImageLine(c->im_out,xcoord(c,i),0,xcoord(c,i),c->gr_height,clr);
gdImageLine(c->im_out,0,ycoord(c,i),c->gr_width,ycoord(c,i),clr);
}
// Draw x- and y- axes
clr = gdImageColorResolve(c->im_out,0,0,0);
gdImageLine(c->im_out,xcoord(c,0.0),0,xcoord(c,0.0),c->gr_height,clr);
gdImageLine(c->im_out,0,ycoord(c,0.0),c->gr_width,ycoord(c,0.0),clr);
// Draw labels
clr = gdImageColorResolve(c->im_out,0,128,0);
for(i= 0; i<=1; i++) {
my_snprintf(tbuf, sizeof(tbuf), "%d", i);
my_gdImageString(c->im_out,gdFontSmall,xcoord(c,i)-6,c->gr_height-14,
(unsigned char*)tbuf,clr);
my_gdImageString(c->im_out,gdFontSmall,3,ycoord(c,i)-12,
(unsigned char*)tbuf,clr);
}
// Draw border around the whole image
gdImageRectangle(c->im_out,0,0,c->gr_width-1,c->gr_height-1,c->border_color);
}
static void gr_lineto(struct context *c, double xpos1, double ypos1, int clr)
{
int xpos, ypos;
xpos = xcoord(c,xpos1);
ypos = ycoord(c,ypos1);
if(c->lastpos_set) {
gdImageLine(c->im_out,c->lastpos_x,c->lastpos_y,xpos,ypos,clr);
}
c->lastpos_x = xpos;
c->lastpos_y = ypos;
c->lastpos_x_dbl = xpos1;
c->lastpos_y_dbl = ypos1;
c->lastpos_set=1;
}
// Heuristically figure out a friendly name to use for the graph.
static void gr_get_name_from_fn(const char *fn, char *buf, size_t buflen)
{
char *r;
r = strrchr(fn,'/');
#ifdef RS_WINDOWS
if(!r) r = strrchr(fn,'\\');
#endif
if(r)
my_snprintf(buf, buflen, "%s", r+1);
else
my_snprintf(buf, buflen, "%s", fn);
r = strrchr(buf,'.');
if(r) {
*r = '\0';
}
}
static void gr_draw_graph_name(struct context *c, struct infile_info *inf, int sf_flag)
{
int ypos;
char buf[100];
char s[100];
ypos = c->gr_height-19-14*c->graph_count;
if(inf->name) {
my_snprintf(buf, sizeof(buf), "%s", inf->name);
}
else {
gr_get_name_from_fn(inf->fn,buf,100);
}
if(inf->thicklines) gdImageSetThickness(c->im_out,3);
gdImageLine(c->im_out,5,ypos+7,13,ypos+7,c->curr_color);
gdImageSetThickness(c->im_out,1);
my_snprintf(s, sizeof(s), "%s", buf);
if(sf_flag) {
double ff;
ff = c->scale_factor / c->natural_scale_factor;
if(ff<0.99999999 || ff>1.00000001) {
my_snprintf(s, sizeof(s), "%s (factor=%.8f)", buf, ff);
}
}
s[sizeof(s)-1]='\0';
my_gdImageString(c->im_out,gdFontSmall,17,ypos,(unsigned char*)s,c->curr_color);
}
static void gr_draw_logo(struct context *c)
{
if(!c->include_logo) return;
gdImageFilledRectangle(c->im_out,c->gr_width-81,c->gr_height-15,
c->gr_width-1,c->gr_height-1,c->border_color);
my_gdImageString(c->im_out,gdFontSmall,c->gr_width-79,c->gr_height-15,
(unsigned char*)"ResampleScope",
gdImageColorResolve(c->im_out,255,255,255));
}
/////////////////////////////////////////////////
// Wrappers for gd functions, which swap the x and y coordinates if -r was used,
// and may do colorspace transformation.
static void rs_gdImageSetPixel(struct context *c, gdImagePtr im, int x, int y, int color)
{
if(c->rotated)
gdImageSetPixel(im,y,x,color);
else
gdImageSetPixel(im,x,y,color);
}
static double srgb_to_linear(double v_srgb)
{
if(v_srgb<=0.04045) {
return v_srgb/12.92;
}
else {
return pow( (v_srgb+0.055)/(1.055) , 2.4);
}
}
// Returns a value typically in the range 0..255,
// where 50 and 250 are our special "dark" and "light" colors.
static double rs_gdImageGetPixel(struct context *c, struct infile_info *inf,
gdImagePtr im, int x, int y)
{
int colorref;
double val;
if(c->rotated)
colorref = gdImageGetPixel(im,y,x);
else
colorref = gdImageGetPixel(im,x,y);
val = (double)gdImageGreen(im, colorref);
if(inf->color_correction_method==CCMETHOD_SRGB) {
double v1;
// We're assuming the app being tested behaved as follows:
// (1) converted the original colors from sRGB to linear;
// (2) resized the image in a linear colorspace;
// (3) converted back to sRGB.
// First, undo (3) by converting from sRGB[0..255] to linear[0..1].
v1 = srgb_to_linear((double)val/255.0);
// The catch is that the color is now in that app's linear colorspace,
// not ours.
// If we were to simply multiply the value by 255, our dark color would
// correspond to ~8.1, and our light color to ~243.7.
// That's not what we want. We want 50 and 250.
// So, we have to be careful to scale and translate it to the correct
// range.
val = (v1-c->srgb50_as_lin1) *
((250.0-50.0)/(c->srgb_250_as_lin1-c->srgb50_as_lin1)) + 50.0;
}
return val;
}
static int rs_gdImageSX(struct context *c, gdImagePtr im)
{
return c->rotated ? gdImageSY(im) : gdImageSX(im);
}
static int rs_gdImageSY(struct context *c, gdImagePtr im)
{
return c->rotated ? gdImageSX(im) : gdImageSY(im);
}
/////////////////////////////////////////////////
// Opens and reads the image, if that hasn't already been done.
static int open_file_for_reading(struct context *c, const char *fn)
{
// The file may have already been opened, to detect the image type.
// If not, open it now.
if(!c->im_in_fp) {
c->im_in_fp = my_fopen(fn,"rb");
if(!c->im_in_fp) {
printmsg(c, "* Error: Failed to read %s\n",fn);
return 0;
}
}
if(!c->im_in) {
c->im_in = gdImageCreateFromPng(c->im_in_fp);
if(!c->im_in) {
printmsg(c, "gd creation failed\n");
return 0;
}
}
return 1;
}
static void close_file_for_reading(struct context *c)
{
if(c->im_in) { gdImageDestroy(c->im_in); c->im_in = NULL; }
if(c->im_in_fp) { fclose(c->im_in_fp); c->im_in_fp = NULL; }
}
//////////////////// DOTIMG ////////////////////
// Plot values for pixels the given "strip".
// The first DOTIMG_STRIPHEIGHT scalines are strip 0,
// the next DOTIMG_STRIPHEIGHT are strip 1, etc.
static int plot_strip(struct context *c, struct infile_info *inf, int stripnum)
{
double v;
int dstpos,k;
double tot;
double value;
int xc,yc;
double zp;
double tmp_offset;
double offset; // Relative position of the nearest "0" point, in target image coords.
for(dstpos=0;dstpos<c->w;dstpos++) {
// Scan through the possible "0" points in this strip, and find the nearest one.
offset = 10000.0; // (too far)
// There are "0" points at ({5,16,27,38...}+stripnum) * scale_factor
for(k=(DOTIMG_HCENTER+stripnum); k<(DOTIMG_SRC_WIDTH-DOTIMG_HCENTER); k+=DOTIMG_HPIXELSPAN) {
// Convert to target image coordinates.
// I don't really remember why this formula works, but it seems to.
zp = (c->scale_factor)*(((double)k) + 0.5 - ((double)DOTIMG_SRC_WIDTH)/2.0) + (c->w/2.0) - 0.5;
// The directed distance to this 0 point.
tmp_offset = ((double)dstpos)-zp;
// Keep track of the smallest (in absolute value) distance.
if(fabs(tmp_offset)<fabs(offset)) {
offset = tmp_offset;
}
}
// Make sure the offset is small enough to be meaningful
// TODO: This might only be correct when downscaling.
if(fabs(offset)>(c->scale_factor*DOTIMG_HCENTER)) continue;
// Add up the DOTIMG_STRIPHEIGHT pixels vertically that are in this strip.
// Ideally, all but the middle one will be empty, but in reality most
// filters get applied vertically as well as horizontally, which can
// cause vertical blurring depending on the filter. This is how we
// undo that.
tot = 0;
for(k=0;k<DOTIMG_STRIPHEIGHT;k++) {
v = rs_gdImageGetPixel(c, inf, c->im_in,
dstpos, DOTIMG_STRIPHEIGHT*stripnum+k);
tot += (v-50.0);
}
// Convert (0 to 200) to (0 to 1).
value = tot/200.0;
if(c->scale_factor < 1.0) {
// Compensate for the fact that we're shrinking the image, which
// reduces the size of a pixel, making it dimmer.
// This factor is normally about 0.996, so this will only make
// a small difference.
value /= c->scale_factor;
}
else {
offset /= c->scale_factor;
}
// Plot the point.
xc = xcoord(c,offset);
yc = ycoord(c,value);
if(point_is_visible(c,xc,yc)) {
gdImageSetPixel(c->im_out,xc,yc,c->curr_color);
if(inf->thicklines) {
gdImageSetPixel(c->im_out,xc-1,yc,c->curr_color);
gdImageSetPixel(c->im_out,xc+1,yc,c->curr_color);
gdImageSetPixel(c->im_out,xc,yc-1,c->curr_color);
gdImageSetPixel(c->im_out,xc,yc+1,c->curr_color);
}
}
}
return 1;
}
static void decide_scale_factor(struct context *c, struct infile_info *inf, int src_width)
{
// Start with the default scale factor:
c->scale_factor = ((double)c->w) / src_width;
c->natural_scale_factor = c->scale_factor;
if(inf->scale_factor_req_set) {
// scale factor overridden by user
c->scale_factor = inf->scale_factor_req;
}
if(inf->scale_fudge_factor_req_set) {
// adjust the scale factor as requested
c->scale_factor *= inf->scale_fudge_factor_req;
}
}
static int run_dotimg_1file(struct context *c, struct infile_info *inf)
{
int retval=0;
int i;
printmsg(c, " Reading %s\n",inf->fn);
if(!open_file_for_reading(c,inf->fn)) goto done;
c->w = rs_gdImageSX(c,c->im_in);
c->h = rs_gdImageSY(c,c->im_in);
if(c->h != DOTIMG_SRC_HEIGHT) {
printmsg(c, "* Error: Image is wrong height (is %d, should be %d)\n",c->h,DOTIMG_SRC_HEIGHT);
goto done;
}
if(c->w<50) {
printmsg(c, "* Error: Image is wrong width (is %d, must be at least 50)\n",c->w);
goto done;
}
decide_scale_factor(c,inf,DOTIMG_SRC_WIDTH);
gr_draw_graph_name(c,inf,1);
for(i=0;i<DOTIMG_NUMSTRIPS;i++) {
if(!plot_strip(c,inf,i)) goto done;
}
retval=1;
done:
close_file_for_reading(c);
c->graph_count++;
return retval;
}
static int run_ds(struct context *c)
{
int ret;
printmsg(c, "Writing %s [dot pattern]\n",c->outfn);
gr_init(c);
c->border_color = gdImageColorResolve(c->im_out,144,192,144);
gr_draw_grid(c);
gr_draw_logo(c);
if(c->inf[1].fn) {
c->curr_color = gdImageColorResolve(c->im_out,
c->inf[1].color_r,c->inf[1].color_g,c->inf[1].color_b);
ret = run_dotimg_1file(c,&c->inf[1]);
c->lastpos_set = 0;
}
c->curr_color = gdImageColorResolve(c->im_out,
c->inf[0].color_r,c->inf[0].color_g,c->inf[0].color_b);
ret = run_dotimg_1file(c,&c->inf[0]);
gr_done(c);
return ret;
}
////////////////////////////////////////////////
//////////////////// LINEIMG ///////////////////
static void gr_lineimg_graph_main(struct context *c, struct infile_info *inf)
{
int i;
double v;
int clr;
double xp, yp;
double tot = 0.0;
double area;
clr = c->curr_color;
if(inf->thicklines)
gdImageSetThickness(c->im_out,3);
for(i=0;i<c->w;i++) {
v = c->samples[i];
yp = (v-50.0)/200.0;
tot += yp;
xp = 0.5+(double)i-(((double)c->w)/2.0);
if(c->scale_factor < 1.0) {
yp /= c->scale_factor;
}
else {
xp /= c->scale_factor;
}
gr_lineto(c,xp,yp,clr);
}
gdImageSetThickness(c->im_out,1);
area = tot/c->scale_factor;
printmsg(c, " Area = %.6f",area);
printmsg(c, "\n");
}
static int run_lineimg_1file(struct context *c, struct infile_info *inf)
{
int retval=0;
int i;
int scanline; // The (middle) scanline we'll analyze
printmsg(c, " Reading %s\n",inf->fn);
if(!open_file_for_reading(c,inf->fn)) {
goto done;
}
c->w = rs_gdImageSX(c,c->im_in);
c->h = rs_gdImageSY(c,c->im_in);
if(c->h < 3) {
printmsg(c, "Image height (%d) too small\n",c->h);
goto done;
}
decide_scale_factor(c,inf,LINEIMG_SRC_WIDTH);
gr_draw_graph_name(c,inf,1);
scanline = c->h / 2;
// Copy the samples we're analyzing
c->samples = malloc(sizeof(double)*c->w);
for(i=0;i<c->w;i++) {
// Read from three different scanlines, to give us a chance of
// detecting weird issues where the scanlines aren't identical.
c->samples[i] = rs_gdImageGetPixel(c, inf, c->im_in,
i, scanline+(i%3)-1);
}
gr_lineimg_graph_main(c,inf);
retval=1;
done:
close_file_for_reading(c);
if(c->samples) { free(c->samples); c->samples=NULL; }
c->graph_count++;
return retval;
}
static int run_us(struct context *c)
{
int ret = 0;
printmsg(c, "Writing %s [line pattern]\n",c->outfn);
gr_init(c);
c->border_color = gdImageColorResolve(c->im_out,204,136,204);
gr_draw_grid(c);
gr_draw_logo(c);
if(c->inf[1].fn) {
c->curr_color = gdImageColorResolve(c->im_out,
c->inf[1].color_r,c->inf[1].color_g,c->inf[1].color_b);
ret = run_lineimg_1file(c,&c->inf[1]);
c->lastpos_set = 0;
}
c->curr_color = gdImageColorResolve(c->im_out,
c->inf[0].color_r,c->inf[0].color_g,c->inf[0].color_b);
ret = run_lineimg_1file(c,&c->inf[0]);
gr_done(c);
return ret;
}
///////////////////////////////////////////////
/////////////// FILE GENERATION ///////////////
static int gen_dotimg_image(struct context *c)
{
int i,j;
gdImagePtr im = NULL;
FILE *w = NULL;
int clr_gray, clr_white;
int clr;
int retval=0;
const char *fn;
fn = c->rotated ? "pdr.png" : "pd.png";
w = my_fopen(fn,"wb");
if(!w) {
printmsg(c, "Can't write %s\n",fn);
goto done;
}
if(c->rotated)
im = gdImageCreateTrueColor(DOTIMG_SRC_HEIGHT,DOTIMG_SRC_WIDTH);
else
im = gdImageCreateTrueColor(DOTIMG_SRC_WIDTH,DOTIMG_SRC_HEIGHT);
clr_gray = gdImageColorResolve(im,50,50,50);
clr_white = gdImageColorResolve(im,250,250,250);
for(j=0;j<DOTIMG_SRC_HEIGHT;j++) {
for(i=0;i<DOTIMG_SRC_WIDTH;i++) {
clr = clr_gray;
if((j%DOTIMG_STRIPHEIGHT==DOTIMG_VCENTER) && (i>=DOTIMG_HCENTER) && (i<DOTIMG_SRC_WIDTH-DOTIMG_HCENTER)) {
if((i-j/DOTIMG_STRIPHEIGHT)%DOTIMG_HPIXELSPAN == DOTIMG_HCENTER) {
clr = clr_white;
}
}
rs_gdImageSetPixel(c,im,i,j,clr);
}
}
gdImagePng(im,w);
if(c->rotated) {
printmsg(c, "Wrote %s (%dx%d - resize to %dx%d)\n",fn,
DOTIMG_SRC_HEIGHT,DOTIMG_SRC_WIDTH,
DOTIMG_DST_HEIGHT,DOTIMG_DST_WIDTH);
}
else {
printmsg(c, "Wrote %s (%dx%d - resize to %dx%d)\n",fn,
DOTIMG_SRC_WIDTH,DOTIMG_SRC_HEIGHT,
DOTIMG_DST_WIDTH,DOTIMG_DST_HEIGHT);
}
retval=1;
done:
if(im) gdImageDestroy(im);
if(w) fclose(w);
return retval;
}
static int gen_lineimg_image(struct context *c)
{
int i,j;
gdImagePtr im = NULL;
FILE *w = NULL;
int clr_black, clr_white;
int clr;
int retval=0;
int middle;