2 * Ocaml-Gd. An interface to the Gd library for generating simple images
3 * Based on Shawn Wagner's OCamlGD 0.7.0. with some mods from GD4O
4 * Copyright (C) 2002 Shawn Wagner
5 * Copyright (C) 2003 Matthew C. Gushee
6 * Copyright (C) 2005 beedauchon
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 exception Too_many_colors
24 exception Color_not_found
25 exception Image_creation_failed
26 exception Not_supported
27 exception Illegal_state
of string
29 let _ = Callback.register_exception
"gdopen failed" Image_creation_failed
30 let _ = Callback.register_exception
"gd type not supported" Not_supported
32 type t
(* Image type *)
33 type c
= int (* Color type *)
34 type font
(* Font type *)
38 method virtual red_part
: int
39 method virtual green_part
: int
40 method virtual blue_part
: int
41 method virtual code
: int
44 class virtual color_allocator
=
46 method virtual create
: red
:int -> green
:int -> blue
:int -> color
47 method virtual closest
: red
:int -> green
:int -> blue
:int -> color
48 method virtual closest_hwb
: red
:int -> green
:int -> blue
:int -> color
49 method virtual resolve
: red
:int -> green
:int -> blue
:int -> color
50 method virtual exact
: red
:int -> green
:int -> blue
:int -> color
51 method virtual find
: red
:int -> green
:int -> blue
:int -> color
52 method virtual white
: color
53 method virtual black
: color
54 method virtual blue
: color
55 method virtual green
: color
56 method virtual red
: color
57 method virtual get_transparent
: color
58 method virtual set_transparent
: color
-> unit
59 method virtual transparent
: unit -> int
64 method virtual colors
: color_allocator
65 method virtual line
: x1
:int -> y1
:int -> x2
:int -> y2
:int -> color
-> unit
66 method virtual dashed_line
: x1
:int -> y1
:int -> x2
:int -> y2
:int -> color
-> unit
67 method virtual rectangle
: x1
:int -> y1
:int -> x2
:int -> y2
:int -> color
-> unit
68 method virtual filled_rectangle
: x1
:int -> y1
:int -> x2
:int -> y2
:int -> color
-> unit
69 method virtual arc
: cx
:int -> cy
:int -> w
:int -> h
:int -> s
:int -> e
:int -> color
-> unit
70 method virtual arc_fill
: cx
:int -> cy
:int -> w
:int -> h
:int -> s
:int -> e
:int -> color
-> style
:int -> unit
71 method virtual border_fill
: x
:int -> y
:int -> border
:color
-> fill
:color
-> unit
72 method virtual fill
: x
:int -> y
:int -> color
-> unit
73 method virtual save_as_png
: string -> unit
74 method virtual save_as_jpeg
: ?quality
:int -> string -> unit
75 method virtual out_as_png
: out_channel
-> unit
76 method virtual out_as_jpeg
: ?quality
:int -> out_channel
-> unit
77 method virtual set_pixel
: x
:int -> y
:int -> color
-> unit
78 method virtual get_pixel
: x
:int -> y
:int -> color
79 method virtual width
: int
80 method virtual height
: int
81 method virtual in_range
: x
:int -> y
:int -> bool
82 method virtual letter
: font
:font
-> x
:int -> y
:int -> c
:char
-> color
-> unit
83 method virtual letter_up
: font
:font
-> x
:int -> y
:int -> c
:char
-> color
-> unit
84 method virtual string: font
:font
-> x
:int -> y
:int -> s
:string -> color
-> unit
85 method virtual string_up
: font
:font
-> x
:int -> y
:int -> s
:string -> color
-> unit
88 (* Private interface routines. *)
90 external do_image_create
: int -> int -> t
= "ml_image_create"
91 external do_image_open_png
: string -> t
= "ml_image_open_png"
92 external do_image_open_jpeg
: string -> t
= "ml_image_open_jpeg"
94 (* Drawing functions *)
95 external do_set_pixel
: t
-> int -> int -> int -> unit = "ml_set_pixel"
97 external do_get_pixel
: t
-> int -> int -> int = "ml_get_pixel"
99 external do_get_width
: t
-> int = "ml_get_width"
100 external do_get_height
: t
-> int = "ml_get_height"
102 external do_draw_line
: t
-> int -> int -> int -> int -> int -> unit
103 = "ml_image_line" "ml_image_line_native"
105 external do_draw_dline
: t
-> int -> int -> int -> int -> int -> unit
106 = "ml_image_dline" "ml_image_dline_native"
108 external do_draw_rect
: t
-> int -> int -> int -> int -> int -> unit
109 = "ml_image_rect" "ml_image_rect_native"
111 external do_draw_frect
: t
-> int -> int -> int -> int -> int -> unit
112 = "ml_image_frect" "ml_image_frect_native"
114 external do_draw_arc
: t
-> int -> int -> int -> int -> int -> int -> int -> unit
115 = "ml_image_arc" "ml_image_arc_native"
117 external do_draw_arc_fill
: t
-> int -> int -> int -> int -> int -> int -> int -> int -> unit
118 = "ml_image_arc_fill" "ml_image_arc_fill_native"
120 external do_border_fill
: t
-> int -> int -> int -> int -> unit
121 = "ml_image_border_fill" "ml_image_border_fill_native"
123 external do_fill
: t
-> int -> int -> int -> unit
126 external do_save_png
: t
-> string -> unit = "ml_save_png"
127 external do_save_jpeg
: t
-> string -> int -> unit = "ml_save_jpeg"
129 external do_dump_png
: t
-> out_channel
-> unit = "ml_dump_png"
130 external do_dump_jpeg
: t
-> out_channel
-> int -> unit = "ml_dump_jpeg"
133 (* External functions related to colors *)
134 external do_color_create
: t
-> red
:int -> green
:int -> blue
:int -> c
135 = "ml_image_color_alloc"
137 external do_find_closest
: t
-> red
:int -> green
:int -> blue
:int -> c
138 = "ml_image_color_closest"
140 external do_find_closest_hwb
: t
-> red
:int -> green
:int -> blue
:int -> c
141 = "ml_image_color_closest_hwb"
143 external do_find_exact
: t
-> red
:int -> green
:int -> blue
:int -> c
144 = "ml_image_color_exact"
146 external do_resolve
: t
-> red
:int -> green
:int -> blue
:int -> c
147 = "ml_image_color_resolve"
149 external do_green_part
: t
-> int -> int = "ml_image_green_part"
150 external do_red_part
: t
-> int -> int = "ml_image_red_part"
151 external do_blue_part
: t
-> int -> int = "ml_image_blue_part"
152 external do_get_transparent
: t
-> int = "ml_image_get_transparent"
153 external do_set_transparent
: t
-> int -> unit = "ml_image_set_transparent"
155 external do_get_font
: int -> font
= "ml_get_font"
157 external do_draw_char
: t
-> font
-> int -> int -> char
-> int -> unit
158 = "ml_image_char" "ml_image_char_native"
160 external do_draw_charu
: t
-> font
-> int -> int -> char
-> int -> unit
161 = "ml_image_charu" "ml_image_charu_native"
163 external do_draw_str
: t
-> font
-> int -> int -> string -> int -> unit
164 = "ml_image_str" "ml_image_str_native"
166 external do_draw_stru
: t
-> font
-> int -> int -> string -> int -> unit
167 = "ml_image_stru" "ml_image_stru_native"
169 external png_version
: unit -> int32
= "ml_image_pngversion"
173 let tiny = do_get_font
0
174 let small = do_get_font
1
175 let medium = do_get_font
2
176 let large = do_get_font
3
177 let giant = do_get_font
4
181 (* Implementation classes *)
182 class gdColor im col
=
186 method blue_part
= do_blue_part im col
187 method red_part
= do_red_part im col
188 method green_part
= do_green_part im col
191 class gd_color_allocator im
=
193 inherit color_allocator
195 val mutable transparent_pcolor
= true
197 method create ~red ~green ~blue
=
198 let color = do_color_create im ~red ~green ~blue
in
199 if color = -1 then raise Too_many_colors
else new gdColor im
color
201 method closest ~red ~green ~blue
=
202 let color = do_find_closest im ~red ~green ~blue
in
203 if color = -1 then raise Color_not_found
else new gdColor im
color
205 method closest_hwb ~red ~green ~blue
=
206 let color = do_find_closest_hwb im ~red ~green ~blue
in
207 if color = -1 then raise Color_not_found
else new gdColor im
color
209 method exact ~red ~green ~blue
=
210 let color = do_find_exact im ~red ~green ~blue
in
211 if color = -1 then raise Color_not_found
else new gdColor im
color
213 method resolve ~red ~green ~blue
=
214 let color = do_resolve im ~red ~green ~blue
in
215 if color = -1 then raise Color_not_found
else new gdColor im
color
217 method find ~red ~green ~blue
=
218 let color = do_find_exact im ~red ~green ~blue
in
222 let color = do_color_create im ~red ~blue ~green
in
223 if color = -1 then raise Too_many_colors
else new gdColor im
color
225 method black
= this#find ~red
:0 ~blue
:0 ~green
:0
226 method white
= this#find ~red
:255 ~blue
:255 ~green
:255
227 method blue
= this#find ~blue
:255 ~red
:0 ~green
:0
228 method green
= this#find ~green
:255 ~red
:0 ~blue
:0
229 method red
= this#find ~red
:255 ~green
:0 ~blue
:0
230 method get_transparent
=
231 let cindex = do_get_transparent im
in
232 if cindex = -1 then raise Color_not_found
233 else new gdColor im
cindex
234 method set_transparent
color =
235 do_set_transparent im
color#code
236 method transparent
() =
237 if transparent_pcolor
then 5
238 else raise
(Illegal_state
239 "Transparent pseudocolor is disabled.")
245 method private get_image
= im
246 val c_a
= new gd_color_allocator im
249 method line ~x1 ~y1 ~x2 ~y2
color =
250 do_draw_line im x1 y1 x2 y2
color#code
252 method dashed_line ~x1 ~y1 ~x2 ~y2
color =
253 do_draw_dline im x1 y1 x2 y2
color#code
255 method rectangle ~x1 ~y1 ~x2 ~y2
color =
256 do_draw_rect im x1 y1 x2 y2
color#code
258 method filled_rectangle ~x1 ~y1 ~x2 ~y2
color =
259 do_draw_frect im x1 y1 x2 y2
color#code
261 method arc ~cx ~cy ~w ~h ~s ~e
color =
262 do_draw_arc im cx cy w h s e
color#code
264 method arc_fill ~cx ~cy ~w ~h ~s ~e
color ~style
=
265 do_draw_arc_fill im cx cy w h s e
color#code style
267 method border_fill ~x ~y ~border ~fill
=
268 do_border_fill im x y
(border#code
) (fill#code
)
270 method fill ~x ~y
color =
271 do_fill im x y
color#code
273 method letter ~font ~x ~y ~c
color =
274 do_draw_char im font x y c
color#code
276 method letter_up ~font ~x ~y ~c
color =
277 do_draw_charu im font x y c
color#code
279 method string ~font ~x ~y ~s
color =
280 do_draw_str im font x y s
color#code
282 method string_up ~font ~x ~y ~s
color =
283 do_draw_stru im font x y s
color#code
285 method save_as_png filename
= do_save_png im filename
286 method save_as_jpeg ?
(quality
= -1) filename
=
287 do_save_jpeg im filename quality
289 method out_as_png channel
= do_dump_png im channel
290 method out_as_jpeg ?
(quality
= -1) channel
=
291 do_dump_jpeg im channel quality
293 method set_pixel ~x ~y
color =
294 do_set_pixel im x y
color#code
296 method get_pixel ~x ~y
=
297 new gdColor im
(do_get_pixel im x y
)
299 method width
= do_get_width im
300 method height
= do_get_height im
302 method in_range ~x ~y
=
303 x
>= 0 && x
<= (do_get_width im
) && y
>= 0 && y
<= (do_get_height im
)
306 (* Image creation functions *)
307 let create ~
(x
:int) ~
(y
:int) =
308 new gdImage
(do_image_create x y
)
310 let open_png filename
=
311 new gdImage
(do_image_open_png filename
)
313 let open_jpeg filename
=
314 new gdImage
(do_image_open_jpeg filename
)