LCOV - code coverage report
Current view: directory - gfx/cairo/cairo/src - cairo-xlib-visual.c (source / functions) Found Hit Coverage
Test: app.info Lines: 82 0 0.0 %
Date: 2012-06-02 Functions: 3 0 0.0 %

       1                 : /* Cairo - a vector graphics library with display and print output
       2                 :  *
       3                 :  * Copyright © 2008 Red Hat, Inc.
       4                 :  *
       5                 :  * This library is free software; you can redistribute it and/or
       6                 :  * modify it either under the terms of the GNU Lesser General Public
       7                 :  * License version 2.1 as published by the Free Software Foundation
       8                 :  * (the "LGPL") or, at your option, under the terms of the Mozilla
       9                 :  * Public License Version 1.1 (the "MPL"). If you do not alter this
      10                 :  * notice, a recipient may use your version of this file under either
      11                 :  * the MPL or the LGPL.
      12                 :  *
      13                 :  * You should have received a copy of the LGPL along with this library
      14                 :  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
      15                 :  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
      16                 :  * You should have received a copy of the MPL along with this library
      17                 :  * in the file COPYING-MPL-1.1
      18                 :  *
      19                 :  * The contents of this file are subject to the Mozilla Public License
      20                 :  * Version 1.1 (the "License"); you may not use this file except in
      21                 :  * compliance with the License. You may obtain a copy of the License at
      22                 :  * http://www.mozilla.org/MPL/
      23                 :  *
      24                 :  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
      25                 :  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
      26                 :  * the specific language governing rights and limitations.
      27                 :  *
      28                 :  * The Original Code is the cairo graphics library.
      29                 :  *
      30                 :  * The Initial Developer of the Original Code is Red Hat, Inc.
      31                 :  *
      32                 :  * Contributor(s):
      33                 :  *      Carl D. Worth <cworth@cworth.org>
      34                 :  */
      35                 : 
      36                 : #include "cairoint.h"
      37                 : 
      38                 : #include "cairo-xlib-private.h"
      39                 : 
      40                 : #include "cairo-error-private.h"
      41                 : 
      42                 : /* A perceptual distance metric between two colors. No sqrt needed
      43                 :  * since the square of the distance is still a valid metric. */
      44                 : 
      45                 : /* XXX: This is currently using linear distance in RGB space which is
      46                 :  * decidedly not perceptually linear. If someone cared a lot about the
      47                 :  * quality, they might choose something else here. Then again, they
      48                 :  * might also choose not to use a PseudoColor visual... */
      49                 : static inline int
      50               0 : _color_distance (unsigned short r1, unsigned short g1, unsigned short b1,
      51                 :                  unsigned short r2, unsigned short g2, unsigned short b2)
      52                 : {
      53               0 :     r1 >>= 8; g1 >>= 8; b1 >>= 8;
      54               0 :     r2 >>= 8; g2 >>= 8; b2 >>= 8;
      55                 : 
      56               0 :     return ((r2 - r1) * (r2 - r1) +
      57               0 :             (g2 - g1) * (g2 - g1) +
      58               0 :             (b2 - b1) * (b2 - b1));
      59                 : }
      60                 : 
      61                 : cairo_status_t
      62               0 : _cairo_xlib_visual_info_create (Display *dpy,
      63                 :                                 int screen,
      64                 :                                 VisualID visualid,
      65                 :                                 cairo_xlib_visual_info_t **out)
      66                 : {
      67                 :     cairo_xlib_visual_info_t *info;
      68               0 :     Colormap colormap = DefaultColormap (dpy, screen);
      69                 :     XColor color;
      70                 :     int gray, red, green, blue;
      71               0 :     int i, j, distance, min_distance = 0;
      72                 :     XColor colors[256];
      73                 :     unsigned short cube_index_to_short[CUBE_SIZE];
      74                 :     unsigned short ramp_index_to_short[RAMP_SIZE];
      75                 :     unsigned char  gray_to_pseudocolor[RAMP_SIZE];
      76                 : 
      77               0 :     for (i = 0; i < CUBE_SIZE; i++)
      78               0 :         cube_index_to_short[i] = (0xffff * i + ((CUBE_SIZE-1)>>1)) / (CUBE_SIZE-1);
      79               0 :     for (i = 0; i < RAMP_SIZE; i++)
      80               0 :         ramp_index_to_short[i] = (0xffff * i + ((RAMP_SIZE-1)>>1)) / (RAMP_SIZE-1);
      81                 : 
      82               0 :     info = malloc (sizeof (cairo_xlib_visual_info_t));
      83               0 :     if (unlikely (info == NULL))
      84               0 :         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
      85                 : 
      86               0 :     info->visualid = visualid;
      87                 : 
      88                 :     /* Allocate a gray ramp and a color cube.
      89                 :      * Give up as soon as failures start. */
      90                 : 
      91               0 :     for (gray = 0; gray < RAMP_SIZE; gray++) {
      92               0 :         color.red = color.green = color.blue = ramp_index_to_short[gray];
      93               0 :         if (! XAllocColor (dpy, colormap, &color))
      94               0 :             goto DONE_ALLOCATE;
      95                 :     }
      96                 : 
      97                 :     /* XXX: Could do this in a more clever order to have the best
      98                 :      * possible results from early failure. Could also choose a cube
      99                 :      * uniformly distributed in a better space than RGB. */
     100               0 :     for (red = 0; red < CUBE_SIZE; red++) {
     101               0 :         for (green = 0; green < CUBE_SIZE; green++) {
     102               0 :             for (blue = 0; blue < CUBE_SIZE; blue++) {
     103               0 :                 color.red = cube_index_to_short[red];
     104               0 :                 color.green = cube_index_to_short[green];
     105               0 :                 color.blue = cube_index_to_short[blue];
     106               0 :                 color.pixel = 0;
     107               0 :                 color.flags = 0;
     108               0 :                 color.pad = 0;
     109               0 :                 if (! XAllocColor (dpy, colormap, &color))
     110               0 :                     goto DONE_ALLOCATE;
     111                 :             }
     112                 :         }
     113                 :     }
     114                 :   DONE_ALLOCATE:
     115                 : 
     116               0 :     for (i = 0; i < ARRAY_LENGTH (colors); i++)
     117               0 :         colors[i].pixel = i;
     118               0 :     XQueryColors (dpy, colormap, colors, ARRAY_LENGTH (colors));
     119                 : 
     120                 :     /* Search for nearest colors within allocated colormap. */
     121               0 :     for (gray = 0; gray < RAMP_SIZE; gray++) {
     122               0 :         for (i = 0; i < 256; i++) {
     123               0 :             distance = _color_distance (ramp_index_to_short[gray],
     124               0 :                                         ramp_index_to_short[gray],
     125               0 :                                         ramp_index_to_short[gray],
     126               0 :                                         colors[i].red,
     127               0 :                                         colors[i].green,
     128               0 :                                         colors[i].blue);
     129               0 :             if (i == 0 || distance < min_distance) {
     130               0 :                 gray_to_pseudocolor[gray] = colors[i].pixel;
     131               0 :                 min_distance = distance;
     132               0 :                 if (!min_distance)
     133               0 :                     break;
     134                 :             }
     135                 :         }
     136                 :     }
     137               0 :     for (red = 0; red < CUBE_SIZE; red++) {
     138               0 :         for (green = 0; green < CUBE_SIZE; green++) {
     139               0 :             for (blue = 0; blue < CUBE_SIZE; blue++) {
     140               0 :                 for (i = 0; i < 256; i++) {
     141               0 :                     distance = _color_distance (cube_index_to_short[red],
     142               0 :                                                 cube_index_to_short[green],
     143               0 :                                                 cube_index_to_short[blue],
     144               0 :                                                 colors[i].red,
     145               0 :                                                 colors[i].green,
     146               0 :                                                 colors[i].blue);
     147               0 :                     if (i == 0 || distance < min_distance) {
     148               0 :                         info->cube_to_pseudocolor[red][green][blue] = colors[i].pixel;
     149               0 :                         min_distance = distance;
     150               0 :                         if (!min_distance)
     151               0 :                             break;
     152                 :                     }
     153                 :                 }
     154                 :             }
     155                 :         }
     156                 :     }
     157                 : 
     158               0 :     for (i = 0, j = 0; i < 256; i++) {
     159               0 :         if (j < CUBE_SIZE - 1 && (((i<<8)+i) - (int)cube_index_to_short[j]) > ((int)cube_index_to_short[j+1] - ((i<<8)+i)))
     160               0 :             j++;
     161               0 :         info->field8_to_cube[i] = j;
     162                 : 
     163               0 :         info->dither8_to_cube[i] = ((int)i - 128) / (CUBE_SIZE - 1);
     164                 :     }
     165               0 :     for (i = 0, j = 0; i < 256; i++) {
     166               0 :         if (j < RAMP_SIZE - 1 && (((i<<8)+i) - (int)ramp_index_to_short[j]) > ((int)ramp_index_to_short[j+1] - ((i<<8)+i)))
     167               0 :             j++;
     168               0 :         info->gray8_to_pseudocolor[i] = gray_to_pseudocolor[j];
     169                 :     }
     170                 : 
     171               0 :     for (i = 0; i < 256; i++) {
     172               0 :         info->colors[i].a = 0xff;
     173               0 :         info->colors[i].r = colors[i].red   >> 8;
     174               0 :         info->colors[i].g = colors[i].green >> 8;
     175               0 :         info->colors[i].b = colors[i].blue  >> 8;
     176                 :     }
     177                 : 
     178               0 :     *out = info;
     179               0 :     return CAIRO_STATUS_SUCCESS;
     180                 : }
     181                 : 
     182                 : void
     183               0 : _cairo_xlib_visual_info_destroy (cairo_xlib_visual_info_t *info)
     184                 : {
     185                 :     /* No need for XFreeColors() whilst using DefaultColormap */
     186               0 :     free (info);
     187               0 : }

Generated by: LCOV version 1.7