/
texturebrush.c
919 lines (752 loc) · 24.3 KB
/
texturebrush.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
/*
* texturebrush.c
*
* Copyright (C) 2003,2006-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:
* Ravindra (rkumar@novell.com)
* Sebastien Pouliot <sebastien@ximian.com>
*
* TODO
* reduce duplication between draw_tile*_texture functions
*/
#include "texturebrush-private.h"
#include "graphics-private.h"
#include "bitmap-private.h"
#include "matrix-private.h"
static GpStatus gdip_texture_setup (GpGraphics *graphics, GpBrush *brush);
static GpStatus gdip_texture_clone (GpBrush *brush, GpBrush **clonedBrush);
static GpStatus gdip_texture_destroy (GpBrush *brush);
/*
* we have a single copy of vtable for
* all instances of texturebrush.
*/
static BrushClass vtable = { BrushTypeTextureFill,
gdip_texture_setup,
gdip_texture_clone,
gdip_texture_destroy };
static cairo_content_t
from_cairoformat_to_content (cairo_format_t format)
{
switch (format) {
case CAIRO_FORMAT_RGB24:
return CAIRO_CONTENT_COLOR;
case CAIRO_FORMAT_A8:
return CAIRO_CONTENT_ALPHA;
case CAIRO_FORMAT_ARGB32:
default:
return CAIRO_CONTENT_COLOR_ALPHA;
}
}
static void
gdip_texture_init (GpTexture *texture)
{
gdip_brush_init (&texture->base, &vtable);
texture->wrapMode = WrapModeTile;
texture->rectangle.X = 0;
texture->rectangle.Y = 0;
texture->rectangle.Width = 0;
texture->rectangle.Height = 0;
texture->pattern = NULL;
cairo_matrix_init_identity (&texture->matrix);
}
static GpTexture*
gdip_texture_new (void)
{
GpTexture *result = (GpTexture *) GdipAlloc (sizeof (GpTexture));
if (result)
gdip_texture_init (result);
return result;
}
/*
* functions to create different wrapmodes.
*/
static GpStatus
draw_tile_texture (cairo_t *ct, GpBitmap *bitmap, GpTexture *brush)
{
cairo_surface_t *original = NULL;
cairo_surface_t *texture;
cairo_pattern_t *pat;
GpStatus status;
GpRect *rect = &brush->rectangle;
cairo_t *ct2;
BYTE *premul = NULL;
if (!rect)
return InvalidParameter;
gdip_bitmap_ensure_surface (bitmap);
if (gdip_bitmap_format_needs_premultiplication (bitmap)) {
premul = gdip_bitmap_get_premultiplied_scan0 (bitmap);
if (premul) {
BitmapData *data = bitmap->active_bitmap;
original = cairo_image_surface_create_for_data (premul, CAIRO_FORMAT_ARGB32,
data->width, data->height, data->stride);
}
}
/* if premul isn't required (or couldn't be computed, e.g. out of memory) */
if (!original)
original = bitmap->surface;
/* Use the original as a pattern */
pat = cairo_pattern_create_for_surface (original);
status = gdip_get_pattern_status (pat);
if (status != Ok)
goto cleanup;
cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
/* texture surface to be created */
texture = cairo_surface_create_similar (original, from_cairoformat_to_content (bitmap->cairo_format),
rect->Width, rect->Height);
status = gdip_get_status (cairo_surface_status (texture));
if (status != Ok) {
cairo_pattern_destroy (pat);
goto cleanup;
}
/* Draw the texture */
ct2 = cairo_create (texture);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
cairo_destroy (ct2);
brush->pattern = cairo_pattern_create_for_surface (texture);
status = gdip_get_pattern_status (brush->pattern);
if (status == Ok)
cairo_pattern_set_extend (brush->pattern, CAIRO_EXTEND_REPEAT);
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
status = gdip_get_status (cairo_status (ct));
cleanup:
if (premul) {
cairo_surface_destroy (original);
GdipFree (premul);
}
return status;
}
static GpStatus
draw_tile_flipX_texture (cairo_t *ct, GpBitmap *bitmap, GpTexture *brush)
{
cairo_surface_t *original = NULL;
cairo_surface_t *texture;
cairo_pattern_t *pat;
GpRect *rect = &brush->rectangle;
GpStatus status;
cairo_t *ct2;
cairo_matrix_t tempMatrix;
BYTE *premul = NULL;
if (!rect)
return InvalidParameter;
gdip_bitmap_ensure_surface (bitmap);
if (gdip_bitmap_format_needs_premultiplication (bitmap)) {
premul = gdip_bitmap_get_premultiplied_scan0 (bitmap);
if (premul) {
BitmapData *data = bitmap->active_bitmap;
original = cairo_image_surface_create_for_data (premul, CAIRO_FORMAT_ARGB32,
data->width, data->height, data->stride);
}
}
/* if premul isn't required (or couldn't be computed, e.g. out of memory) */
if (!original)
original = bitmap->surface;
/* Use the original as a pattern */
pat = cairo_pattern_create_for_surface (original);
status = gdip_get_pattern_status (pat);
if (status != Ok)
goto cleanup;
cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
/* texture surface to be created */
texture = cairo_surface_create_similar (original, from_cairoformat_to_content (bitmap->cairo_format),
2 * rect->Width, rect->Height);
status = gdip_get_status (cairo_surface_status (texture));
if (status != Ok) {
cairo_pattern_destroy (pat);
goto cleanup;
}
/* Draw left part of the texture */
ct2 = cairo_create (texture);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
/* Not sure if this is a bug, but using rect->Width - 1 avoids the seam. */
cairo_matrix_init_identity (&tempMatrix);
cairo_matrix_translate (&tempMatrix, rect->Width - 1, 0);
/* scale in -X direction to flip along X */
cairo_matrix_scale (&tempMatrix, -1.0, 1.0);
cairo_pattern_set_matrix (pat, &tempMatrix);
/* Draw right part of the texture */
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, rect->Width, 0, rect->Width, rect->Height);
cairo_fill (ct2);
cairo_destroy (ct2);
brush->pattern = cairo_pattern_create_for_surface (texture);
status = gdip_get_pattern_status (brush->pattern);
if (status == Ok)
cairo_pattern_set_extend (brush->pattern, CAIRO_EXTEND_REPEAT);
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
status = gdip_get_status (cairo_status (ct));
cleanup:
if (premul) {
cairo_surface_destroy (original);
GdipFree (premul);
}
return status;
}
static GpStatus
draw_tile_flipY_texture (cairo_t *ct, GpBitmap *bitmap, GpTexture *brush)
{
cairo_surface_t *original = NULL;
cairo_surface_t *texture;
cairo_pattern_t *pat;
GpMatrix tempMatrix;
GpRect *rect = &brush->rectangle;
GpStatus status;
cairo_t *ct2;
BYTE *premul = NULL;
if (!rect)
return InvalidParameter;
gdip_bitmap_ensure_surface (bitmap);
if (gdip_bitmap_format_needs_premultiplication (bitmap)) {
premul = gdip_bitmap_get_premultiplied_scan0 (bitmap);
if (premul) {
BitmapData *data = bitmap->active_bitmap;
original = cairo_image_surface_create_for_data (premul, CAIRO_FORMAT_ARGB32,
data->width, data->height, data->stride);
}
}
/* if premul isn't required (or couldn't be computed, e.g. out of memory) */
if (!original)
original = bitmap->surface;
/* Use the original as a pattern */
pat = cairo_pattern_create_for_surface (original);
status = gdip_get_pattern_status (pat);
if (status != Ok)
goto cleanup;
cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
/* texture surface to be created */
texture = cairo_surface_create_similar (original, from_cairoformat_to_content (bitmap->cairo_format),
rect->Width, 2 * rect->Height);
status = gdip_get_status (cairo_surface_status (texture));
if (status != Ok) {
cairo_pattern_destroy (pat);
goto cleanup;
}
/* Draw upper part of the texture */
ct2 = cairo_create (texture);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
/* Not sure if this is a bug, but using rect->Height - 1 avoids the seam. */
cairo_matrix_init_identity (&tempMatrix);
cairo_matrix_translate (&tempMatrix, 0, rect->Height - 1);
/* scale in -Y direction to flip along Y */
cairo_matrix_scale (&tempMatrix, 1.0, -1.0);
cairo_pattern_set_matrix (pat, &tempMatrix);
/* Draw lower part of the texture */
cairo_translate (ct2, 0, rect->Height);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
cairo_destroy(ct2);
brush->pattern = cairo_pattern_create_for_surface (texture);
status = gdip_get_pattern_status (brush->pattern);
if (status != Ok) {
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
goto cleanup;
}
cairo_pattern_set_extend (brush->pattern, CAIRO_EXTEND_REPEAT);
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
status = gdip_get_status (cairo_status (ct));
cleanup:
if (premul) {
cairo_surface_destroy (original);
GdipFree (premul);
}
return status;
}
static GpStatus
draw_tile_flipXY_texture (cairo_t *ct, GpBitmap *bitmap, GpTexture *brush)
{
cairo_surface_t *original = NULL;
cairo_surface_t *texture;
cairo_pattern_t *pat;
GpMatrix tempMatrix;
GpRect *rect = &brush->rectangle;
GpStatus status;
cairo_t *ct2;
BYTE *premul = NULL;
if (!rect)
return InvalidParameter;
gdip_bitmap_ensure_surface (bitmap);
if (gdip_bitmap_format_needs_premultiplication (bitmap)) {
premul = gdip_bitmap_get_premultiplied_scan0 (bitmap);
if (premul) {
BitmapData *data = bitmap->active_bitmap;
original = cairo_image_surface_create_for_data (premul, CAIRO_FORMAT_ARGB32,
data->width, data->height, data->stride);
}
}
/* if premul isn't required (or couldn't be computed, e.g. out of memory) */
if (!original)
original = bitmap->surface;
/* Use the original as a pattern */
pat = cairo_pattern_create_for_surface (original);
status = gdip_get_pattern_status(pat);
if (status != Ok)
goto cleanup;
cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
/* texture surface to be created */
texture = cairo_surface_create_similar (original, from_cairoformat_to_content (bitmap->cairo_format),
2 * rect->Width, 2 * rect->Height);
status = gdip_get_status (cairo_surface_status (texture));
if (status != Ok) {
cairo_pattern_destroy (pat);
goto cleanup;
}
/* Draw upper left part of the texture */
ct2 = cairo_create (texture);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
/* Not sure if this is a bug, but using rect->Height - 1 avoids the seam. */
cairo_matrix_init_identity (&tempMatrix);
cairo_matrix_translate (&tempMatrix, 0, rect->Height - 1);
/* scale in -Y direction to flip along Y */
cairo_matrix_scale (&tempMatrix, 1.0, -1.0);
cairo_pattern_set_matrix (pat, &tempMatrix);
/* Draw lower left part of the texture */
cairo_translate (ct2, 0, rect->Height);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
/* Reset the pattern matrix and do fresh transformation */
cairo_matrix_init_identity (&tempMatrix);
/* Not sure if this is a bug, but using rect->Width - 1 avoids the seam. */
cairo_matrix_translate (&tempMatrix, rect->Width - 1, 0);
/* scale in -X direction to flip along X */
cairo_matrix_scale (&tempMatrix, -1.0, 1.0);
cairo_pattern_set_matrix (pat, &tempMatrix);
/* Draw upper right part of the texture */
cairo_translate (ct2, rect->Width, -rect->Height);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
/* Not sure if this is a bug, but using rect->Height - 1 avoids the seam. */
cairo_matrix_translate (&tempMatrix, 0, rect->Height - 1);
/* scale in -Y direction to flip along Y */
cairo_matrix_scale (&tempMatrix, 1.0, -1.0);
cairo_pattern_set_matrix (pat, &tempMatrix);
/* Draw lower right part of the texture */
cairo_translate (ct2, 0, rect->Height);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
cairo_destroy(ct2);
brush->pattern = cairo_pattern_create_for_surface (texture);
status = gdip_get_pattern_status(brush->pattern);
if (status != Ok) {
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
goto cleanup;
}
cairo_pattern_set_extend (brush->pattern, CAIRO_EXTEND_REPEAT);
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
status = gdip_get_status (cairo_status (ct));
cleanup:
if (premul) {
cairo_surface_destroy (original);
GdipFree (premul);
}
return status;
}
static GpStatus
draw_clamp_texture (cairo_t *ct, GpBitmap *bitmap, GpTexture *brush)
{
cairo_surface_t *original;
cairo_surface_t *texture;
cairo_pattern_t *pat;
GpRect *rect = &brush->rectangle;
GpStatus status;
cairo_t *ct2;
/* Original image surface */
gdip_bitmap_ensure_surface (bitmap);
original = bitmap->surface;
/* Use the original as a pattern */
pat = cairo_pattern_create_for_surface (original);
status = gdip_get_pattern_status (pat);
if (status != Ok) {
return status;
}
cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
/* texture surface to be created */
texture = cairo_surface_create_similar (original, from_cairoformat_to_content (bitmap->cairo_format),
rect->Width, rect->Height);
if (texture == NULL) {
cairo_pattern_destroy (pat);
return OutOfMemory;
}
/* Draw the texture */
ct2 = cairo_create (texture);
cairo_identity_matrix (ct2);
cairo_set_source (ct2, pat);
cairo_rectangle (ct2, 0, 0, rect->Width, rect->Height);
cairo_fill (ct2);
cairo_destroy(ct2);
brush->pattern = cairo_pattern_create_for_surface (texture);
status = gdip_get_pattern_status (brush->pattern);
if (status != Ok) {
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
return status;
}
cairo_pattern_destroy (pat);
cairo_surface_destroy (texture);
return gdip_get_status (cairo_status (ct));
}
GpStatus
gdip_texture_setup (GpGraphics *graphics, GpBrush *brush)
{
cairo_t *ct;
cairo_pattern_t *pattern;
GpTexture *texture;
GpImage *img;
GpStatus status = Ok;
BOOL dispose_bitmap;
if (!graphics || !brush || !graphics->ct)
return InvalidParameter;
texture = (GpTexture *) brush;
img = texture->image;
if (!img)
return InvalidParameter;
if (img->type != ImageTypeBitmap)
return NotImplemented;
if (gdip_is_an_indexed_pixelformat (img->active_bitmap->pixel_format)) {
/* Unable to create a surface for the bitmap; it is an indexed image.
* Instead, it will first be converted to 32-bit RGB. */
img = gdip_convert_indexed_to_rgb (img);
if (img == NULL) {
return OutOfMemory;
}
gdip_bitmap_ensure_surface (img);
dispose_bitmap = TRUE;
} else {
dispose_bitmap = FALSE;
}
ct = graphics->ct;
/* We create the new pattern for brush, if the brush is changed
* or if pattern has not been created yet. */
if (texture->base.changed || (texture->pattern == NULL)) {
if (texture->pattern != NULL) {
cairo_pattern_destroy (texture->pattern);
}
switch (texture->wrapMode) {
case WrapModeTile:
status = draw_tile_texture (ct, img, texture);
break;
case WrapModeTileFlipX:
status = draw_tile_flipX_texture (ct, img, texture);
break;
case WrapModeTileFlipY:
status = draw_tile_flipY_texture (ct, img, texture);
break;
case WrapModeTileFlipXY:
status = draw_tile_flipXY_texture (ct, img, texture);
break;
case WrapModeClamp:
status = draw_clamp_texture (ct, img, texture);
break;
default:
status = InvalidParameter;
}
}
if (dispose_bitmap) {
GdipDisposeImage((GpImage *)img);
}
if ((status != Ok) || (gdip_get_pattern_status(texture->pattern) != Ok)) {
return GenericError;
}
pattern = texture->pattern;
if (pattern != NULL) {
/* got something to apply ? */
if (!gdip_is_matrix_empty (&texture->matrix)) {
cairo_matrix_t product;
gdip_cairo_matrix_copy (&product, &texture->matrix);
cairo_matrix_invert (&product);
cairo_pattern_set_matrix (pattern, &product);
}
cairo_set_source (ct, pattern);
}
return gdip_get_status (cairo_status (ct));
}
GpStatus
gdip_texture_clone (GpBrush *brush, GpBrush **clonedBrush)
{
GpTexture *result;
GpTexture *texture;
GpStatus status;
if (!brush || !clonedBrush)
return InvalidParameter;
result = (GpTexture *) GdipAlloc (sizeof (GpTexture));
if (result == NULL) {
return OutOfMemory;
}
texture = (GpTexture *) brush;
result->base = texture->base;
result->wrapMode = texture->wrapMode;
/* Let the clone create its own pattern. */
result->pattern = NULL;
result->base.changed = TRUE;
gdip_cairo_matrix_copy (&result->matrix, &texture->matrix);
result->rectangle.X = texture->rectangle.X;
result->rectangle.Y = texture->rectangle.Y;
result->rectangle.Width = texture->rectangle.Width;
result->rectangle.Height = texture->rectangle.Height;
result->image = NULL;
status = GdipCloneImage (texture->image, &result->image);
if (status != Ok) {
if (result->image)
GdipDisposeImage (result->image);
GdipFree (result);
result = NULL;
} else {
cairo_surface_reference (result->image->surface);
}
*clonedBrush = (GpBrush *) result;
return status;
}
GpStatus
gdip_texture_destroy (GpBrush *brush)
{
/* a. the NULL check for brush is done by the caller, GdipDeleteBrush */
/* b. brush itself is freed by the caller */
GpTexture *texture = (GpTexture *) brush;
if (texture->pattern) {
cairo_pattern_destroy (texture->pattern);
texture->pattern = NULL;
}
if (texture->image) {
GdipDisposeImage (texture->image);
texture->image = NULL;
}
return Ok;
}
/* coverity[+alloc : arg-*2] */
GpStatus
GdipCreateTexture (GpImage *image, GpWrapMode wrapMode, GpTexture **texture)
{
cairo_surface_t *imageSurface = NULL;
GpTexture *result;
GpStatus status;
if (!image || !texture)
return InvalidParameter;
if ((wrapMode < WrapModeTile) || (wrapMode > WrapModeClamp))
return OutOfMemory;
if (image->type != ImageTypeBitmap)
return NotImplemented;
result = gdip_texture_new ();
if (!result)
return OutOfMemory;
result->image = NULL;
status = GdipCloneImage (image, &result->image);
if (status != Ok)
goto failure;
/* note: we must keep the scan0 alive, so we must use the cloned image (and not the original) see bug #80971 */
imageSurface = cairo_image_surface_create_for_data ((BYTE*)result->image->active_bitmap->scan0,
image->cairo_format, image->active_bitmap->width, image->active_bitmap->height, image->active_bitmap->stride);
if (!imageSurface)
goto failure;
result->wrapMode = wrapMode;
if (result->image->surface)
cairo_surface_destroy (result->image->surface);
result->image->surface = imageSurface;
result->rectangle.X = 0;
result->rectangle.Y = 0;
result->rectangle.Width = image->active_bitmap->width;
result->rectangle.Height = image->active_bitmap->height;
*texture = result;
return Ok;
failure:
if (result->image)
GdipDisposeImage (result->image);
if (imageSurface)
cairo_surface_destroy (imageSurface);
GdipFree (result);
*texture = NULL;
return status;
}
/* coverity[+alloc : arg-*6] */
GpStatus
GdipCreateTexture2 (GpImage *image, GpWrapMode wrapMode, float x, float y, float width, float height, GpTexture **texture)
{
return GdipCreateTexture2I (image, wrapMode, (int) x, (int) y, (int) width, (int) height, texture);
}
/* coverity[+alloc : arg-*6] */
GpStatus
GdipCreateTexture2I (GpImage *image, GpWrapMode wrapMode, int x, int y, int width, int height, GpTexture **texture)
{
int bmpWidth;
int bmpHeight;
GpStatus status;
GpImage *resized_image = NULL;
if (!image || !texture)
return InvalidParameter;
if (image->type != ImageTypeBitmap)
return NotImplemented;
bmpWidth = image->active_bitmap->width;
bmpHeight = image->active_bitmap->height;
/* MS behaves this way */
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || (bmpWidth < (x + width)) || (bmpHeight < (y + height)))
return OutOfMemory;
status = GdipCloneBitmapAreaI (x, y, width, height, image->active_bitmap->pixel_format, image, &resized_image);
if (status != Ok)
return status;
status = GdipCreateTexture (resized_image, wrapMode, texture);
GdipDisposeImage (resized_image);
return status;
}
/* coverity[+alloc : arg-*6] */
GpStatus
GdipCreateTextureIA (GpImage *image, GpImageAttributes *imageAttributes, float x, float y, float width, float height, GpTexture **texture)
{
/* FIXME MonoTODO: Make use of ImageAttributes parameter when
* ImageAttributes is implemented */
GpWrapMode mode = imageAttributes ? WrapModeClamp : WrapModeTile;
return GdipCreateTexture2 (image, mode, x, y, width, height, texture);
}
/* coverity[+alloc : arg-*6] */
GpStatus
GdipCreateTextureIAI (GpImage *image, GpImageAttributes *imageAttributes, int x, int y, int width, int height, GpTexture **texture)
{
/* FIXME MonoTODO: Make use of ImageAttributes parameter when
* ImageAttributes is implemented */
GpWrapMode mode = imageAttributes ? WrapModeClamp : WrapModeTile;
return GdipCreateTexture2I (image, mode, x, y, width, height, texture);
}
GpStatus
GdipGetTextureTransform (GpTexture *texture, GpMatrix *matrix)
{
if ((texture == NULL) || (matrix == NULL)) {
return InvalidParameter;
}
gdip_cairo_matrix_copy(matrix, &texture->matrix);
return Ok;
}
GpStatus
GdipSetTextureTransform (GpTexture *texture, GDIPCONST GpMatrix *matrix)
{
if ((texture == NULL) || (matrix == NULL)) {
return InvalidParameter;
}
gdip_cairo_matrix_copy(&texture->matrix, matrix);
texture->base.changed = TRUE;
return Ok;
}
GpStatus
GdipResetTextureTransform (GpTexture *texture)
{
if (texture == NULL) {
return InvalidParameter;
}
cairo_matrix_init_identity (&texture->matrix);
texture->base.changed = TRUE;
return Ok;
}
GpStatus
GdipMultiplyTextureTransform (GpTexture *texture, GpMatrix *matrix, GpMatrixOrder order)
{
GpStatus status;
BOOL invertible = FALSE;
cairo_matrix_t mat;
if ((texture == NULL) || (matrix == NULL)) {
return InvalidParameter;
}
/* the matrix MUST be invertible to be used */
status = GdipIsMatrixInvertible ((GpMatrix*) matrix, &invertible);
if (!invertible || (status != Ok))
return InvalidParameter;
if (order == MatrixOrderPrepend)
cairo_matrix_multiply (&mat, matrix, &texture->matrix);
else if (order == MatrixOrderAppend)
cairo_matrix_multiply (&mat, &texture->matrix, matrix);
gdip_cairo_matrix_copy (&texture->matrix, &mat);
texture->base.changed = TRUE;
return status;
}
GpStatus
GdipTranslateTextureTransform (GpTexture *texture, float dx, float dy, GpMatrixOrder order)
{
GpStatus status;
if (texture == NULL) {
return InvalidParameter;
}
status = GdipTranslateMatrix (&texture->matrix, dx, dy, order);
if (status == Ok)
texture->base.changed = TRUE;
return status;
}
GpStatus
GdipScaleTextureTransform (GpTexture *texture, float sx, float sy, GpMatrixOrder order)
{
GpStatus status;
if (texture == NULL) {
return InvalidParameter;
}
status = GdipScaleMatrix (&texture->matrix, sx, sy, order);
if (status == Ok)
texture->base.changed = TRUE;
return status;
}
/* MonoTODO - FIXME - this hack affect the public transform and doesn't work with MultiplyTransform */
GpStatus
GdipRotateTextureTransform (GpTexture *texture, float angle, GpMatrixOrder order)
{
GpStatus status;
if (texture == NULL)
return InvalidParameter;
status = GdipRotateMatrix (&texture->matrix, angle, order);
if (status == Ok)
texture->base.changed = TRUE;
return status;
}
GpStatus
GdipSetTextureWrapMode (GpTexture *texture, GpWrapMode wrapMode)
{
if (texture == NULL)
return InvalidParameter;
/* ignore invalid GpWrapMode value */
if ((wrapMode < WrapModeTile) || (wrapMode > WrapModeClamp))
return Ok;
texture->wrapMode = wrapMode;
texture->base.changed = TRUE;
return Ok;
}
GpStatus
GdipGetTextureWrapMode (GpTexture *texture, GpWrapMode *wrapMode)
{
if (!texture || !wrapMode)
return InvalidParameter;
*wrapMode = texture->wrapMode;
return Ok;
}
/* coverity[+alloc : arg-*1] */
GpStatus
GdipGetTextureImage (GpTexture *texture, GpImage **image)
{
if (!texture || !image)
return InvalidParameter;
return GdipCloneImage (texture->image, image);
}