1 /* GNUPLOT - gp_cairo_helpers.c */
2 
3 /*[
4  * Copyright 2009   Timothee Lecomte
5  *
6  * Permission to use, copy, and distribute this software and its
7  * documentation for any purpose with or without fee is hereby granted,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.
11  *
12  * Permission to modify the software is granted, but not the right to
13  * distribute the complete modified source code.  Modifications are to
14  * be distributed as patches to the released version.  Permission to
15  * distribute binaries produced by compiling modified sources is granted,
16  * provided you
17  *   1. distribute the corresponding source modifications from the
18  *    released version in the form of a patch file along with the binaries,
19  *   2. add special version identification to distinguish your version
20  *    in addition to the base release version number,
21  *   3. provide your name and address as the primary contact for the
22  *    support of your modified version, and
23  *   4. retain our contact information in regard to use of the base
24  *    software.
25  * Permission to distribute the released version of the source code along
26  * with corresponding source modifications in the form of a patch file is
27  * granted with same provisions 2 through 4 for binary distributions.
28  *
29  * This software is provided "as is" without express or implied warranty
30  * to the extent permitted by applicable law.
31  *
32  *
33  * Alternatively, the contents of this file may be used under the terms of the
34  * GNU General Public License Version 2 or later (the "GPL"), in which case the
35  * provisions of GPL are applicable instead of those above. If you wish to allow
36  * use of your version of this file only under the terms of the GPL and not
37  * to allow others to use your version of this file under the above gnuplot
38  * license, indicate your decision by deleting the provisions above and replace
39  * them with the notice and other provisions required by the GPL. If you do not
40  * delete the provisions above, a recipient may use your version of this file
41  * under either the GPL or the gnuplot license.
42 ]*/
43 
44 #include "gp_cairo_helpers.h"
45 
46 #include "alloc.h"
47 /* for rgb functions */
48 # include "getcolor.h"
49 
gp_cairo_helper_coordval_to_chars(coordval * image,int M,int N,t_imagecolor color_mode)50 unsigned int * gp_cairo_helper_coordval_to_chars(coordval* image, int M, int N, t_imagecolor color_mode)
51 {
52 	int m,n;
53 	unsigned int *image255, *image255copy;
54 	rgb_color rgb1;
55 	rgb255_color rgb255;
56 
57 	/* cairo image buffer, upper bits are alpha, then r, g and b
58 	 * Depends on endianess */
59 	image255 = (unsigned int*) malloc(M*N*sizeof(unsigned int));
60 	if (!image255) { fprintf(stderr,"cairo terminal: out of memory!\n"); gp_exit(EXIT_FAILURE);}
61 	image255copy = image255;
62 
63 	/* TrueColor 24-bit plot->color mode */
64 	if (color_mode == IC_RGB) {
65 		for (n=0; n<N; n++) {
66 		for (m=0; m<M; m++) {
67 			rgb1.r = *image++;
68 			rgb1.g = *image++;
69 			rgb1.b = *image++;
70 			rgb255_from_rgb1( rgb1, &rgb255 );
71 			*image255copy++ = (0xFF<<24) + (rgb255.r<<16) + (rgb255.g<<8) + rgb255.b;
72 		}
73 		}
74 	} else if (color_mode == IC_RGBA) {
75 		unsigned char alpha255;
76 		double alpha1;
77 		for (n=0; n<N; n++) {
78 		for (m=0; m<M; m++) {
79 			alpha255 = *(image+3);
80 			alpha1 = (float)alpha255 / 255.;
81 			rgb1.r = alpha1 * (*image++);
82 			rgb1.g = alpha1 * (*image++);
83 			rgb1.b = alpha1 * (*image++);
84 			image++;
85 			rgb255_from_rgb1( rgb1, &rgb255 );
86 			*image255copy++ = (alpha255<<24)
87 					+ (rgb255.r<<16) + (rgb255.g<<8) + rgb255.b;
88 		}
89 		}
90 	/* Palette plot->color lookup from gray value */
91 	} else {
92 		for (n=0; n<N; n++) {
93 		for (m=0; m<M; m++) {
94 			if (isnan(*image)) {
95 				image++;
96 				*image255copy++ = 0x00000000;
97 			} else {
98 				rgb255maxcolors_from_gray( *image++, &rgb255 );
99 				*image255copy++ = (0xFF<<24) + (rgb255.r<<16) + (rgb255.g<<8) + rgb255.b;
100 			}
101 		}
102 		}
103 	}
104 
105 	return image255;
106 }
107