forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_image.cpp
2006 lines (1738 loc) · 55.9 KB
/
_image.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
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
/* -*- mode: c++; c-basic-offset: 4 -*- */
/* Python API mandates Python.h is included *first* */
#include "Python.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include "numpy/arrayobject.h"
#include "agg_color_rgba.h"
#include "agg_conv_transform.h"
#include "agg_image_accessors.h"
#include "agg_path_storage.h"
#include "agg_pixfmt_rgb.h"
#include "agg_pixfmt_rgba.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_renderer_scanline.h"
#include "agg_rendering_buffer.h"
#include "agg_scanline_bin.h"
#include "agg_scanline_bin.h"
#include "agg_scanline_u.h"
#include "agg_span_allocator.h"
#include "agg_span_image_filter_rgb.h"
#include "agg_span_image_filter_rgba.h"
#include "agg_span_interpolator_linear.h"
#include "agg_rasterizer_sl_clip.h"
#include "util/agg_color_conv_rgb8.h"
#include "_image.h"
#include "mplutils.h"
typedef agg::pixfmt_rgba32_plain pixfmt;
typedef agg::pixfmt_rgba32_pre pixfmt_pre;
typedef agg::renderer_base<pixfmt> renderer_base;
typedef agg::span_interpolator_linear<> interpolator_type;
typedef agg::rasterizer_scanline_aa<agg::rasterizer_sl_clip_dbl> rasterizer;
Image::Image() :
bufferIn(NULL), rbufIn(NULL), colsIn(0), rowsIn(0),
bufferOut(NULL), rbufOut(NULL), colsOut(0), rowsOut(0), BPP(4),
interpolation(BILINEAR), aspect(ASPECT_FREE), bg(1, 1, 1, 0), resample(true)
{
_VERBOSE("Image::Image");
}
Image::~Image()
{
_VERBOSE("Image::~Image");
delete [] bufferIn;
bufferIn = NULL;
delete rbufIn;
rbufIn = NULL;
delete rbufOut;
rbufOut = NULL;
delete [] bufferOut;
bufferOut = NULL;
}
int
Image::setattr(const char * name, const Py::Object & value)
{
_VERBOSE("Image::setattr");
__dict__[name] = value;
return 0;
}
Py::Object
Image::getattr(const char * name)
{
_VERBOSE("Image::getattro");
if (__dict__.hasKey(name)) return __dict__[name];
else return getattr_default(name);
}
char Image::apply_rotation__doc__[] =
"apply_rotation(angle)\n"
"\n"
"Apply the rotation (degrees) to image"
;
Py::Object
Image::apply_rotation(const Py::Tuple& args)
{
_VERBOSE("Image::apply_rotation");
args.verify_length(1);
double r = Py::Float(args[0]);
agg::trans_affine M = agg::trans_affine_rotation(r * agg::pi / 180.0);
srcMatrix *= M;
imageMatrix *= M;
return Py::Object();
}
char Image::flipud_out__doc__[] =
"flipud()\n"
"\n"
"Flip the output image upside down"
;
char Image::flipud_in__doc__[] =
"flipud()\n"
"\n"
"Flip the input image upside down"
;
Py::Object
Image::flipud_in(const Py::Tuple& args)
{
_VERBOSE("Image::flipud_in");
args.verify_length(0);
int stride = rbufIn->stride();
rbufIn->attach(bufferIn, colsIn, rowsIn, -stride);
return Py::Object();
}
char Image::set_bg__doc__[] =
"set_bg(r,g,b,a)\n"
"\n"
"Set the background color"
;
Py::Object
Image::set_bg(const Py::Tuple& args)
{
_VERBOSE("Image::set_bg");
args.verify_length(4);
bg.r = Py::Float(args[0]);
bg.g = Py::Float(args[1]);
bg.b = Py::Float(args[2]);
bg.a = Py::Float(args[3]);
return Py::Object();
}
char Image::apply_scaling__doc__[] =
"apply_scaling(sx, sy)\n"
"\n"
"Apply the scale factors sx, sy to the transform matrix"
;
Py::Object
Image::apply_scaling(const Py::Tuple& args)
{
_VERBOSE("Image::apply_scaling");
args.verify_length(2);
double sx = Py::Float(args[0]);
double sy = Py::Float(args[1]);
//printf("applying scaling %1.2f, %1.2f\n", sx, sy);
agg::trans_affine M = agg::trans_affine_scaling(sx, sy);
srcMatrix *= M;
imageMatrix *= M;
return Py::Object();
}
char Image::apply_translation__doc__[] =
"apply_translation(tx, ty)\n"
"\n"
"Apply the translation tx, ty to the transform matrix"
;
Py::Object
Image::apply_translation(const Py::Tuple& args)
{
_VERBOSE("Image::apply_translation");
args.verify_length(2);
double tx = Py::Float(args[0]);
double ty = Py::Float(args[1]);
//printf("applying translation %1.2f, %1.2f\n", tx, ty);
agg::trans_affine M = agg::trans_affine_translation(tx, ty);
srcMatrix *= M;
imageMatrix *= M;
return Py::Object();
}
char Image::as_rgba_str__doc__[] =
"numrows, numcols, s = as_rgba_str()"
"\n"
"Call this function after resize to get the data as string\n"
"The string is a numrows by numcols x 4 (RGBA) unsigned char buffer\n"
;
Py::Object
Image::as_rgba_str(const Py::Tuple& args, const Py::Dict& kwargs)
{
_VERBOSE("Image::as_rgba_str");
args.verify_length(0);
std::pair<agg::int8u*, bool> bufpair = _get_output_buffer();
#if PY3K
Py::Object ret = Py::asObject(Py_BuildValue("nny#", rowsOut, colsOut,
bufpair.first, colsOut * rowsOut * 4));
#else
Py::Object ret = Py::asObject(Py_BuildValue("nns#", rowsOut, colsOut,
bufpair.first, colsOut * rowsOut * 4));
#endif
if (bufpair.second) delete [] bufpair.first;
return ret;
}
char Image::color_conv__doc__[] =
"numrows, numcols, buffer = color_conv(format)"
"\n"
"format 0(BGRA) or 1(ARGB)\n"
"Convert image to format and return in a writable buffer\n"
;
Py::Object
Image::color_conv(const Py::Tuple& args)
{
_VERBOSE("Image::color_conv");
args.verify_length(1);
int format = Py::Int(args[0]);
PyObject* py_buffer = NULL;
int row_len = colsOut * 4;
#if PY3K
unsigned char* buf = (unsigned char *)malloc(row_len * rowsOut);
if (buf == NULL)
throw Py::MemoryError("Image::color_conv could not allocate memory");
#else
py_buffer = PyBuffer_New(row_len * rowsOut);
if (py_buffer == NULL)
throw Py::MemoryError("Image::color_conv could not allocate memory");
void* buf;
Py_ssize_t buffer_len;
int ret = PyObject_AsWriteBuffer(py_buffer, &buf, &buffer_len);
if (ret != 0)
{
Py_XDECREF(py_buffer);
throw Py::MemoryError("Image::color_conv could not allocate memory");
}
#endif
agg::rendering_buffer rtmp;
rtmp.attach(reinterpret_cast<unsigned char*>(buf), colsOut, rowsOut,
row_len);
switch (format)
{
case 0:
agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_bgra32());
break;
case 1:
agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_argb32());
break;
default:
Py_XDECREF(py_buffer);
throw Py::ValueError("Image::color_conv unknown format");
}
#if PY3K
py_buffer = PyByteArray_FromStringAndSize((char *)buf, row_len * rowsOut);
if (py_buffer == NULL) {
free(buf);
}
#endif
PyObject* o = Py_BuildValue("nnN", rowsOut, colsOut, py_buffer);
return Py::asObject(o);
}
char Image::buffer_rgba__doc__[] =
"buffer = buffer_rgba()"
"\n"
"Return the image buffer as rgba32\n"
;
Py::Object
Image::buffer_rgba(const Py::Tuple& args)
{
//"Return the image object as rgba";
_VERBOSE("RendererAgg::buffer_rgba");
args.verify_length(0);
int row_len = colsOut * 4;
PyObject* o = Py_BuildValue("nns#", rowsOut, colsOut,
rbufOut, row_len * rowsOut);
return Py::asObject(o);
}
char Image::reset_matrix__doc__[] =
"reset_matrix()"
"\n"
"Reset the transformation matrix"
;
Py::Object
Image::reset_matrix(const Py::Tuple& args)
{
_VERBOSE("Image::reset_matrix");
args.verify_length(0);
srcMatrix.reset();
imageMatrix.reset();
return Py::Object();
}
char Image::get_matrix__doc__[] =
"(m11,m21,m12,m22,m13,m23) = get_matrix()\n"
"\n"
"Get the affine transformation matrix\n"
" /m11,m12,m13\\\n"
" /m21,m22,m23|\n"
" \\ 0 , 0 , 1 /"
;
Py::Object
Image::get_matrix(const Py::Tuple& args)
{
_VERBOSE("Image::get_matrix");
args.verify_length(0);
double m[6];
srcMatrix.store_to(m);
Py::Tuple ret(6);
for (int i = 0;i < 6;i++)
{
ret[i] = Py::Float(m[i]);
}
return ret;
}
char Image::resize__doc__[] =
"resize(width, height, norm=1, radius=4.0)\n"
"\n"
"Resize the image to width, height using interpolation\n"
"norm and radius are optional args for some of the filters and must be\n"
"passed as kwargs\n"
;
Py::Object
Image::resize(const Py::Tuple& args, const Py::Dict& kwargs)
{
_VERBOSE("Image::resize");
args.verify_length(2);
int norm = 1;
if (kwargs.hasKey("norm"))
{
norm = Py::Int(kwargs["norm"]);
}
double radius = 4.0;
if (kwargs.hasKey("radius"))
{
radius = Py::Float(kwargs["radius"]);
}
if (bufferIn == NULL)
{
throw Py::RuntimeError("You must first load the image");
}
int numcols = Py::Int(args[0]);
int numrows = Py::Int(args[1]);
colsOut = numcols;
rowsOut = numrows;
size_t NUMBYTES(numrows * numcols * BPP);
delete [] bufferOut;
bufferOut = new agg::int8u[NUMBYTES];
if (bufferOut == NULL) //todo: also handle allocation throw
{
throw Py::MemoryError("Image::resize could not allocate memory");
}
delete rbufOut;
rbufOut = new agg::rendering_buffer;
rbufOut->attach(bufferOut, numcols, numrows, numcols * BPP);
// init the output rendering/rasterizing stuff
pixfmt pixf(*rbufOut);
renderer_base rb(pixf);
rb.clear(bg);
rasterizer ras;
agg::scanline_u8 sl;
ras.clip_box(0, 0, numcols, numrows);
//srcMatrix *= resizingMatrix;
//imageMatrix *= resizingMatrix;
imageMatrix.invert();
interpolator_type interpolator(imageMatrix);
typedef agg::span_allocator<agg::rgba8> span_alloc_type;
span_alloc_type sa;
// the image path
agg::path_storage path;
agg::rendering_buffer rbufPad;
double x0, y0, x1, y1;
x0 = 0.0;
x1 = colsIn;
y0 = 0.0;
y1 = rowsIn;
path.move_to(x0, y0);
path.line_to(x1, y0);
path.line_to(x1, y1);
path.line_to(x0, y1);
path.close_polygon();
agg::conv_transform<agg::path_storage> imageBox(path, srcMatrix);
ras.add_path(imageBox);
typedef agg::wrap_mode_reflect reflect_type;
typedef agg::image_accessor_wrap<pixfmt_pre, reflect_type, reflect_type> img_accessor_type;
pixfmt_pre pixfmtin(*rbufIn);
img_accessor_type ia(pixfmtin);
switch (interpolation)
{
case NEAREST:
{
typedef agg::span_image_filter_rgba_nn<img_accessor_type, interpolator_type> span_gen_type;
typedef agg::renderer_scanline_aa<renderer_base, span_alloc_type, span_gen_type> renderer_type;
span_gen_type sg(ia, interpolator);
renderer_type ri(rb, sa, sg);
agg::render_scanlines(ras, sl, ri);
}
break;
case HANNING:
case HAMMING:
case HERMITE:
{
agg::image_filter_lut filter;
switch (interpolation)
{
case HANNING:
filter.calculate(agg::image_filter_hanning(), norm);
break;
case HAMMING:
filter.calculate(agg::image_filter_hamming(), norm);
break;
case HERMITE:
filter.calculate(agg::image_filter_hermite(), norm);
break;
}
if (resample)
{
typedef agg::span_image_resample_rgba_affine<img_accessor_type> span_gen_type;
typedef agg::renderer_scanline_aa<renderer_base, span_alloc_type, span_gen_type> renderer_type;
span_gen_type sg(ia, interpolator, filter);
renderer_type ri(rb, sa, sg);
agg::render_scanlines(ras, sl, ri);
}
else
{
typedef agg::span_image_filter_rgba_2x2<img_accessor_type, interpolator_type> span_gen_type;
typedef agg::renderer_scanline_aa<renderer_base, span_alloc_type, span_gen_type> renderer_type;
span_gen_type sg(ia, interpolator, filter);
renderer_type ri(rb, sa, sg);
agg::render_scanlines(ras, sl, ri);
}
}
break;
case BILINEAR:
case BICUBIC:
case SPLINE16:
case SPLINE36:
case KAISER:
case QUADRIC:
case CATROM:
case GAUSSIAN:
case BESSEL:
case MITCHELL:
case SINC:
case LANCZOS:
case BLACKMAN:
{
agg::image_filter_lut filter;
switch (interpolation)
{
case BILINEAR:
filter.calculate(agg::image_filter_bilinear(), norm);
break;
case BICUBIC:
filter.calculate(agg::image_filter_bicubic(), norm);
break;
case SPLINE16:
filter.calculate(agg::image_filter_spline16(), norm);
break;
case SPLINE36:
filter.calculate(agg::image_filter_spline36(), norm);
break;
case KAISER:
filter.calculate(agg::image_filter_kaiser(), norm);
break;
case QUADRIC:
filter.calculate(agg::image_filter_quadric(), norm);
break;
case CATROM:
filter.calculate(agg::image_filter_catrom(), norm);
break;
case GAUSSIAN:
filter.calculate(agg::image_filter_gaussian(), norm);
break;
case BESSEL:
filter.calculate(agg::image_filter_bessel(), norm);
break;
case MITCHELL:
filter.calculate(agg::image_filter_mitchell(), norm);
break;
case SINC:
filter.calculate(agg::image_filter_sinc(radius), norm);
break;
case LANCZOS:
filter.calculate(agg::image_filter_lanczos(radius), norm);
break;
case BLACKMAN:
filter.calculate(agg::image_filter_blackman(radius), norm);
break;
}
if (resample)
{
typedef agg::span_image_resample_rgba_affine<img_accessor_type> span_gen_type;
typedef agg::renderer_scanline_aa<renderer_base, span_alloc_type, span_gen_type> renderer_type;
span_gen_type sg(ia, interpolator, filter);
renderer_type ri(rb, sa, sg);
agg::render_scanlines(ras, sl, ri);
}
else
{
typedef agg::span_image_filter_rgba<img_accessor_type, interpolator_type> span_gen_type;
typedef agg::renderer_scanline_aa<renderer_base, span_alloc_type, span_gen_type> renderer_type;
span_gen_type sg(ia, interpolator, filter);
renderer_type ri(rb, sa, sg);
agg::render_scanlines(ras, sl, ri);
}
}
break;
}
return Py::Object();
}
char Image::get_interpolation__doc__[] =
"get_interpolation()\n"
"\n"
"Get the interpolation scheme to one of the module constants, "
"one of image.NEAREST, image.BILINEAR, etc..."
;
Py::Object
Image::get_interpolation(const Py::Tuple& args)
{
_VERBOSE("Image::get_interpolation");
args.verify_length(0);
return Py::Int((int)interpolation);
}
char Image::get_aspect__doc__[] =
"get_aspect()\n"
"\n"
"Get the aspect constraint constants"
;
Py::Object
Image::get_aspect(const Py::Tuple& args)
{
_VERBOSE("Image::get_aspect");
args.verify_length(0);
return Py::Int((int)aspect);
}
char Image::get_size__doc__[] =
"numrows, numcols = get_size()\n"
"\n"
"Get the number or rows and columns of the input image"
;
Py::Object
Image::get_size(const Py::Tuple& args)
{
_VERBOSE("Image::get_size");
args.verify_length(0);
Py::Tuple ret(2);
ret[0] = Py::Int((long)rowsIn);
ret[1] = Py::Int((long)colsIn);
return ret;
}
char Image::get_resample__doc__[] =
"get_resample()\n"
"\n"
"Get the resample flag."
;
Py::Object
Image::get_resample(const Py::Tuple& args)
{
_VERBOSE("Image::get_resample");
args.verify_length(0);
return Py::Int((int)resample);
}
char Image::get_size_out__doc__[] =
"numrows, numcols = get_size()\n"
"\n"
"Get the number or rows and columns of the output image"
;
Py::Object
Image::get_size_out(const Py::Tuple& args)
{
_VERBOSE("Image::get_size_out");
args.verify_length(0);
Py::Tuple ret(2);
ret[0] = Py::Int((long)rowsOut);
ret[1] = Py::Int((long)colsOut);
return ret;
}
//get the output buffer, flipped if necessary. The second element of
//the pair is a bool that indicates whether you need to free the
//memory
std::pair<agg::int8u*, bool>
Image::_get_output_buffer()
{
_VERBOSE("Image::_get_output_buffer");
std::pair<agg::int8u*, bool> ret;
bool flipy = rbufOut->stride() < 0;
if (flipy)
{
agg::int8u* buffer = new agg::int8u[rowsOut*colsOut*4];
agg::rendering_buffer rb;
rb.attach(buffer, colsOut, rowsOut, colsOut*4);
rb.copy_from(*rbufOut);
ret.first = buffer;
ret.second = true;
}
else
{
ret.first = bufferOut;
ret.second = false;
}
return ret;
}
char Image::set_interpolation__doc__[] =
"set_interpolation(scheme)\n"
"\n"
"Set the interpolation scheme to one of the module constants, "
"eg, image.NEAREST, image.BILINEAR, etc..."
;
Py::Object
Image::set_interpolation(const Py::Tuple& args)
{
_VERBOSE("Image::set_interpolation");
args.verify_length(1);
size_t method = (long)Py::Int(args[0]);
interpolation = (unsigned)method;
return Py::Object();
}
char Image::set_resample__doc__[] =
"set_resample(boolean)\n"
"\n"
"Set the resample flag."
;
Py::Object
Image::set_resample(const Py::Tuple& args)
{
_VERBOSE("Image::set_resample");
args.verify_length(1);
int flag = Py::Int(args[0]);
resample = (bool)flag;
return Py::Object();
}
char Image::set_aspect__doc__[] =
"set_aspect(scheme)\n"
"\n"
"Set the aspect ration to one of the image module constant."
"eg, one of image.ASPECT_PRESERVE, image.ASPECT_FREE"
;
Py::Object
Image::set_aspect(const Py::Tuple& args)
{
_VERBOSE("Image::set_aspect");
args.verify_length(1);
size_t method = (long)Py::Int(args[0]);
aspect = (unsigned)method;
return Py::Object();
}
void
Image::init_type()
{
_VERBOSE("Image::init_type");
behaviors().name("Image");
behaviors().doc("Image");
behaviors().supportGetattr();
behaviors().supportSetattr();
add_varargs_method("apply_rotation", &Image::apply_rotation, Image::apply_rotation__doc__);
add_varargs_method("apply_scaling", &Image::apply_scaling, Image::apply_scaling__doc__);
add_varargs_method("apply_translation", &Image::apply_translation, Image::apply_translation__doc__);
add_keyword_method("as_rgba_str", &Image::as_rgba_str, Image::as_rgba_str__doc__);
add_varargs_method("color_conv", &Image::color_conv, Image::color_conv__doc__);
add_varargs_method("buffer_rgba", &Image::buffer_rgba, Image::buffer_rgba__doc__);
add_varargs_method("get_aspect", &Image::get_aspect, Image::get_aspect__doc__);
add_varargs_method("get_interpolation", &Image::get_interpolation, Image::get_interpolation__doc__);
add_varargs_method("get_resample", &Image::get_resample, Image::get_resample__doc__);
add_varargs_method("get_size", &Image::get_size, Image::get_size__doc__);
add_varargs_method("get_size_out", &Image::get_size_out, Image::get_size_out__doc__);
add_varargs_method("reset_matrix", &Image::reset_matrix, Image::reset_matrix__doc__);
add_varargs_method("get_matrix", &Image::get_matrix, Image::get_matrix__doc__);
add_keyword_method("resize", &Image::resize, Image::resize__doc__);
add_varargs_method("set_interpolation", &Image::set_interpolation, Image::set_interpolation__doc__);
add_varargs_method("set_resample", &Image::set_resample, Image::set_resample__doc__);
add_varargs_method("set_aspect", &Image::set_aspect, Image::set_aspect__doc__);
add_varargs_method("set_bg", &Image::set_bg, Image::set_bg__doc__);
add_varargs_method("flipud_out", &Image::flipud_out, Image::flipud_out__doc__);
add_varargs_method("flipud_in", &Image::flipud_in, Image::flipud_in__doc__);
}
char _image_module_from_images__doc__[] =
"from_images(numrows, numcols, seq)\n"
"\n"
"return an image instance with numrows, numcols from a seq of image\n"
"instances using alpha blending. seq is a list of (Image, ox, oy)"
;
Py::Object
_image_module::from_images(const Py::Tuple& args)
{
_VERBOSE("_image_module::from_images");
args.verify_length(3);
size_t numrows = (long)Py::Int(args[0]);
size_t numcols = (long)Py::Int(args[1]);
if (numrows >= 32768 || numcols >= 32768)
{
throw Py::RuntimeError("numrows and numcols must both be less than 32768");
}
Py::SeqBase<Py::Object> tups = args[2];
size_t N = tups.length();
if (N == 0)
{
throw Py::RuntimeError("Empty list of images");
}
Py::Tuple tup;
size_t ox(0), oy(0), thisx(0), thisy(0);
float alpha;
bool apply_alpha;
//copy image 0 output buffer into return images output buffer
Image* imo = new Image;
imo->rowsOut = numrows;
imo->colsOut = numcols;
size_t NUMBYTES(numrows * numcols * imo->BPP);
imo->bufferOut = new agg::int8u[NUMBYTES];
if (imo->bufferOut == NULL) //todo: also handle allocation throw
{
throw Py::MemoryError("_image_module::from_images could not allocate memory");
}
delete imo->rbufOut;
imo->rbufOut = new agg::rendering_buffer;
imo->rbufOut->attach(imo->bufferOut, imo->colsOut, imo->rowsOut, imo->colsOut * imo->BPP);
pixfmt pixf(*imo->rbufOut);
renderer_base rb(pixf);
rb.clear(agg::rgba(0, 0, 0, 0));
for (size_t imnum = 0; imnum < N; imnum++)
{
tup = Py::Tuple(tups[imnum]);
Image* thisim = static_cast<Image*>(tup[0].ptr());
ox = (long)Py::Int(tup[1]);
oy = (long)Py::Int(tup[2]);
if (tup.size() <= 3 || tup[3].ptr() == Py_None)
{
apply_alpha = false;
}
else
{
apply_alpha = true;
alpha = Py::Float(tup[3]);
}
bool isflip = (thisim->rbufOut->stride()) < 0;
//std::cout << "from images " << isflip << "; stride=" << thisim->rbufOut->stride() << std::endl;
size_t ind = 0;
for (size_t j = 0; j < thisim->rowsOut; j++)
{
for (size_t i = 0; i < thisim->colsOut; i++)
{
thisx = i + ox;
if (isflip)
{
thisy = thisim->rowsOut - j + oy;
}
else
{
thisy = j + oy;
}
if (thisx >= numcols || thisy >= numrows)
{
ind += 4;
continue;
}
pixfmt::color_type p;
p.r = *(thisim->bufferOut + ind++);
p.g = *(thisim->bufferOut + ind++);
p.b = *(thisim->bufferOut + ind++);
if (apply_alpha)
{
p.a = (pixfmt::value_type) *(thisim->bufferOut + ind++) * alpha;
}
else
{
p.a = *(thisim->bufferOut + ind++);
}
pixf.blend_pixel(thisx, thisy, p, 255);
}
}
}
return Py::asObject(imo);
}
char _image_module_fromarray__doc__[] =
"fromarray(A, isoutput)\n"
"\n"
"Load the image from a numpy array\n"
"By default this function fills the input buffer, which can subsequently\n"
"be resampled using resize. If isoutput=1, fill the output buffer.\n"
"This is used to support raw pixel images w/o resampling"
;
Py::Object
_image_module::fromarray(const Py::Tuple& args)
{
_VERBOSE("_image_module::fromarray");
args.verify_length(2);
Py::Object x = args[0];
int isoutput = Py::Int(args[1]);
PyArrayObject *A = (PyArrayObject *) PyArray_FromObject(x.ptr(), PyArray_DOUBLE, 2, 3);
if (A == NULL)
{
throw Py::ValueError("Array must be rank 2 or 3 of doubles");
}
Py::Object A_obj((PyObject *)A, true);
Image* imo = new Image;
imo->rowsIn = A->dimensions[0];
imo->colsIn = A->dimensions[1];
size_t NUMBYTES(imo->colsIn * imo->rowsIn * imo->BPP);
agg::int8u *buffer = new agg::int8u[NUMBYTES];
if (buffer == NULL) //todo: also handle allocation throw
{
throw Py::MemoryError("_image_module::fromarray could not allocate memory");
}
if (isoutput)
{
// make the output buffer point to the input buffer
imo->rowsOut = imo->rowsIn;
imo->colsOut = imo->colsIn;
imo->rbufOut = new agg::rendering_buffer;
imo->bufferOut = buffer;
imo->rbufOut->attach(imo->bufferOut, imo->colsOut, imo->rowsOut, imo->colsOut * imo->BPP);
}
else
{
imo->bufferIn = buffer;
imo->rbufIn = new agg::rendering_buffer;
imo->rbufIn->attach(buffer, imo->colsIn, imo->rowsIn, imo->colsIn*imo->BPP);
}
if (A->nd == 2) //assume luminance for now;
{
agg::int8u gray;
for (size_t rownum = 0; rownum < imo->rowsIn; rownum++)
{
for (size_t colnum = 0; colnum < imo->colsIn; colnum++)
{
double val = *(double *)(A->data + rownum * A->strides[0] + colnum * A->strides[1]);
gray = int(255 * val);
*buffer++ = gray; // red
*buffer++ = gray; // green
*buffer++ = gray; // blue
*buffer++ = 255; // alpha
}
}
}
else if (A->nd == 3) // assume RGB
{
if (A->dimensions[2] != 3 && A->dimensions[2] != 4)
{
throw Py::ValueError(Printf("3rd dimension must be length 3 (RGB) or 4 (RGBA); found %d", A->dimensions[2]).str());
}
int rgba = A->dimensions[2] == 4;
double r, g, b, alpha;
size_t offset = 0;
for (size_t rownum = 0; rownum < imo->rowsIn; rownum++)
{
for (size_t colnum = 0; colnum < imo->colsIn; colnum++)
{
offset = rownum * A->strides[0] + colnum * A->strides[1];
r = *(double *)(A->data + offset);
g = *(double *)(A->data + offset + A->strides[2]);
b = *(double *)(A->data + offset + 2 * A->strides[2]);
if (rgba)
{
alpha = *(double *)(A->data + offset + 3 * A->strides[2]);
}
else
{
alpha = 1.0;
}
*buffer++ = int(255 * r); // red
*buffer++ = int(255 * g); // green
*buffer++ = int(255 * b); // blue
*buffer++ = int(255 * alpha); // alpha
}
}
}
else // error
{
throw Py::ValueError("Illegal array rank; must be rank; must 2 or 3");
}
buffer -= NUMBYTES;
return Py::asObject(imo);
}
char _image_module_fromarray2__doc__[] =
"fromarray2(A, isoutput)\n"