Merge branch 'master' of ssh+git://sandmann@git.freedesktop.org/git/pixman
[profile/ivi/pixman.git] / pixman / pixman-image.c
1 /*
2  * Copyright © 2000 SuSE, Inc.
3  * Copyright © 2007 Red Hat, Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of SuSE not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  SuSE makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  *
15  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
17  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "pixman.h"
30 #include "pixman-private.h"
31
32 static void
33 init_source_image (source_image_t *image)
34 {
35     image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
36 }
37
38 static pixman_bool_t
39 init_gradient (gradient_t     *gradient,
40                const pixman_gradient_stop_t *stops,
41                int             n_stops)
42 {
43     return_val_if_fail (n_stops > 0, FALSE);
44
45     init_source_image (&gradient->common);
46
47     gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
48     if (!gradient->stops)
49         return FALSE;
50
51     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
52     
53     gradient->n_stops = n_stops;
54
55     gradient->stop_range = 0xffff;
56     gradient->color_table = NULL;
57     gradient->color_table_size = 0;
58
59     return TRUE;
60 }
61
62 static uint32_t
63 color_to_uint32 (const pixman_color_t *color)
64 {
65     return
66         (color->alpha >> 8 << 24) |
67         (color->red >> 8 << 16) |
68         (color->green & 0xff00) |
69         (color->blue >> 8);
70 }
71
72 static pixman_image_t *
73 allocate_image (void)
74 {
75     pixman_image_t *image = malloc (sizeof (pixman_image_t));
76     
77     if (image)
78     {
79         image_common_t *common = &image->common;
80
81         pixman_region_init (&common->full_region);
82         pixman_region_init (&common->clip_region);
83         common->src_clip = &common->full_region;
84         common->has_client_clip = FALSE;
85         common->transform = NULL;
86         common->repeat = PIXMAN_REPEAT_NONE;
87         common->filter = PIXMAN_FILTER_NEAREST;
88         common->filter_params = NULL;
89         common->n_filter_params = 0;
90         common->alpha_map = NULL;
91         common->component_alpha = FALSE;
92         common->ref_count = 1;
93         common->read_func = NULL;
94         common->write_func = NULL;
95     }
96
97     return image;
98 }
99
100 /* Ref Counting */
101 pixman_image_t *
102 pixman_image_ref (pixman_image_t *image)
103 {
104     image->common.ref_count++;
105
106     return image;
107 }
108
109 /* returns TRUE when the image is freed */
110 pixman_bool_t
111 pixman_image_unref (pixman_image_t *image)
112 {
113     image_common_t *common = (image_common_t *)image;
114
115     common->ref_count--;
116
117     if (common->ref_count == 0)
118     {
119         pixman_region_fini (&common->clip_region);
120         pixman_region_fini (&common->full_region);
121
122         if (common->transform)
123             free (common->transform);
124
125         if (common->filter_params)
126             free (common->filter_params);
127
128         if (common->alpha_map)
129             pixman_image_unref ((pixman_image_t *)common->alpha_map);
130
131 #if 0
132         if (image->type == BITS && image->bits.indexed)
133             free (image->bits.indexed);
134 #endif
135         
136 #if 0
137         memset (image, 0xaa, sizeof (pixman_image_t));
138 #endif
139         if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
140         {
141             if (image->gradient.stops)
142                 free (image->gradient.stops);
143         }
144
145         
146         if (image->type == BITS && image->bits.free_me)
147             free (image->bits.free_me);
148         
149         free (image);
150
151         return TRUE;
152     }
153
154     return FALSE;
155 }
156
157 /* Constructors */
158 pixman_image_t *
159 pixman_image_create_solid_fill (pixman_color_t *color)
160 {
161     pixman_image_t *img = allocate_image();
162     if (!img)
163         return NULL;
164     
165     init_source_image (&img->solid.common);
166     
167     img->type = SOLID;
168     img->solid.color = color_to_uint32 (color);
169
170     return img;
171 }
172
173 pixman_image_t *
174 pixman_image_create_linear_gradient (pixman_point_fixed_t         *p1,
175                                      pixman_point_fixed_t         *p2,
176                                      const pixman_gradient_stop_t *stops,
177                                      int                           n_stops)
178 {
179     pixman_image_t *image;
180     linear_gradient_t *linear;
181
182     return_val_if_fail (n_stops >= 2, NULL);
183     
184     image = allocate_image();
185     
186     if (!image)
187         return NULL;
188
189     linear = &image->linear;
190     
191     if (!init_gradient (&linear->common, stops, n_stops))
192     {
193         free (image);
194         return NULL;
195     }
196
197     linear->p1 = *p1;
198     linear->p2 = *p2;
199
200     image->type = LINEAR;
201
202     return image;
203 }
204
205
206 pixman_image_t *
207 pixman_image_create_radial_gradient (pixman_point_fixed_t         *inner,
208                                      pixman_point_fixed_t         *outer,
209                                      pixman_fixed_t                inner_radius,
210                                      pixman_fixed_t                outer_radius,
211                                      const pixman_gradient_stop_t *stops,
212                                      int                           n_stops)
213 {
214     pixman_image_t *image;
215     radial_gradient_t *radial;
216
217     return_val_if_fail (n_stops >= 2, NULL);
218     
219     image = allocate_image();
220
221     if (!image)
222         return NULL;
223
224     radial = &image->radial;
225
226     if (!init_gradient (&radial->common, stops, n_stops))
227     {
228         free (image);
229         return NULL;
230     }
231
232     image->type = RADIAL;
233     
234     radial->c1.x = inner->x;
235     radial->c1.y = inner->y;
236     radial->c1.radius = inner_radius;
237     radial->c2.x = outer->x;
238     radial->c2.y = outer->y;
239     radial->c2.radius = outer_radius;
240     radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
241     radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
242     radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
243     radial->A = (radial->cdx * radial->cdx
244                  + radial->cdy * radial->cdy
245                  - radial->dr  * radial->dr);
246     
247     return image;
248 }
249
250 pixman_image_t *
251 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
252                                       pixman_fixed_t angle,
253                                       const pixman_gradient_stop_t *stops,
254                                       int n_stops)
255 {
256     pixman_image_t *image = allocate_image();
257     conical_gradient_t *conical;
258
259     if (!image)
260         return NULL;
261
262     conical = &image->conical;
263     
264     if (!init_gradient (&conical->common, stops, n_stops))
265     {
266         free (image);
267         return NULL;
268     }
269
270     image->type = CONICAL;
271     conical->center = *center;
272     conical->angle = angle;
273
274     return image;
275 }
276
277 static uint32_t *
278 create_bits (pixman_format_code_t format,
279              int                  width,
280              int                  height,
281              int                 *rowstride_bytes)
282 {
283     int stride;
284     int buf_size;
285     int bpp;
286     
287     bpp = PIXMAN_FORMAT_BPP (format);
288     stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
289     buf_size = height * stride;
290
291     if (rowstride_bytes)
292         *rowstride_bytes = stride;
293
294     return calloc (buf_size, 1);
295 }
296
297 static void
298 reset_clip_region (pixman_image_t *image)
299 {
300     pixman_region_fini (&image->common.clip_region);
301     
302     if (image->type == BITS)
303     {
304         pixman_region_init_rect (&image->common.clip_region, 0, 0,
305                                  image->bits.width, image->bits.height);        
306     }
307     else
308     {
309         pixman_region_init (&image->common.clip_region);
310     }
311 }
312
313 pixman_image_t *
314 pixman_image_create_bits (pixman_format_code_t  format,
315                           int                   width,
316                           int                   height,
317                           uint32_t             *bits,
318                           int                   rowstride_bytes)
319 {
320     pixman_image_t *image;
321     uint32_t *free_me = NULL;
322
323     /* must be a whole number of uint32_t's 
324      */
325     return_val_if_fail (bits == NULL ||
326                         (rowstride_bytes % sizeof (uint32_t)) == 0, NULL); 
327
328     if (!bits)
329     {
330         free_me = bits = create_bits (format, width, height, &rowstride_bytes);
331         if (!bits)
332             return NULL;
333     }
334     
335     image = allocate_image();
336
337     if (!image)
338         return NULL;
339     
340     image->type = BITS;
341     image->bits.format = format;
342     image->bits.width = width;
343     image->bits.height = height;
344     image->bits.bits = bits;
345     image->bits.free_me = free_me;
346     
347     image->bits.rowstride = rowstride_bytes / sizeof (uint32_t); /* we store it in number
348                                                                   * of uint32_t's
349                                                                   */
350     image->bits.indexed = NULL;
351
352     pixman_region_fini (&image->common.full_region);
353     pixman_region_init_rect (&image->common.full_region, 0, 0,
354                              image->bits.width, image->bits.height);
355
356     reset_clip_region (image);
357     return image;
358 }
359
360 pixman_bool_t
361 pixman_image_set_clip_region (pixman_image_t    *image,
362                               pixman_region16_t *region)
363 {
364     image_common_t *common = (image_common_t *)image;
365
366     if (region)
367     {
368         return pixman_region_copy (&common->clip_region, region);
369     }
370     else
371     {
372         reset_clip_region (image);
373         
374         return TRUE;
375     }
376 }
377
378 /* Sets whether the clip region includes a clip region set by the client
379  */
380 void
381 pixman_image_set_has_client_clip (pixman_image_t *image,
382                                   pixman_bool_t   client_clip)
383 {
384     image->common.has_client_clip = client_clip;
385 }
386
387 pixman_bool_t
388 pixman_image_set_transform (pixman_image_t           *image,
389                             const pixman_transform_t *transform)
390 {
391     static const pixman_transform_t id =
392     {
393         { { pixman_fixed_1, 0, 0 },
394           { 0, pixman_fixed_1, 0 },
395           { 0, 0, pixman_fixed_1 }
396         }
397     };
398     
399     image_common_t *common = (image_common_t *)image;
400
401     if (common->transform == transform)
402         return TRUE;
403
404     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
405     {
406         free(common->transform);
407         common->transform = NULL;
408         return TRUE;
409     }
410     
411     if (common->transform == NULL)
412         common->transform = malloc (sizeof (pixman_transform_t));
413     if (common->transform == NULL)
414         return FALSE;
415
416     memcpy(common->transform, transform, sizeof(pixman_transform_t));
417
418     return TRUE;
419 }
420
421 void
422 pixman_image_set_repeat (pixman_image_t  *image,
423                          pixman_repeat_t  repeat)
424 {
425     image->common.repeat = repeat;
426 }
427
428 pixman_bool_t 
429 pixman_image_set_filter (pixman_image_t       *image,
430                          pixman_filter_t       filter,
431                          const pixman_fixed_t *params,
432                          int                   n_params)
433 {
434     image_common_t *common = (image_common_t *)image;
435     pixman_fixed_t *new_params;
436
437     if (params == common->filter_params && filter == common->filter)
438         return TRUE;
439
440     new_params = NULL;
441     if (params)
442     {
443         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
444         if (!new_params)
445             return FALSE;
446
447         memcpy (new_params,
448                 params, n_params * sizeof (pixman_fixed_t));
449     }
450
451     common->filter = filter;
452         
453     if (common->filter_params)
454         free (common->filter_params);
455
456     common->filter_params = new_params;
457     common->n_filter_params = n_params;
458     return TRUE;
459 }
460
461 void
462 pixman_image_set_source_clipping (pixman_image_t  *image,
463                                   pixman_bool_t    source_clipping)
464 {
465     image_common_t *common = &image->common;
466
467     if (source_clipping)
468         common->src_clip = &common->clip_region;
469     else
470         common->src_clip = &common->full_region;
471 }
472
473 /* Unlike all the other property setters, this function does not
474  * copy the content of indexed. Doing this copying is simply
475  * way, way too expensive.
476  */
477 void
478 pixman_image_set_indexed (pixman_image_t         *image,
479                           const pixman_indexed_t *indexed)
480 {
481     bits_image_t *bits = (bits_image_t *)image;
482
483     bits->indexed = indexed;
484 }
485
486 void
487 pixman_image_set_alpha_map (pixman_image_t *image,
488                             pixman_image_t *alpha_map,
489                             int16_t         x,
490                             int16_t         y)
491 {
492     image_common_t *common = (image_common_t *)image;
493     
494     return_if_fail (!alpha_map || alpha_map->type == BITS);
495
496     if (common->alpha_map != (bits_image_t *)alpha_map)
497     {
498         if (common->alpha_map)
499             pixman_image_unref ((pixman_image_t *)common->alpha_map);
500
501         if (alpha_map)
502             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
503         else
504             common->alpha_map = NULL;
505     }
506
507     common->alpha_origin.x = x;
508     common->alpha_origin.y = y;
509 }
510
511 void
512 pixman_image_set_component_alpha   (pixman_image_t       *image,
513                                     pixman_bool_t         component_alpha)
514 {
515     image->common.component_alpha = component_alpha;
516 }
517
518
519 void
520 pixman_image_set_accessors (pixman_image_t             *image,
521                             pixman_read_memory_func_t   read_func,
522                             pixman_write_memory_func_t  write_func)
523 {
524     return_if_fail (image != NULL);
525
526     image->common.read_func = read_func;
527     image->common.write_func = write_func;
528 }
529
530 uint32_t *
531 pixman_image_get_data (pixman_image_t *image)
532 {
533     if (image->type == BITS)
534         return image->bits.bits;
535
536     return NULL;
537 }
538
539 int
540 pixman_image_get_width (pixman_image_t *image)
541 {
542     if (image->type == BITS)
543         return image->bits.width;
544
545     return 0;
546 }
547
548 int
549 pixman_image_get_height (pixman_image_t *image)
550 {
551     if (image->type == BITS)
552         return image->bits.height;
553
554     return 0;
555 }
556
557 int
558 pixman_image_get_stride (pixman_image_t *image)
559 {
560     if (image->type == BITS)
561         return image->bits.rowstride * sizeof (uint32_t);
562
563     return 0;
564 }
565
566 int
567 pixman_image_get_depth (pixman_image_t *image)
568 {
569     if (image->type == BITS)
570         return PIXMAN_FORMAT_DEPTH (image->bits.format);
571
572     return 0;
573 }
574
575 pixman_bool_t
576 color_to_pixel (pixman_color_t *color,
577                 uint32_t       *pixel,
578                 pixman_format_code_t format)
579 {
580     uint32_t c = color_to_uint32 (color);
581
582     if (!(format == PIXMAN_a8r8g8b8     ||
583           format == PIXMAN_x8r8g8b8     ||
584           format == PIXMAN_a8b8g8r8     ||
585           format == PIXMAN_x8b8g8r8     ||
586           format == PIXMAN_r5g6b5       ||
587           format == PIXMAN_b5g6r5       ||
588           format == PIXMAN_a8))
589     {
590         return FALSE;
591     }
592     
593     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
594     {
595         c = ((c & 0xff000000) >>  0) |
596             ((c & 0x00ff0000) >> 16) |
597             ((c & 0x0000ff00) >>  0) |
598             ((c & 0x000000ff) << 16);
599     }
600
601     if (format == PIXMAN_a8)
602         c = c >> 24;
603     else if (format == PIXMAN_r5g6b5 ||
604              format == PIXMAN_b5g6r5)
605         c = cvt8888to0565 (c);
606
607 #if 0
608     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
609     printf ("pixel: %x\n", c);
610 #endif
611     
612     *pixel = c;
613     return TRUE;
614 }
615
616 pixman_bool_t
617 pixman_image_fill_rectangles (pixman_op_t                   op,
618                               pixman_image_t               *dest,
619                               pixman_color_t               *color,
620                               int                           n_rects,
621                               const pixman_rectangle16_t   *rects)
622 {
623     pixman_image_t *solid;
624     pixman_color_t c;
625     int i;
626     
627     if (color->alpha == 0xffff)
628     {
629         if (op == PIXMAN_OP_OVER)
630             op = PIXMAN_OP_SRC;
631     }
632
633     if (op == PIXMAN_OP_CLEAR)
634     {
635         c.red = 0;
636         c.green = 0;
637         c.blue = 0;
638         c.alpha = 0;
639
640         color = &c;
641         
642         op = PIXMAN_OP_SRC;
643     }
644
645     if (op == PIXMAN_OP_SRC)
646     {
647         uint32_t pixel;
648         
649         if (color_to_pixel (color, &pixel, dest->bits.format))
650         {
651             for (i = 0; i < n_rects; ++i)
652             {
653                 pixman_region16_t fill_region;
654                 int n_boxes, j;
655                 pixman_box16_t *boxes;
656                 
657                 pixman_region_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
658                 pixman_region_intersect (&fill_region, &fill_region, &dest->common.clip_region);
659
660                 boxes = pixman_region_rectangles (&fill_region, &n_boxes);
661                 for (j = 0; j < n_boxes; ++j)
662                 {
663                     const pixman_box16_t *box = &(boxes[j]);
664                     pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
665                                  box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
666                                  pixel);
667                 }
668
669                 pixman_region_fini (&fill_region);
670             }
671             return TRUE;
672         }
673     }
674     
675     solid = pixman_image_create_solid_fill (color);
676     if (!solid)
677         return FALSE;
678
679     for (i = 0; i < n_rects; ++i)
680     {
681         const pixman_rectangle16_t *rect = &(rects[i]);
682         
683         pixman_image_composite (op, solid, NULL, dest,
684                                 0, 0, 0, 0,
685                                 rect->x, rect->y,
686                                 rect->width, rect->height);
687     }
688     
689     pixman_image_unref (solid);
690
691     return TRUE;
692 }