forked from blackfiveimaging/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgsrctest.cpp
545 lines (495 loc) · 13.1 KB
/
imgsrctest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include "cachedimage.h"
#include "imagesource_util.h"
#include "imagesource_dither.h"
#include "imagesource_solid.h"
#include "imagesource_cms.h"
#include "imagesource_desaturate.h"
#include "imagesource_rotate.h"
#include "imagesource_bilinear.h"
#include "imagesource_lanczossinc.h"
#include "imagesource_downsample.h"
#include "imagesource_convolution.h"
#include "imagesource_gaussianblur.h"
#include "imagesource_unsharpmask.h"
#include "imagesource_devicen_preview.h"
#include "imagesource_devicen_remap.h"
#include "imagesource_montage.h"
#include "imagesource_scaledensity.h"
#include "imagesource_gamma.h"
#include "imagesource_chequerboard.h"
#include "imagesource/imagesource_dropshadow.h"
#include "imagesource_hsweep.h"
#include "imagesource_hticks.h"
#include "jpegsaver.h"
#include "tiffsaver.h"
#include "progresstext.h"
#include "profilemanager/profilemanager.h"
#if 0
class ImageSource_RainbowSweep : public ImageSource
{
public:
ImageSource_RainbowSweep(int width,int height)
: ImageSource(width,height,IS_TYPE_RGB)
{
randomaccess=true;
MakeRowBuffer();
}
~ImageSource_RainbowSweep()
{
}
ISDataType *GetRow(int row)
{
if(currentrow==row)
return(rowbuffer);
double a=row;
a/=height;
for(int x=0;x<width;++x)
{
int c=(x*1536)/width;
int r=c;
if(r>768) r-=1536;
if(r<0) r=-r;
r=512-r;
if(r<0) r=0;
if(r>255) r=255;
int g=c-512;
if(g<0) g=-g;
g=512-g;
if(g<0) g=0;
if(g>255) g=255;
int b=c-1024;
if(b<0) b=-b;
b=512-b;
if(b<0) b=0;
if(b>255) b=255;
r=128*a+r*(1-a);
g=128*a+g*(1-a);
b=128*a+b*(1-a);
rowbuffer[x*3]=EIGHTTOIS(r);
rowbuffer[x*3+1]=EIGHTTOIS(g);
rowbuffer[x*3+2]=EIGHTTOIS(b);
}
currentrow=row;
return(rowbuffer);
}
};
int main(int argc,char **argv)
{
try
{
if(argc<2)
return(0);
ImageSource *is=new ImageSource_RainbowSweep(1024,300);
JPEGSaver js(argv[1],RefCountPtr<ImageSource>(is));
js.Save();
}
catch(const char *err)
{
cerr << "Error: " << err << endl;
}
return(0);
}
#endif
#if 0
class Test : public ConfigFile, public ProfileManager
{
public:
Test() : ConfigFile(), ProfileManager(this,"[ColourManagement]"), factory(*this)
{
}
~Test()
{
}
void ShowParasites(ImageSource *is)
{
if(is)
{
RefCountPtr<ISParasite> p=is->GetParasite(ISPARATYPE_PSIMAGERESOURCEBLOCK);
if(p)
Debug[TRACE] << "Got clipping path parasite" << std::endl;
else
Debug[TRACE] << "No clipping path parasite" << std::endl;
}
}
void ProcessImage(const char *fn)
{
RefCountPtr<char> tmp=RefCountPtr<char>(BuildFilename(fn,"-test","tif"));
ImageSource *is=ISLoadImage(fn);
ShowParasites(is);
CachedImage cached((ImageSource_rp(is)));
is=cached.GetImageSource();
RefCountPtr<CMSTransform> trans;
RefCountPtr<CMSProfile>emb=is->GetEmbeddedProfile();
if(emb)
trans=factory.GetTransform(CM_COLOURDEVICE_DISPLAY,&*emb);
else
trans=factory.GetTransform(CM_COLOURDEVICE_DISPLAY,is->type);
is=new ImageSource_CMS(is,trans);
// is=new ImageSource_DropShadow(is,20,5);
ProgressText prog;
TIFFSaver ts(&*tmp,ImageSource_rp(is));
ts.SetProgress(&prog);
ts.Save();
}
protected:
CMTransformFactory factory;
};
int main(int argc,char **argv)
{
try
{
if(argc<2)
return(0);
Test test;
test.ProcessImage(argv[1]);
}
catch(const char *err)
{
cerr << "Error: " << err << endl;
}
return(0);
}
#endif
#if 0
// Render a sweep pattern for approximate gamma determination.
// This doesn't need to be accurate (which is just as well, because it isn't!)
// - just good enough to give us an approximate gamma so the test point when
// printing the linearization charts are reasonably well-distributed.
ImageSource *MakeGammaEvalSweep(DeviceNColorantList &colorants,int width,int height,int res)
{
int cols=colorants.GetColorantCount();
int stripeheight=height/3;
// We search for black, and throw an error if not found...
int blk=-1;
blk=colorants.GetColorantIndex("Black");
if(blk==-1)
blk=colorants.GetColorantIndex("Matte Black");
if(blk==-1)
throw "Error: Can't find black ink!";
ISDeviceNValue bg(cols,0); // Background colour
ISDeviceNValue fg(cols,0); // Foreground for ticks.
fg[blk]=IS_SAMPLEMAX; // Render ticks in black
ImageSource_Montage *mon=new ImageSource_Montage(IS_TYPE_DEVICEN,res,cols);
ImageSource_HTicks *ticks=new ImageSource_HTicks(width,stripeheight,IS_TYPE_DEVICEN,cols);
ticks->SetBG(bg);
ticks->SetFG(fg);
mon->Add(ticks,0,0); // Now owned by the montage
ImageSource_HSweep *sweep = new ImageSource_HSweep(width,stripeheight,bg,fg,IS_TYPE_DEVICEN);
mon->Add(sweep,0,stripeheight); // Now owned by the montage
ImageSource_Chequerboard *cb = new ImageSource_Chequerboard(width,stripeheight,IS_TYPE_DEVICEN,cols,4);
cb->SetBG(bg);
cb->SetFG(fg);
mon->Add(cb,0,stripeheight*2); // Now owned by the montage
return(mon);
}
// Render a density evaluation pattern - this pattern should be printed at reduced density in
// Raw mode to determine absolute maximum ink levels for individual ink channels.
ImageSource *MakeDensityEvalSweep(DeviceNColorantList &colorants,int width,int height,int res)
{
int cols=colorants.GetColorantCount();
int stripeheight=(2*height)/(cols+1); // We'll render each pass as stripeheight pixels
// and the row of "ticks" at half that height
// We search for black, and throw an error if not found...
int blk=-1;
blk=colorants.GetColorantIndex("Black");
if(blk==-1)
blk=colorants.GetColorantIndex("Matte Black");
if(blk==-1)
throw "Error: Can't find black ink!";
ISDeviceNValue bg(cols,0); // Background colour
ISDeviceNValue tfg(cols,0); // Foreground for ticks.
tfg[blk]=IS_SAMPLEMAX; // Render ticks in black
ImageSource_Montage *mon=new ImageSource_Montage(IS_TYPE_DEVICEN,res,cols);
ImageSource_HTicks *ticks=new ImageSource_HTicks(width/2,stripeheight/2,IS_TYPE_DEVICEN,cols);
ticks->SetBG(bg);
ticks->SetFG(tfg);
mon->Add(ticks,0,0); // Now owned by the montage
ticks=new ImageSource_HTicks(width/2,stripeheight/2,IS_TYPE_DEVICEN,cols);
ticks->SetBG(bg);
ticks->SetFG(tfg);
mon->Add(ticks,width/2,0);
for(int i=0;i<cols;++i)
{
ISDeviceNValue sfg(cols,0);
sfg[i]=IS_SAMPLEMAX;
ImageSource *is=new ImageSource_HSweep(width/2,stripeheight,bg,sfg,IS_TYPE_DEVICEN);
mon->Add(is,(width/2)*(i&1),stripeheight*(i/2)+stripeheight/2);
}
return(mon);
}
int main(int argc,char **argv)
{
try
{
if(argc==2)
{
DeviceNColorantList colorants;
new DeviceNColorant(colorants,"Yellow");
new DeviceNColorant(colorants,"Black");
new DeviceNColorant(colorants,"Light Cyan");
new DeviceNColorant(colorants,"Light Magenta");
new DeviceNColorant(colorants,"Magenta");
new DeviceNColorant(colorants,"Cyan");
// ImageSource *is=MakeDensityEvalSweep(colorants,1140,200,144);
ImageSource *is=MakeGammaEvalSweep(colorants,1140,90,144);
ImageSource_DeviceN_Preview dst(is,&colorants);
TIFFSaver ts(argv[1],&dst);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
}
if(argc==4)
{
ImageSource *is=ISLoadImage(argv[1]);
int pc=atoi(argv[2]);
ImageSource_Downsample dst(is,(is->width*pc)/100,(is->height*pc)/100);
TIFFSaver ts(argv[3],&dst);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
}
if(argc==5)
{
ImageSource *is=ISLoadImage(argv[1]);
int pc=atoi(argv[2]);
float gamma=atof(argv[3]);
is=new ImageSource_Gamma(is,gamma);
is=new ImageSource_Downsample(is,(is->width*pc)/100,(is->height*pc)/100);
ImageSource_Gamma dst(is,1.0/gamma);
TIFFSaver ts(argv[4],&dst);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
}
#endif
#if 0
if(argc==3)
{
int channelmap[]={5,4,0,1,2,3};
DeviceNColorantList colorants;
new DeviceNColorant(colorants,"Yellow");
new DeviceNColorant(colorants,"Black");
new DeviceNColorant(colorants,"Light Cyan");
new DeviceNColorant(colorants,"Light Magenta");
new DeviceNColorant(colorants,"Magenta");
new DeviceNColorant(colorants,"Cyan");
new DeviceNColorant(colorants,"Light Black");
ImageSource *is=ISLoadImage(argv[1]);
is=new ImageSource_DeviceN_Remap(is,channelmap);
ImageSource_DeviceN_Preview dst(is,&colorants);
TIFFSaver ts(argv[2],&dst);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
}
if(argc==5)
{
ImageSource *is=ISLoadImage(argv[1]);
int w=atoi(argv[2]);
int h=atoi(argv[3]);
ImageSource_Downsample dst(is,w,h);
TIFFSaver ts(argv[4],&dst);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
}
#endif
#if 0
if(argc==4)
{
int tempchange=atoi(argv[2]);
ImageSource *is=ISLoadImage(argv[1]);
CMSRGBGamma RGBGamma(2.2,2.2,2.2);
CMSGamma GreyGamma(2.2);
CMSWhitePoint srcwp(6500+tempchange);
CMSWhitePoint dstwp(6500);
CMSProfile *source;
switch(STRIP_ALPHA(is->type))
{
case IS_TYPE_RGB:
is=new ImageSource_Desaturate(is);
source=new CMSProfile(CMSPrimaries_Rec709,RGBGamma,srcwp);
break;
case IS_TYPE_GREY:
source=new CMSProfile(GreyGamma,srcwp);
break;
default:
throw "Only RGB and Greyscale images are currently supported";
}
if(!source)
throw "Can't create source profile";
CMSProfile dest(CMSPrimaries_Rec709,RGBGamma,dstwp);
CMSTransform transform(source,&dest,LCMSWRAPPER_INTENT_ABSOLUTE_COLORIMETRIC);
is=new ImageSource_CMS(is,&transform);
delete source;
TIFFSaver ts(argv[3],is);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
#endif
#if 0
if(argc==5)
{
ImageSource *is=ISLoadImage(argv[1]);
int w=atoi(argv[2]);
int h=atoi(argv[3]);
is=new ImageSource_Downsample(is,w,h);
TIFFSaver ts(argv[4],is);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
if(argc==4)
{
double pct=atof(argv[2]);
ImageSource *is=ISLoadImage(argv[1]);
cerr << "Original size: " << is->width << " by " << is->height << endl;
is=new ImageSource_Downsample(is,int((is->width*pct)/100.0),int((is->height*pct)/100.0));
TIFFSaver ts(argv[3],is);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
if(argc==6)
{
double radius=atof(argv[2]);
double amount=atof(argv[3]);
double threshold=atof(argv[4]);
ImageSource *is=ISLoadImage(argv[1]);
// ConvKernel_Gaussian kernel(radius);
// ConvKernel_UnsharpMask kernel(radius,amount);
// kernel.Normalize();
// is=new ImageSource_Convolution(is,&kernel);
is=new ImageSource_UnsharpMask(is,radius,amount,threshold);
TIFFSaver ts(argv[5],is);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
if(argc==3)
{
ImageSource *is=ISLoadImage(argv[1]);
is=new ImageSource_GaussianBlur(is,5);
TIFFSaver ts(argv[2],is,false);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
if(argc==4)
{
ImageSource *is=ISLoadImage(argv[1]);
is=new ImageSource_GaussianBlur(is,atof(argv[2]));
TIFFSaver ts(argv[3],is,false);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
if(argc==5)
{
ImageSource *is=ISLoadImage(argv[1]);
// is=new ImageSource_Downsample(is,(is->width*95)/100,(is->height*95)/100);
// is=new ImageSource_LanczosSinc(is,atoi(argv[2]),atoi(argv[3]));
int w=atoi(argv[2]);
int h=atoi(argv[3]);
float radius=float(is->width)/w;
// cerr << "Using Gaussian radius of: " << radius << endl;
// is=new ImageSource_GaussianBlur(is,radius);
is=ISScaleImageBySize(is,w,h);
TIFFSaver ts(argv[4],is,false);
ProgressText p;
ts.SetProgress(&p);
ts.Save();
delete is;
}
if(argc==2)
{
// Rescue routine for CM screwup!
ImageSource *is=ISLoadImage(argv[1]);
int prevrow[3]={-1,-1,-1};
int row=0;
while(row<is->height)
{
ISDataType *rowptr;
do
{
rowptr=is->GetRow(row);
++row;
} while((row<is->height)
&& (rowptr[0]==prevrow[0])
&& (rowptr[1]==prevrow[1])
&& (rowptr[2]==prevrow[2]));
if(row<is->height)
{
cerr << "Unique row at " << row << endl;
prevrow[0]=rowptr[0];
prevrow[1]=rowptr[1];
prevrow[2]=rowptr[2];
int x=0;
int tmp[3]={-1,-1,-1};
while(x<is->width)
{
int s=x*is->samplesperpixel;
if(rowptr[s]!=tmp[0] || rowptr[s+1]!=tmp[1] || rowptr[s+2]!=tmp[2])
{
tmp[0]=rowptr[s];
tmp[1]=rowptr[s+1];
tmp[2]=rowptr[s+2];
float r=(100.0 * tmp[0])/IS_SAMPLEMAX;
float g=(100.0 * tmp[1])/IS_SAMPLEMAX;
float b=(100.0 * tmp[2])/IS_SAMPLEMAX;
cout << r << ", " << g << ", " << b << endl;
cerr << r << ", " << g << ", " << b << endl;
}
++x;
}
}
}
delete is;
}
}
catch(const char *err)
{
cerr << "Error: " << err << endl;
}
return(0);
}
#endif
int main(int argc,char **argv)
{
try
{
if(argc<2)
return(0);
ImageSource *is=ISLoadImage(argv[1]);
cerr << "File has " << is->samplesperpixel << " spp"<< std::endl;
for(int y=0;y<is->height;++y)
{
ISDataType *row=is->GetRow(y);
for(int x=0;x<is->width;++x)
{
ISDataType r,g,b;
r=*row++;
g=*row++;
b=*row++;
int out=(r&0xf800)|((g>>5)&0x0Fe0)|((b>>11)&0x1f);
putc(out>>8,stdout);
putc(out&255,stdout);
}
}
}
catch(const char *err)
{
cerr << "Error: " << err << endl;
}
return(0);
}