1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FONTFORGE_GIMAGE_H
29 #define FONTFORGE_GIMAGE_H
30 
31 #include "basics.h"
32 
33 typedef uint32 Color;
34 
35 #define COLOR_UNKNOWN		((Color) 0xffffffff)
36 #define COLOR_TRANSPARENT	((Color) 0xffffffff)
37 #define COLOR_DEFAULT		((Color) 0xfffffffe)
38 #define COLOR_WHITE		((Color) 0xffffff)
39 #define COLOR_CREATE(r,g,b)	(((r)<<16) | ((g)<<8) | (b))
40 #define COLOR_ALPHA(col)	(((col)>>24))
41 #define COLOR_RED(col)		(((col)>>16) & 0xff)
42 #define COLOR_GREEN(col)	(((col)>>8) & 0xff)
43 #define COLOR_BLUE(col)		((col)&0xff)
44 
45 struct hslrgb {
46     double h,s,l,v;
47     double r,g,b;
48     uint8 rgb, hsl, hsv;
49 };
50 
51 struct hslrgba {
52     double h,s,l,v;
53     double r,g,b;
54     uint8 rgb, hsl, hsv, has_alpha;
55     double alpha;
56 };
57 
58 typedef struct clut {
59     int16 clut_len;
60     unsigned int is_grey: 1;
61     uint32 trans_index;		/* will be ignored for cluts in images, use base->trans instead */
62     Color clut[256];
63 } GClut;
64 
65 #define GCLUT_CLUT_EMPTY \
66 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
67   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
68   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
69   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
70   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
71   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
72   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
73   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
74   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
75   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
76   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
77   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
78   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
79   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
80   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
81   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
82 }
83 
84 
85 typedef struct revcmap RevCMap;
86 
87 enum image_type { it_mono, it_bitmap=it_mono, it_index, it_true, it_rgba };
88 
89 struct _GImage {
90 /* Format: bitmaps are stored with the most significant bit first in byte units
91 	    indexed    images are stored in byte units
92 	    true color images are stored in 4 byte units, 0,red,green,blue
93 	    rgba       images are stored in 4 byte units, alpha,red,green blue
94 */
95     enum image_type image_type: 2;
96     int16 delay;		/* for animated GIFs, delay to next frame */
97     int32 width, height;
98     int32 bytes_per_line;
99     uint8 *data;
100     GClut *clut;
101     Color trans;		/* PNG supports more than one transparent color, we don't */
102 				/* for non-true color images this is the index, not a color */
103 };
104 
105 /* We deal with 1 bit, 8 bit and 32 bit images internal. 1 bit images may have*/
106 /*  a clut (if they don't assume bw, 0==black, 1==white), 8 bit must have a */
107 /*  clut, 32bit are actually 24 bit RGB images, but we pad them for easy */
108 /*  accessing. it_screen means that we've got an image that can be drawn */
109 /*  directly on the screen */
110 typedef struct gimage {
111     short list_len;		/* length of list */
112     union {			/* depends on whether has_list is set */
113 	struct _GImage *image;
114     	struct _GImage **images;
115     } u;
116     void *userdata;
117 } GImage;
118 
119 enum pastetrans_type { ptt_paste_trans_to_trans, ptt_old_shines_through};
120 
121 typedef struct grect {
122     int32 x,y,width,height;
123 } GRect;
124 
125 #define GRECT_EMPTY { 0, 0, 0, 0 }
126 
127 
128 typedef struct gpoint {
129     int16 x,y;
130 } GPoint;
131 
132 #define GPOINT_EMPTY { 0, 0 }
133 
134 
135 extern GImage *GImageCreate(enum image_type type, int32 width, int32 height);
136 extern GImage *_GImage_Create(enum image_type type, int32 width, int32 height);
137 extern void GImageDestroy(GImage *gi);
138 extern GImage *GImageCreateAnimation(GImage **images, int n);
139 extern GImage *GImageAddImageBefore(GImage *dest, GImage *src, int pos);
140 
141 extern Color GImageGetPixelRGBA(GImage *base,int x, int y);
142 extern int GImageGetWidth(GImage *);
143 extern int GImageGetHeight(GImage *);
144 extern void *GImageGetUserData(GImage *img);
145 extern void GImageSetUserData(GImage *img,void *userdata);
146 extern RevCMap *GClutReverse(GClut *clut,int side_size);
147 void GClut_RevCMapFree(RevCMap *rev);
148 extern GClut *GImageFindCLUT(GImage *image,GClut *clut,int clutmax);
149 extern int GImageGreyClut(GClut *clut);
150 extern Color _GImage_ColourFName(char *name);
151 extern Color GDrawColorDarken(Color col, int by);
152 extern Color GDrawColorBrighten(Color col, int by);
153 
154 extern int GImageWriteGImage(GImage *gi, char *filename);
155 extern int GImageWrite_Bmp(GImage *gi, FILE *fp);
156 extern int GImageWriteBmp(GImage *gi, char *filename);
157 extern GImage *GImageRead_Bmp(FILE *file);
158 extern GImage *GImageReadBmp(char *filename);
159 extern int GImageWriteXbm(GImage *gi, char *filename);
160 extern GImage *GImageReadXbm(char *filename);
161 extern int GImageWriteXpm(GImage *gi, char *filename);
162 extern GImage *GImageReadXpm(char *filename);
163 extern GImage *GImageReadTiff(char *filename);
164 extern GImage *GImageReadJpeg(char *filename);
165 extern GImage *GImageRead_Jpeg(FILE *fp);
166 extern int GImageWrite_Jpeg(GImage *gi, FILE *outfile, int quality, int progressive);
167 extern int GImageWriteJpeg(GImage *gi, char *filename, int quality, int progressive);
168 extern GImage *GImageRead_Png(FILE *fp);
169 extern GImage *GImageReadPng(char *filename);
170 extern GImage *GImageReadPngBuf(char* buf, size_t sz);
171 extern int GImageWrite_Png(GImage *gi, FILE *fp, int progressive);
172 extern int GImageWritePng(GImage *gi, char *filename, int progressive);
173 extern int GImageWritePngBuf(GImage *gi, char** buf, size_t* sz, int compression_level, int progressive);
174 extern GImage *GImageReadGif(char *filename);
175 extern int GImageWriteGif(GImage *gi,char *filename,int progressive);
176 extern GImage *GImageReadRas(char *filename);		/* Sun Raster */
177 extern GImage *GImageReadRgb(char *filename);		/* SGI */
178 extern GImage *GImageRead(char *filename);
179 
180 extern void GImageDrawRect(GImage *img,GRect *r,Color col);
181 extern void GImageDrawImage(GImage *dest,GImage *src,GRect *junk,int x, int y);
182 extern void GImageBlendOver(GImage *dest,GImage *src,GRect *from,int x, int y);
183 
184 extern void gRGB2HSL(struct hslrgb *col);
185 extern void gHSL2RGB(struct hslrgb *col);
186 extern void gRGB2HSV(struct hslrgb *col);
187 extern void gHSV2RGB(struct hslrgb *col);
188 extern void gColor2Hslrgb(struct hslrgb *col,Color from);
189 extern void gColor2Hslrgba(struct hslrgba *col,Color from);
190 extern Color gHslrgb2Color(struct hslrgb *col);
191 extern Color gHslrgba2Color(struct hslrgba *col);
192 
193 #endif /* FONTFORGE_GIMAGE_H */
194