-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathimage.c
2707 lines (2261 loc) · 70.6 KB
/
image.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
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 4; -*-
*
* image.c
*
* Copyright (c) 2003 Alexandre Pigolkine
* Copyright (C) 2007 Novell, Inc (http://www.novell.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Alexandre Pigolkine([email protected])
* Sanjay Gupta ([email protected])
* Vladimir Vukicevic ([email protected])
* Jordi Mas ([email protected])
* Jonathan Gilbert ([email protected])
* Sebastien Pouliot <[email protected]>
*/
#include "gdiplus-private.h"
#include "image-private.h"
#include "imageattributes-private.h"
#include "general-private.h"
#include "graphics-private.h"
#include "matrix.h"
#include "metafile-private.h"
#include "bmpcodec.h"
#include "pngcodec.h"
#include "jpegcodec.h"
#include "gifcodec.h"
#include "tiffcodec.h"
#include "icocodec.h"
#include "emfcodec.h"
#include "wmfcodec.h"
/*
* format guids
*/
extern GUID gdip_bmp_image_format_guid;
extern GUID gdip_jpg_image_format_guid;
extern GUID gdip_png_image_format_guid;
extern GUID gdip_gif_image_format_guid;
extern GUID gdip_tif_image_format_guid;
extern GUID gdip_wmf_image_format_guid;
extern GUID gdip_emf_image_format_guid;
extern GUID gdip_ico_image_format_guid;
GUID gdip_exif_image_format_guid = {0xb96b3cb2U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e}};
GUID gdip_membmp_image_format_guid = {0xb96b3caaU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e}};
/*
* encoder param guids
*/
GUID GdipEncoderCompression = {0x0E09D739DU, 0x0CCD4U, 0x44EEU, {0x8E, 0x0BA, 0x3F, 0x0BF, 0x8B, 0x0E4, 0x0FC, 0x58}};
GUID GdipEncoderColorDepth = {0x66087055U, 0x0AD66U, 0x4C7CU, {0x9A, 0x18, 0x38, 0x0A2, 0x31, 0x0B, 0x83, 0x37}};
GUID GdipEncoderSaveFlag = {0x292266FCU, 0x0AC40U, 0x47BFU, {0x8C, 0x0FC, 0x0A8, 0x5B, 0x89, 0x0A6, 0x55, 0x0DE}};
GUID GdipEncoderSaveAsCMYK = {0x0A219BBC9U, 0x0A9DU, 0x4005U, {0x0A3, 0x0EE, 0x3A, 0x42, 0x1B, 0x8B, 0x0B0, 0x6C}};
GUID GdipEncoderImageItems = {0x63875E13U, 0x1F1DU, 0x45ABU, {0x91, 0x95, 0x0A2, 0x9B, 0x60, 0x66, 0x0A6, 0x50}};
GUID GdipEncoderTransformation = {0x8D0EB2D1U, 0x0A58EU, 0x4EA8U, {0x0AA, 0x14, 0x10, 0x80, 0x74, 0x0B7, 0x0B6, 0x0F9}};
GUID GdipEncoderQuality = {0x1D5BE4B5U, 0x0FA4AU, 0x452DU, {0x9C, 0x0DD, 0x5D, 0x0B3, 0x51, 0x5, 0x0E7, 0x0EB}};
GUID GdipEncoderLuminanceTable = {0x0EDB33BCEU, 0x266U, 0x4A77U, {0x0B9, 0x4, 0x27, 0x21, 0x60, 0x99, 0x0E7, 0x17}};
GUID GdipEncoderChrominanceTable = {0x0F2E455DCU, 0x9B3U, 0x4316U, {0x82, 0x60, 0x67, 0x6A, 0x0DA, 0x32, 0x48, 0x1C}};
#define DECODERS_SUPPORTED 8
#define ENCODERS_SUPPORTED 5
static BYTE *g_decoder_list;
static int g_decoders = 0;
static BYTE *g_encoder_list;
static int g_encoders = 0;
static ImageFormat
gdip_image_format_for_format_guid (GDIPCONST GUID *formatGUID)
{
if (memcmp (formatGUID, &gdip_bmp_image_format_guid, sizeof (GUID)) == 0)
return BMP;
if (memcmp (formatGUID, &gdip_jpg_image_format_guid, sizeof (GUID)) == 0)
return JPEG;
if (memcmp (formatGUID, &gdip_png_image_format_guid, sizeof (GUID)) == 0)
return PNG;
if (memcmp (formatGUID, &gdip_gif_image_format_guid, sizeof (GUID)) == 0)
return GIF;
if (memcmp (formatGUID, &gdip_tif_image_format_guid, sizeof (GUID)) == 0)
return TIF;
if (memcmp (formatGUID, &gdip_exif_image_format_guid, sizeof (GUID)) == 0)
return EXIF;
if (memcmp (formatGUID, &gdip_wmf_image_format_guid, sizeof (GUID)) == 0)
return WMF;
if (memcmp (formatGUID, &gdip_emf_image_format_guid, sizeof (GUID)) == 0)
return EMF;
if (memcmp (formatGUID, &gdip_ico_image_format_guid, sizeof (GUID)) == 0)
return ICON;
if (memcmp (formatGUID, &gdip_membmp_image_format_guid, sizeof (GUID)) == 0)
return PNG; /* MemoryBmp is saved as PNG */
return INVALID;
}
/* hack #1 - nonplaceable WMF metafiles are supported by no signature match them in the codecs */
static const BYTE nonplaceable_wmf_sig_pattern[] = { 0x01, 0x00, 0x09, 0x00, 0x00, 0x03 };
static const BYTE nonplaceable_wmf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const int nonplaceable_wmf_sig_count = 1;
static const int nonplaceable_wmf_sig_size = 6;
static BOOL
signature_match (char *data, size_t data_size, int size, int count, BYTE* sig_pattern, BYTE* sig_mask)
{
if (data_size < size)
return FALSE;
int sig;
for (sig = 0; sig < (size * count); sig += size) {
BOOL match = TRUE;
int p;
for (p = 0; ((p < size) && (p < data_size)); p++) {
BYTE *mask = (BYTE*)(&sig_mask [sig]);
BYTE *pattern = (BYTE*)(&sig_pattern [sig]);
if ((data [p] & mask[p]) != pattern[p]) {
match = FALSE;
break;
}
}
if (match)
return TRUE;
}
return FALSE;
}
static ImageFormat
get_image_format (char *sig_read, size_t size_read, ImageFormat *final)
{
ImageCodecInfo *decoder = (ImageCodecInfo*)g_decoder_list;
int index;
/* look at every decoder available, this will depends on how libgdiplus is compiled */
for (index = 0; index < g_decoders; index++, decoder++) {
/* for each signature in the codec */
if (signature_match (sig_read, size_read, decoder->SigSize, decoder->SigCount, (BYTE*)&decoder->SigPattern[0],
(BYTE*)&decoder->SigMask[0])) {
*final = gdip_image_format_for_format_guid (&decoder->FormatID);
return *final;
}
}
/* last chance - GDI+ still detects some files even if they don't match the codec signatures */
/* [mis-]handle non-placeable metafiles are WMF but reported as EMF (see #81178 for a test case) */
if (signature_match (sig_read, size_read, nonplaceable_wmf_sig_size, nonplaceable_wmf_sig_count,
(BYTE*)&nonplaceable_wmf_sig_pattern[0], (BYTE*)&nonplaceable_wmf_sig_mask[0])) {
*final = EMF;
return WMF;
}
return INVALID;
}
/* Converts the given interpolation value to cairo_filter_t */
static cairo_filter_t
gdip_get_cairo_filter (InterpolationMode imode)
{
switch (imode) {
case InterpolationModeHighQuality:
case InterpolationModeHighQualityBilinear:
case InterpolationModeHighQualityBicubic:
return CAIRO_FILTER_BEST;
case InterpolationModeNearestNeighbor:
return CAIRO_FILTER_NEAREST;
case InterpolationModeBilinear:
return CAIRO_FILTER_BILINEAR;
case InterpolationModeBicubic:
return CAIRO_FILTER_GAUSSIAN;
case InterpolationModeLowQuality:
return CAIRO_FILTER_FAST;
case InterpolationModeDefault:
default:
return CAIRO_FILTER_GOOD;
}
}
void
gdip_image_init (GpImage *image)
{
gdip_bitmap_init(image);
}
static void
copy_pixel (BYTE *src, BYTE *trg, int size)
{
memcpy (trg, src, size);
}
static GpStatus
gdip_flip_x (GpImage *image)
{
BYTE *src;
BYTE *line;
int stride;
int width;
int height;
int pixel_size;
int i;
int j;
stride = image->active_bitmap->stride;
width = image->active_bitmap->width;
height = image->active_bitmap->height;
pixel_size = gdip_get_pixel_format_components (image->active_bitmap->pixel_format) * gdip_get_pixel_format_depth (image->active_bitmap->pixel_format) / 8;
line = GdipAlloc (stride);
if (!line) {
return OutOfMemory;
}
src = (BYTE *) image->active_bitmap->scan0;
for (i = 0; i < height; i++, src += stride) {
memcpy (line, src, stride); /* Save original line */
for (j = 0; j < width; j++) {
copy_pixel(&line[(width - j - 1) * pixel_size], &src[j * pixel_size], pixel_size);
}
}
GdipFree (line);
return Ok;
}
static GpStatus
gdip_flip_y (GpImage *image)
{
BYTE *src;
BYTE *trg;
BYTE *line;
int stride;
int height;
int i;
stride = image->active_bitmap->stride;
height = image->active_bitmap->height;
line = GdipAlloc (stride);
if (!line) {
return OutOfMemory;
}
src = (BYTE *) image->active_bitmap->scan0;
trg = (BYTE *) image->active_bitmap->scan0;
trg += (height-1) * stride;
for (i = 0; i < (height /2); i++, src += stride, trg -= stride) {
memcpy (line, trg, stride); /* Save target line*/
memcpy (trg, src, stride); /* Copy src to trg*/
memcpy (src, line, stride); /* Copy trg to src*/
}
GdipFree (line);
return Ok;
}
GpStatus WINGDIPAPI
GdipDisposeImage (GpImage *image)
{
if (!image)
return InvalidParameter;
switch (image->type) {
case ImageTypeBitmap:
return gdip_bitmap_dispose (image);
case ImageTypeMetafile:
return gdip_metafile_dispose ((GpMetafile*)image);
default: /* imageUndefined */
g_warning ("unknown image type couldn't be disposed, ptr = %p, type %d", (void*)image, image->type);
return Ok;
}
}
/* coverity[+alloc : arg-*1] */
GpStatus WINGDIPAPI
GdipGetImageGraphicsContext (GpImage *image, GpGraphics **graphics)
{
GpGraphics *gfx;
cairo_pattern_t *filter;
if (!image || !graphics)
return InvalidParameter;
/* This function isn't allowed on metafiles - unless we're recording */
if (image->type == ImageTypeMetafile) {
GpMetafile *mf = (GpMetafile*)image;
if (!mf->recording)
return OutOfMemory;
*graphics = gdip_metafile_graphics_new (mf);
return *graphics ? Ok : OutOfMemory;
}
if (!image->active_bitmap)
return InvalidParameter;
/*
* Microsoft GDI+ only supports these pixel formats Format24bppRGB, Format32bppARGB,
* Format32bppPARGB, Format32bppRGB, Format48bppRGB, Format64bppARGB, Format64bppPARGB
* but we're limited to 24/32 inside libgdiplus
*/
switch (image->active_bitmap->pixel_format) {
case PixelFormat24bppRGB:
case PixelFormat32bppARGB:
case PixelFormat32bppPARGB:
case PixelFormat32bppRGB:
break;
default:
return OutOfMemory;
}
if (gdip_bitmap_ensure_surface (image) == NULL)
return OutOfMemory;
gfx = gdip_graphics_new (image->surface);
if (!gfx)
return OutOfMemory;
gfx->dpi_x = image->active_bitmap->dpi_horz <= 0 ? gdip_get_display_dpi () : image->active_bitmap->dpi_horz;
gfx->dpi_y = image->active_bitmap->dpi_vert <= 0 ? gdip_get_display_dpi () : image->active_bitmap->dpi_vert;
gfx->bounds.Width = image->active_bitmap->width;
gfx->bounds.Height = image->active_bitmap->height;
gfx->orig_bounds.Width = image->active_bitmap->width;
gfx->orig_bounds.Height = image->active_bitmap->height;
gfx->image = image;
gfx->type = gtMemoryBitmap;
filter = cairo_pattern_create_for_surface (image->surface);
cairo_pattern_set_filter (filter, gdip_get_cairo_filter (gfx->interpolation));
cairo_pattern_destroy (filter);
*graphics = gfx;
return Ok;
}
GpStatus WINGDIPAPI
GdipDrawImageI (GpGraphics *graphics, GpImage *image, INT x, INT y)
{
return GdipDrawImage (graphics, image, x, y);
}
GpStatus WINGDIPAPI
GdipDrawImage (GpGraphics *graphics, GpImage *image, REAL x, REAL y)
{
if (!graphics)
return InvalidParameter;
if (graphics->state == GraphicsStateBusy)
return ObjectBusy;
if (!image)
return InvalidParameter;
switch (image->type) {
case ImageTypeBitmap: {
ActiveBitmapData *data = image->active_bitmap;
return GdipDrawImageRect (graphics, image, x, y, data->width, data->height);
}
case ImageTypeMetafile: {
MetafileHeader *metaheader = &((GpMetafile*)image)->metafile_header;
return GdipDrawImageRect (graphics, image, x, y, metaheader->Width, metaheader->Height);
}
default:
return InvalidParameter;
}
}
GpStatus WINGDIPAPI
GdipDrawImageRectI (GpGraphics *graphics, GpImage *image, INT x, INT y, INT width, INT height)
{
return GdipDrawImageRect (graphics, image, x, y, width, height);
}
GpStatus WINGDIPAPI
GdipDrawImageRect (GpGraphics *graphics, GpImage *image, REAL x, REAL y, REAL width, REAL height)
{
cairo_pattern_t *pattern;
cairo_pattern_t *org_pattern;
MetafilePlayContext *metacontext = NULL;
BOOL need_scaling = FALSE;
double scaled_width, scaled_height;
cairo_matrix_t orig_matrix;
if (!graphics)
return InvalidParameter;
if (graphics->state == GraphicsStateBusy)
return ObjectBusy;
if (!image)
return InvalidParameter;
if (image->type == ImageTypeBitmap) {
/* check does not apply to metafiles, and it's better be done before converting the image */
if (gdip_is_overflow(x) || gdip_is_overflow(y))
return ValueOverflow;
if (gdip_is_an_indexed_pixelformat (image->active_bitmap->pixel_format)) {
GpStatus status = OutOfMemory;
GpBitmap *rgb_bitmap = gdip_convert_indexed_to_rgb (image);
if (rgb_bitmap) {
status = GdipDrawImageRect (graphics, rgb_bitmap, x, y, width, height);
GdipDisposeImage (rgb_bitmap);
}
return status;
}
}
if ((width == 0) || (height == 0))
return Ok;
/* conversion must be done after the recursive call to GdipDrawImageRect to remove the indexed bitmap */
if (!OPTIMIZE_CONVERSION (graphics)) {
x = gdip_unitx_convgr (graphics, x);
y = gdip_unity_convgr (graphics, y);
width = gdip_unitx_convgr (graphics, width);
height = gdip_unity_convgr (graphics, height);
}
cairo_new_path (graphics->ct);
/* metafile */
if (image->type == ImageTypeMetafile) {
GpStatus status;
metacontext = gdip_metafile_play_setup ((GpMetafile*)image, graphics, x, y, width, height);
status = gdip_metafile_play (metacontext);
gdip_metafile_play_cleanup (metacontext);
return status;
}
/* Create a surface for this bitmap if one doesn't exist */
if (gdip_bitmap_ensure_surface (image) == NULL)
return OutOfMemory;
if (width != image->active_bitmap->width || height != image->active_bitmap->height) {
scaled_width = (double) width / image->active_bitmap->width;
scaled_height = (double) height / image->active_bitmap->height;
need_scaling = TRUE;
}
/* Use the image->surface as a pattern */
pattern = cairo_pattern_create_for_surface (image->surface);
cairo_pattern_set_filter (pattern, gdip_get_cairo_filter (graphics->interpolation));
cairo_get_matrix (graphics->ct, &orig_matrix);
cairo_translate (graphics->ct, x, y);
if (need_scaling)
cairo_scale (graphics->ct, scaled_width, scaled_height);
org_pattern = cairo_get_source(graphics->ct);
cairo_pattern_reference(org_pattern);
cairo_set_source (graphics->ct, pattern);
cairo_identity_matrix (graphics->ct);
cairo_paint (graphics->ct);
cairo_set_source (graphics->ct, org_pattern);
cairo_set_matrix (graphics->ct, &orig_matrix);
cairo_pattern_destroy (org_pattern);
cairo_pattern_destroy (pattern);
return Ok;
}
/* points are upper left, upper right, lower left. fourth point is extrapolated. */
GpStatus WINGDIPAPI
GdipDrawImagePoints (GpGraphics *graphics, GpImage *image, GDIPCONST GpPointF *dstPoints, INT count)
{
cairo_pattern_t *pattern;
cairo_pattern_t *org_pattern;
GpMatrix *matrix = NULL;
cairo_matrix_t orig_matrix;
GpRectF tRect;
MetafilePlayContext *metacontext = NULL;
if (!graphics || !dstPoints || count <= 0)
return InvalidParameter;
if (graphics->state == GraphicsStateBusy)
return ObjectBusy;
if (!image || (count != 3 && count != 4))
return InvalidParameter;
if (count == 4)
return NotImplemented;
cairo_new_path (graphics->ct);
if (image->type == ImageTypeBitmap) {
if (gdip_is_an_indexed_pixelformat (image->active_bitmap->pixel_format)) {
GpStatus status = OutOfMemory;
GpBitmap *rgb_bitmap = gdip_convert_indexed_to_rgb (image);
if (rgb_bitmap) {
status = GdipDrawImagePoints (graphics, rgb_bitmap, dstPoints, count);
GdipDisposeImage(rgb_bitmap);
}
return status;
}
tRect.Width = image->active_bitmap->width;
tRect.Height = image->active_bitmap->height;
} else {
MetafileHeader *metaheader = &((GpMetafile*)image)->metafile_header;
tRect.Width = metaheader->Width;
tRect.Height = metaheader->Height;
}
tRect.X = 0;
tRect.Y = 0;
GdipCreateMatrix3 (&tRect, dstPoints, &matrix);
/* metafile */
if (image->type == ImageTypeMetafile) {
GpStatus status;
metacontext = gdip_metafile_play_setup ((GpMetafile*)image, graphics, tRect.X, tRect.Y, tRect.Width, tRect.Height);
cairo_get_matrix (graphics->ct, &orig_matrix);
gdip_cairo_set_matrix (graphics, matrix);
status = gdip_metafile_play (metacontext);
GdipDeleteMatrix (matrix);
gdip_metafile_play_cleanup (metacontext);
return status;
}
/* Create a surface for this bitmap if one doesn't exist */
if (gdip_bitmap_ensure_surface (image) == NULL) {
GdipDeleteMatrix(matrix);
return OutOfMemory;
}
pattern = cairo_pattern_create_for_surface (image->surface);
cairo_pattern_set_filter (pattern, gdip_get_cairo_filter (graphics->interpolation));
org_pattern = cairo_get_source(graphics->ct);
cairo_pattern_reference(org_pattern);
cairo_get_matrix(graphics->ct, &orig_matrix);
gdip_cairo_set_matrix (graphics, matrix);
cairo_set_source_surface (graphics->ct, image->surface, 0, 0);
cairo_paint (graphics->ct);
cairo_set_source(graphics->ct, org_pattern);
cairo_set_matrix (graphics->ct, &orig_matrix);
GdipDeleteMatrix (matrix);
cairo_pattern_destroy (org_pattern);
cairo_pattern_destroy (pattern);
return Ok;
}
GpStatus WINGDIPAPI
GdipDrawImagePointsI (GpGraphics *graphics, GpImage *image, GDIPCONST GpPoint *dstPoints, INT count)
{
GpStatus status;
GpPointF *pointsF;
if (!dstPoints || count < 0)
return InvalidParameter;
pointsF = convert_points (dstPoints, count);
if (!pointsF)
return OutOfMemory;
status = GdipDrawImagePoints (graphics, image, pointsF, count);
GdipFree (pointsF);
return status;
}
GpStatus WINGDIPAPI
GdipDrawImagePointRect (GpGraphics *graphics, GpImage *image, REAL x, REAL y, REAL srcx, REAL srcy,
REAL srcwidth, REAL srcheight, GpUnit srcUnit)
{
return GdipDrawImageRectRect (graphics, image, x, y, srcwidth, srcheight, srcx, srcy,
srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
}
GpStatus WINGDIPAPI
GdipDrawImagePointRectI (GpGraphics *graphics, GpImage *image, INT x, INT y, INT srcx, INT srcy,
INT srcwidth, INT srcheight, GpUnit srcUnit)
{
return GdipDrawImagePointRect (graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
}
GpStatus WINGDIPAPI
GdipDrawImageRectRect (GpGraphics *graphics, GpImage *image,
REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight,
REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
GpUnit srcUnit,
GDIPCONST GpImageAttributes *imageAttributes,
DrawImageAbort callback, void *callbackData)
{
GpStatus status;
cairo_pattern_t *pattern;
cairo_pattern_t *orig;
cairo_matrix_t mat;
GpBitmap *preprocessed_image = NULL;
if (!graphics)
return InvalidParameter;
if (graphics->state == GraphicsStateBusy)
return ObjectBusy;
if (!image)
return InvalidParameter;
switch (srcUnit) {
case UnitPixel:
break;
case UnitPoint:
case UnitInch:
case UnitDocument:
case UnitMillimeter:
if (graphics->type != gtPostScript)
return NotImplemented; /* GDI+ returns the same */
break;
case UnitWorld:
case UnitDisplay:
default:
return InvalidParameter;
}
if (image->type == ImageTypeBitmap) {
if (gdip_is_an_indexed_pixelformat (image->active_bitmap->pixel_format)) {
GpBitmap *rgb_bitmap = gdip_convert_indexed_to_rgb (image);
if (!rgb_bitmap)
return OutOfMemory;
status = GdipDrawImageRectRect (graphics, rgb_bitmap,
dstx, dsty, dstwidth, dstheight,
srcx, srcy, srcwidth, srcheight,
srcUnit, imageAttributes, callback, callbackData);
GdipDisposeImage (rgb_bitmap);
return status;
}
} else {
/* metafile support */
return NotImplemented;
}
/* see OPTIMIZE_CONVERSION in general.h */
if (srcUnit != UnitPixel && graphics->type != gtPostScript) {
dstx = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_x, graphics->type, dstx);
dsty = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_y, graphics->type, dsty);
dstwidth = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_x, graphics->type, dstwidth);
dstheight = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_y, graphics->type, dstheight);
srcx = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_x, graphics->type, srcx);
srcy = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_y, graphics->type, srcy);
srcwidth = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_x, graphics->type, srcwidth);
srcheight = gdip_unit_conversion (srcUnit, UnitCairoPoint, graphics->dpi_y, graphics->type, srcheight);
}
if (gdip_near_zero (srcwidth) || gdip_near_zero (dstwidth) ||
gdip_near_zero (srcheight) || gdip_near_zero (dstheight)) {
return Ok;
}
status = gdip_process_bitmap_attributes (image, (GpImageAttributes *) imageAttributes, &preprocessed_image);
if (status != Ok) {
return status;
}
if (preprocessed_image == NULL) {
preprocessed_image = (GpBitmap *) image;
}
cairo_matrix_init (&mat, 1, 0, 0, 1, 0, 0);
if (imageAttributes && imageAttributes->wrapmode != WrapModeClamp) {
float posx;
float posy;
BOOL flipXOn = (imageAttributes->wrapmode == WrapModeTileFlipX);
BOOL flipYOn = (imageAttributes->wrapmode == WrapModeTileFlipY);
BOOL flipX = FALSE;
BOOL flipY = FALSE;
GpBitmap *imgflipX = NULL;
GpBitmap *imgflipY = NULL;
GpBitmap *imgflipXY = NULL;
float img_width = preprocessed_image->active_bitmap->width * (dstwidth / srcwidth);
float img_height = preprocessed_image->active_bitmap->height * (dstheight / srcheight);
cairo_surface_t *cur_surface;
if (imageAttributes->wrapmode == WrapModeTileFlipXY) {
flipXOn = flipYOn = TRUE;
}
if (flipXOn) {
/* We're ok just cloning the bitmap, we don't need the image data
* and we destroy it before we leave this function */
status = gdip_bitmap_clone (preprocessed_image, &imgflipX);
if (status != Ok) {
gdip_bitmap_dispose(imgflipX);
return status;
}
status = gdip_flip_x (imgflipX);
if (status != Ok) {
gdip_bitmap_dispose (imgflipX);
return status;
}
gdip_bitmap_ensure_surface (imgflipX);
}
if (flipYOn) {
status = gdip_bitmap_clone (preprocessed_image, &imgflipY);
if (status != Ok) {
gdip_bitmap_dispose(imgflipX);
gdip_bitmap_dispose(imgflipY);
return status;
}
status = gdip_flip_y (imgflipY);
if (status != Ok) {
gdip_bitmap_dispose (imgflipX);
gdip_bitmap_dispose (imgflipY);
return status;
}
gdip_bitmap_ensure_surface (imgflipY);
}
if (flipXOn && flipYOn) {
status = gdip_bitmap_clone (preprocessed_image, &imgflipXY);
if (status != Ok) {
gdip_bitmap_dispose(imgflipX);
gdip_bitmap_dispose(imgflipY);
gdip_bitmap_dispose(imgflipXY);
return status;
}
status = gdip_flip_x (imgflipXY);
if (status != Ok) {
gdip_bitmap_dispose(imgflipX);
gdip_bitmap_dispose(imgflipY);
gdip_bitmap_dispose(imgflipXY);
return status;
}
status = gdip_flip_y (imgflipXY);
if (status != Ok) {
gdip_bitmap_dispose(imgflipX);
gdip_bitmap_dispose(imgflipY);
gdip_bitmap_dispose(imgflipXY);
return status;
}
gdip_bitmap_ensure_surface (imgflipXY);
}
gdip_bitmap_ensure_surface (preprocessed_image);
for (posy = 0; posy < dstheight; posy += img_height) {
for (posx = 0; posx < dstwidth; posx += img_width) {
if (flipX && flipY) {
cur_surface = imgflipXY->surface;
} else {
if (flipX) {
cur_surface = imgflipX->surface;
} else {
if (flipY) {
cur_surface = imgflipY->surface;
} else {
cur_surface = preprocessed_image->surface;
}
}
}
cairo_matrix_translate (&mat, srcx, srcy);
cairo_matrix_scale (&mat, srcwidth / dstwidth, srcheight / dstheight);
cairo_matrix_translate (&mat, - (dstx + posx), - (dsty + posy));
pattern = cairo_pattern_create_for_surface (cur_surface);
cairo_pattern_set_matrix (pattern, &mat);
orig = cairo_get_source (graphics->ct);
cairo_pattern_reference (orig);
cairo_set_source (graphics->ct, pattern);
cairo_rectangle (graphics->ct, dstx + posx, dsty + posy, img_width, img_height);
cairo_fill (graphics->ct);
cairo_set_source (graphics->ct, orig);
cairo_matrix_init_identity (&mat);
cairo_pattern_set_matrix (pattern, &mat);
cairo_pattern_destroy (orig);
cairo_pattern_destroy (pattern);
if (flipXOn) {
flipX = !flipX;
}
}
if (flipYOn) {
flipY = !flipY;
}
}
if (imgflipX) {
GdipDisposeImage ((GpImage *) imgflipX);
}
if (imgflipY) {
GdipDisposeImage ((GpImage *) imgflipY);
}
if (imgflipXY) {
GdipDisposeImage ((GpImage *) imgflipXY);
}
} else {
cairo_pattern_t *filter;
gdip_bitmap_ensure_surface (preprocessed_image);
filter = cairo_pattern_create_for_surface (preprocessed_image->surface);
cairo_pattern_set_filter (filter, gdip_get_cairo_filter (graphics->interpolation));
cairo_matrix_translate (&mat, srcx, srcy);
if (!gdip_near_zero(srcwidth - dstwidth) && !gdip_near_zero(srcheight - dstheight))
cairo_matrix_scale (&mat, srcwidth / dstwidth, srcheight / dstheight);
cairo_matrix_translate (&mat, -dstx, -dsty);
pattern = cairo_pattern_create_for_surface (preprocessed_image->surface);
cairo_pattern_set_matrix (pattern, &mat);
g_assert (cairo_status (graphics->ct) == CAIRO_STATUS_SUCCESS);
orig = cairo_get_source (graphics->ct);
cairo_pattern_reference (orig);
cairo_set_source (graphics->ct, pattern);
cairo_rectangle (graphics->ct, dstx, dsty, dstwidth, dstheight);
cairo_fill (graphics->ct);
cairo_set_source (graphics->ct, orig);
cairo_pattern_destroy (orig);
cairo_matrix_init_identity (&mat);
cairo_pattern_set_matrix (pattern, &mat);
cairo_pattern_destroy (pattern);
cairo_pattern_destroy (filter);
}
if (preprocessed_image != image) {
GdipDisposeImage ((GpImage *) preprocessed_image);
}
return Ok;
}
GpStatus WINGDIPAPI
GdipDrawImageRectRectI (GpGraphics *graphics, GpImage *image, INT dstx, INT dsty, INT dstwidth, INT dstheight,
INT srcx, INT srcy, INT srcwidth, INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes *imageAttributes,
DrawImageAbort callback, void *callbackData)
{
return GdipDrawImageRectRect (graphics, image, dstx, dsty, dstwidth, dstheight,
srcx, srcy, srcwidth, srcheight,
srcUnit,
imageAttributes, callback, callbackData);
}
GpStatus WINGDIPAPI
GdipDrawImagePointsRect (GpGraphics *graphics, GpImage *image, GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy,
REAL srcwidth, REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes *imageAttributes, DrawImageAbort callback,
void *callbackData)
{
GpRectF rect;
GpStatus status;
GpMatrix *matrix = NULL;
cairo_matrix_t orig_matrix;
if (!graphics || !points || count <= 0)
return InvalidParameter;
if (graphics->state == GraphicsStateBusy)
return ObjectBusy;
if (!image || (count != 3 && count != 4))
return InvalidParameter;
switch (srcUnit) {
case UnitPixel:
break;
case UnitPoint:
case UnitInch:
case UnitDocument:
case UnitMillimeter:
if (graphics->type != gtPostScript)
return NotImplemented; /* GDI+ returns the same */
break;
case UnitWorld:
case UnitDisplay:
default:
return InvalidParameter;
}
if (count == 4)
return NotImplemented;
/* Short circuit empty destination rectangle to avoid creating non-invertible matrix */
if (points[2].X + points[1].X - points[0].X - points[0].X == 0 &&
points[2].Y + points[1].Y - points[0].Y - points[0].Y == 0) {
return Ok;
}
rect.X = 0;
rect.Y = 0;
if (image->type == ImageTypeBitmap) {
rect.Width = image->active_bitmap->width;
rect.Height = image->active_bitmap->height;
} else {
MetafileHeader *metaheader = &((GpMetafile*)image)->metafile_header;
rect.Width = metaheader->Width;
rect.Height = metaheader->Height;
}
status = GdipCreateMatrix3 (&rect, points, &matrix);
if (status != Ok) {
GdipDeleteMatrix (matrix);
return status;
}
cairo_get_matrix (graphics->ct, &orig_matrix);
gdip_cairo_set_matrix (graphics, matrix);
g_assert (cairo_status (graphics->ct) == CAIRO_STATUS_SUCCESS);
status = GdipDrawImageRectRect (graphics, image, rect.X, rect.Y, rect.Width, rect.Height, srcx, srcy,
srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
cairo_set_matrix (graphics->ct, &orig_matrix);
GdipDeleteMatrix (matrix);
return status;
}
GpStatus WINGDIPAPI
GdipDrawImagePointsRectI (GpGraphics *graphics, GpImage *image, GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy,
INT srcwidth, INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes *imageAttributes, DrawImageAbort callback,
void *callbackData)
{
GpPointF *pointsF;
if (!points || count < 0)
return InvalidParameter;
pointsF = convert_points (points, count);
if (!pointsF)
return OutOfMemory;
GpStatus status = GdipDrawImagePointsRect(graphics, image, pointsF, count, srcx, srcy, srcwidth, srcheight,
srcUnit, imageAttributes, callback, callbackData);
GdipFree(pointsF);
return status;
}
/*
* These two will never be implemented, as 'stream' is a COM IStream
*/
GpStatus WINGDIPAPI
GdipLoadImageFromStream (void *stream, GpImage **image)
{
if (!gdiplusInitialized)
return GdiplusNotInitialized;
if (!stream || !image)
return InvalidParameter;
return NotImplemented; /* GdipLoadImageFromStream - not supported */
}
GpStatus WINGDIPAPI
GdipLoadImageFromStreamICM (void *stream, GpImage **image)
{
return GdipLoadImageFromStream (stream, image);
}
GpStatus WINGDIPAPI
GdipSaveImageToStream (GpImage *image, void *stream, GDIPCONST CLSID *encoderCLSID, GDIPCONST EncoderParameters *params)
{
if (!stream || !image)
return InvalidParameter;
return NotImplemented; /* GdipSaveImageToStream - not supported */
}
/* coverity[+alloc : arg-*1] */
GpStatus WINGDIPAPI
GdipLoadImageFromFile (GDIPCONST WCHAR *file, GpImage **image)
{
FILE *fp = NULL;
GpImage *result = NULL;
GpStatus status = Ok;