123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841(* $Header: /home/cvs/gd4o/gd.ml,v 1.6 2003/11/25 01:02:32 matt Exp $ *)(*
* GD4O: An OCaml interface to the Gd graphics library.
* Based on Shawn Wagner's OCamlGD 0.7.0.
* Copyright (C) 2002 Shawn Wagner
* Copyright (C) 2003 Matthew C. Gushee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)exceptionToo_many_colorsexceptionColor_not_foundexceptionImage_creation_failedexceptionNot_supportedexceptionIllegal_stateofstringexceptionGD_Freetype_exceptionofstringlet_=Callback.register_exception"gdopen failed"Image_creation_failedlet_=Callback.register_exception"gd type not supported"Not_supportedlet_=Callback.register_exception"gd freetype exception"(GD_Freetype_exception"msg")typet(* Image type *)typec=int(* Color type *)typefont(* Font type *)typeftex_flag=FTExSetSpacing|FTExSetCharmaptypeftex_charmap=FTExUnicode|FTExShiftJIS|FTExBig5(*
class virtual color =
object
method virtual red_part: int
method virtual green_part: int
method virtual blue_part: int
method virtual code: int
method virtual antialiased: color
method virtual is_aa: bool
end
*)typecolor={red_channel:int;green_channel:int;blue_channel:int;alpha_channel:int;index:int;}classvirtualcolor_allocator=objectmethodvirtualcreate:red:int->green:int->blue:int->colormethodvirtualclosest:red:int->green:int->blue:int->colormethodvirtualclosest_hwb:red:int->green:int->blue:int->colormethodvirtualresolve:red:int->green:int->blue:int->colormethodvirtualexact:red:int->green:int->blue:int->colormethodvirtualfind:red:int->green:int->blue:int->colormethodvirtualget_color_by_index:int->colormethodvirtualnew_ml_color:int->colormethodvirtualwhite:colormethodvirtualblack:colormethodvirtualblue:colormethodvirtualgreen:colormethodvirtualred:colormethodvirtualget_transparent:colormethodvirtualset_transparent:color->unitmethodvirtualset_antialiased:bool->unitmethodvirtualset_brushed:bool->unitmethodvirtualset_styled:bool->unitmethodvirtualset_tiled:bool->unitmethodvirtualantialiased:unit->intmethodvirtualbrushed:unit->intmethodvirtualstyled:unit->intmethodvirtualstyled_brushed:unit->intmethodvirtualtiled:unit->intmethodvirtualtransparent:unit->intendclassvirtualimage=objectmethodvirtualget_image:tmethodvirtualcolors:color_allocatormethodvirtualline:x1:int->y1:int->x2:int->y2:int->?pseudo:int->color->unitmethodvirtualdashed_line:x1:int->y1:int->x2:int->y2:int->?pseudo:int->color->unitmethodvirtualrectangle:x1:int->y1:int->x2:int->y2:int->?pseudo:int->color->unitmethodvirtualfilled_rectangle:x1:int->y1:int->x2:int->y2:int->?pseudo:int->color->unitmethodvirtualpolygon:pts:(int*int)array->?pseudo:int->color->unitmethodvirtualfilled_polygon:pts:(int*int)array->?pseudo:int->color->unitmethodvirtualarc:cx:int->cy:int->w:int->h:int->s:int->e:int->?pseudo:int->color->unitmethodvirtualclosed_arc:cx:int->cy:int->w:int->h:int->s:int->e:int->?nofill:bool->?edged:bool->?pseudo:int->color->unitmethodvirtualclosed_chord:cx:int->cy:int->w:int->h:int->s:int->e:int->?nofill:bool->?edged:bool->?pseudo:int->color->unitmethodvirtualfilled_ellipse:cx:int->cy:int->w:int->h:int->?pseudo:int->color->unitmethodvirtualborder_fill:x:int->y:int->border:color->fill:color->unitmethodvirtualfill:x:int->y:int->color->unitmethodvirtualset_antialiased:color->unitmethodvirtualset_antialiased_dont_blend:aacolor:color->dontblend:color->unitmethodvirtualset_brush:image->unitmethodvirtualset_tile:image->unitmethodvirtualset_thickness:int->unitmethodvirtualset_clip:x1:int->y1:int->x2:int->y2:int->unitmethodvirtualsave_as_png:string->unitmethodvirtualsave_as_jpeg:?quality:int->string->unitmethodvirtualout_as_png:out_channel->unitmethodvirtualout_as_jpeg:?quality:int->out_channel->unitmethodvirtualset_pixel:x:int->y:int->color->unitmethodvirtualget_pixel:x:int->y:int->colormethodvirtualwidth:intmethodvirtualheight:intmethodvirtualin_range:x:int->y:int->boolmethodvirtualletter:font:font->x:int->y:int->c:char->color->unitmethodvirtualletter_up:font:font->x:int->y:int->c:char->color->unitmethodvirtualstring:font:font->x:int->y:int->s:string->color->unitmethodvirtualstring_up:font:font->x:int->y:int->s:string->color->unitmethodvirtualstring_ft:fg:color->fname:string->size:float->angle:float->x:int->y:int->string->intarraymethodvirtualstring_ftex:fg:color->fname:string->size:float->angle:float->x:int->y:int->?flags:ftex_flagarray->?spacing:float->?charmap:ftex_charmap->string->intarraymethodvirtualcopy:image->x:int->y:int->src_x:int->src_y:int->w:int->h:int->unitmethodvirtualcopy_resized:image->x:int->y:int->src_x:int->src_y:int->w:int->h:int->src_w:int->src_h:int->unitmethodvirtualcopy_resampled:image->x:int->y:int->src_x:int->src_y:int->w:int->h:int->src_w:int->src_h:int->unitmethodvirtualcopy_rotated:image->x:float->y:float->src_x:int->src_y:int->w:int->h:int->angle:int->unitmethodvirtualcopy_merge:image->x:int->y:int->src_x:int->src_y:int->w:int->h:int->pct:int->unitmethodvirtualcopy_merge_gray:image->x:int->y:int->src_x:int->src_y:int->w:int->h:int->pct:int->unitmethodvirtualpalette_copy:image->unitend(* Private interface routines. *)(* Create an image *)externaldo_image_create:int->int->t="ml_image_create"externaldo_image_create_truecolor:int->int->t="ml_image_create_truecolor"externaldo_image_open_png:string->t="ml_image_open_png"externaldo_image_open_jpeg:string->t="ml_image_open_jpeg"externaldo_is_truecolor:t->bool="ml_image_is_truecolor"(* Drawing functions *)externaldo_set_pixel:t->int->int->int->unit="ml_set_pixel"externaldo_get_pixel:t->int->int->int="ml_get_pixel"externaldo_get_width:t->int="ml_get_width"externaldo_get_height:t->int="ml_get_height"externaldo_draw_line:t->int->int->int->int->int->int->unit="ml_image_line""ml_image_line_native"externaldo_draw_dline:t->int->int->int->int->int->int->unit="ml_image_dline""ml_image_dline_native"externaldo_draw_rect:t->int->int->int->int->int->int->unit="ml_image_rect""ml_image_rect_native"externaldo_draw_frect:t->int->int->int->int->int->int->unit="ml_image_frect""ml_image_frect_native"externaldo_draw_poly:t->(int*int)array->int->int->int->unit="ml_image_poly"externaldo_draw_fpoly:t->(int*int)array->int->int->int->unit="ml_image_fpoly"externaldo_draw_arc:t->int->int->int->int->int->int->int->int->unit="ml_image_arc""ml_image_arc_native"externaldo_draw_carc:t->int->int->int->int->int->int->int->int->bool->bool->unit="ml_image_carc""ml_image_carc_native"externaldo_draw_cchord:t->int->int->int->int->int->int->int->int->bool->bool->unit="ml_image_cchord""ml_image_cchord_native"externaldo_draw_fell:t->int->int->int->int->int->int->unit="ml_image_fell""ml_image_fell_native"externaldo_border_fill:t->int->int->int->int->unit="ml_image_border_fill""ml_image_border_fill_native"externaldo_fill:t->int->int->int->unit="ml_image_fill"externaldo_set_antialiased:t->int->unit="ml_image_set_antialiased"externaldo_set_antialiased_dont_blend:t->int->int->unit="ml_image_set_antialiased_dont_blend"externaldo_set_brush:t->t->unit="ml_image_set_brush"externaldo_set_tile:t->t->unit="ml_image_set_tile"externaldo_set_thickness:t->int->unit="ml_image_set_thickness"externaldo_set_clip:t->int->int->int->int->unit="ml_image_set_clip"externaldo_save_png:t->string->unit="ml_save_png"externaldo_save_jpeg:t->string->int->unit="ml_save_jpeg"externaldo_dump_png:t->out_channel->unit="ml_dump_png"externaldo_dump_jpeg:t->out_channel->int->unit="ml_dump_jpeg"(* External functions related to colors *)externaldo_color_create:t->red:int->green:int->blue:int->c="ml_image_color_alloc"externaldo_find_closest:t->red:int->green:int->blue:int->c="ml_image_color_closest"externaldo_find_closest_hwb:t->red:int->green:int->blue:int->c="ml_image_color_closest_hwb"externaldo_find_exact:t->red:int->green:int->blue:int->c="ml_image_color_exact"externaldo_resolve:t->red:int->green:int->blue:int->c="ml_image_color_resolve"externaldo_green_channel:t->int->int="ml_image_green_channel"externaldo_red_channel:t->int->int="ml_image_red_channel"externaldo_blue_channel:t->int->int="ml_image_blue_channel"externaldo_alpha_channel:t->int->int="ml_image_alpha_channel"externaldo_get_transparent:t->int="ml_image_get_transparent"externaldo_set_transparent:t->int->unit="ml_image_set_transparent"externaldo_get_font:int->font="ml_get_font"externaldo_draw_char:t->font->int->int->char->int->unit="ml_image_char""ml_image_char_native"externaldo_draw_charu:t->font->int->int->char->int->unit="ml_image_charu""ml_image_charu_native"externaldo_draw_str:t->font->int->int->string->int->unit="ml_image_str""ml_image_str_native"externaldo_draw_stru:t->font->int->int->string->int->unit="ml_image_stru""ml_image_stru_native"externaldo_draw_str_ft:t->int->string->float->float->int->int->string->intarray="ml_image_str_ft""ml_image_str_ft_native"externaldo_draw_str_ftex:t->int->string->float->float->int->int->ftex_flagarray->float->ftex_charmap->string->intarray="ml_image_str_ftex""ml_image_str_ftex_native"externaldo_ft_bbox:string->float->float->int->int->string->intarray="ml_image_ft_bbox""ml_image_ft_bbox_native"externaldo_ftex_bbox:string->float->float->int->int->ftex_flagarray->float->ftex_charmap->string->intarray="ml_image_ftex_bbox""ml_image_ftex_bbox_native"externaldo_copy:t->t->x:int->y:int->src_x:int->src_y:int->w:int->h:int->unit="ml_image_copy""ml_image_copy_native"externaldo_copy_resized:t->t->x:int->y:int->src_x:int->src_y:int->w:int->h:int->src_w:int->src_h:int->unit="ml_image_copy_resized""ml_image_copy_resized_native"externaldo_copy_resampled:t->t->x:int->y:int->src_x:int->src_y:int->w:int->h:int->src_w:int->src_h:int->unit="ml_image_copy_resampled""ml_image_copy_resampled_native"externaldo_copy_rotated:t->t->x:float->y:float->src_x:int->src_y:int->w:int->h:int->angle:int->unit="ml_image_copy_rotated""ml_image_copy_rotated_native"externaldo_copy_merge:t->t->x:int->y:int->src_x:int->src_y:int->w:int->h:int->pct:int->unit="ml_image_copy_merge""ml_image_copy_merge_native"externaldo_copy_merge_gray:t->t->x:int->y:int->src_x:int->src_y:int->w:int->h:int->pct:int->unit="ml_image_copy_merge_gray""ml_image_copy_merge_gray_native"externaldo_palette_copy:t->t->unit="ml_image_palette_copy"moduleFont=structlettiny=do_get_font0letsmall=do_get_font1letmedium=do_get_font2letlarge=do_get_font3letgiant=do_get_font4end(* Implementation classes *)(*
class gdColor im col =
object(self)
inherit color
val antialias_color = false
method code = col
method blue_part = do_blue_part im col
method red_part = do_red_part im col
method green_part = do_green_part im col
method antialiased = ({< antialias_color = true >} :> color)
method is_aa = antialias_color
end
*)classvirtualgd_color_allocatorim=object(self)inheritcolor_allocatorvalmutableaa_pcolor=falsevalmutablebrushed_pcolor=falsevalmutablestyled_pcolor=falsevalmutablestyled_brushed_pcolor=falsevalmutabletiled_pcolor=falsevalmutabletransparent_pcolor=truemethodcreate~red~green~blue=letcindex=do_color_createim~red~green~blueinifcindex=-1thenraiseToo_many_colorselseself#new_ml_colorcindexmethodclosest~red~green~blue=letcindex=do_find_closestim~red~green~blueinifcindex=-1thenraiseColor_not_foundelseself#new_ml_colorcindexmethodclosest_hwb~red~green~blue=letcindex=do_find_closest_hwbim~red~green~blueinifcindex=-1thenraiseColor_not_foundelseself#new_ml_colorcindexmethodexact~red~green~blue=letcindex=do_find_exactim~red~green~blueinifcindex=-1thenraiseColor_not_foundelseself#new_ml_colorcindexmethodresolve~red~green~blue=letcindex=do_resolveim~red~green~blueinifcindex=-1thenraiseColor_not_foundelseself#new_ml_colorcindexmethodfind~red~green~blue=letcindex=do_find_exactim~red~green~blueinifcindex<>-1thenself#new_ml_colorcindexelse(letcindex=do_color_createim~red~blue~greeninifcindex=-1thenraiseToo_many_colorselseself#new_ml_colorcindex)methodblack=self#find~red:0~blue:0~green:0methodwhite=self#find~red:255~blue:255~green:255methodblue=self#find~blue:255~red:0~green:0methodgreen=self#find~green:255~red:0~blue:0methodred=self#find~red:255~green:0~blue:0methodget_transparent=letcindex=do_get_transparentiminifcindex=-1thenraiseColor_not_foundelseself#new_ml_colorcindexmethodset_transparentcolor=do_set_transparentimcolor.indexmethodset_antialiasedenable=aa_pcolor<-enablemethodset_brushedenable=brushed_pcolor<-enable;styled_brushed_pcolor<-enable&&styled_pcolormethodset_styledenable=styled_pcolor<-enable;styled_brushed_pcolor<-enable&&brushed_pcolormethodset_tiledenable=tiled_pcolor<-enablemethodantialiased()=ifaa_pcolorthen0elseraise(Illegal_state"You must call 'set_antialiased' before calling 'antialiased'.")methodbrushed()=ifbrushed_pcolorthen1elseraise(Illegal_state"You must call 'set_brushed' before calling 'brushed'.")methodstyled()=ifstyled_pcolorthen2elseraise(Illegal_state"You must call 'set_styled' before calling 'styled'.")methodstyled_brushed()=ifstyled_brushed_pcolorthen3elseraise(Illegal_state"You must call 'set_brushed' and 'set_styled' before calling \n\
\ 'styled_brushed'.")methodtiled()=iftiled_pcolorthen4elseraise(Illegal_state"You must call 'set_tiled' before calling 'tiled'.")methodtransparent()=iftransparent_pcolorthen5elseraise(Illegal_state"Transparent pseudocolor is disabled.")endclassgd_8bit_color_allocatorim=object(self)inheritgd_color_allocatorimvalcolors=Array.make256{index=-1;red_channel=-1;green_channel=-1;blue_channel=-1;alpha_channel=-1;}methodprivatenew_ml_coloridx=letmc={index=idx;red_channel=do_red_channelimidx;green_channel=do_green_channelimidx;blue_channel=do_blue_channelimidx;alpha_channel=do_alpha_channelimidx;}incolors.(idx)<-mc;mcmethodget_color_by_indexidx=letc=colors.(idx)inifc.index=-1thenself#new_ml_coloridxelsecendclassgd_truecolor_allocatorim=object(self)inheritgd_color_allocatorimvalcolors:(int,color)Hashtbl.t=Hashtbl.create1024methodprivatenew_ml_coloridx=letmc={index=idx;red_channel=do_red_channelimidx;green_channel=do_green_channelimidx;blue_channel=do_blue_channelimidx;alpha_channel=do_alpha_channelimidx;}inHashtbl.replacecolorsidxmc;mcmethodget_color_by_indexidx=tryHashtbl.findcolorsidxwithNot_found->self#new_ml_coloridxendclassvirtualgdImageim=object(self)inheritimagemethodget_image=immethodline~x1~y1~x2~y2?(pseudo=-1)color=do_draw_lineimx1y1x2y2color.indexpseudomethoddashed_line~x1~y1~x2~y2?(pseudo=-1)color=do_draw_dlineimx1y1x2y2color.indexpseudomethodrectangle~x1~y1~x2~y2?(pseudo=-1)color=do_draw_rectimx1y1x2y2color.indexpseudomethodfilled_rectangle~x1~y1~x2~y2?(pseudo=-1)color=do_draw_frectimx1y1x2y2color.indexpseudomethodpolygon~pts?(pseudo=-1)color=do_draw_polyimpts(Array.lengthpts)color.indexpseudomethodfilled_polygon~pts?(pseudo=-1)color=do_draw_fpolyimpts(Array.lengthpts)color.indexpseudomethodarc~cx~cy~w~h~s~e?(pseudo=-1)color=do_draw_arcimcxcywhsecolor.indexpseudomethodclosed_arc~cx~cy~w~h~s~e?(nofill=false)?(edged=false)?(pseudo=-1)color=do_draw_carcimcxcywhsecolor.indexpseudonofilledgedmethodclosed_chord~cx~cy~w~h~s~e?(nofill=false)?(edged=false)?(pseudo=-1)color=do_draw_cchordimcxcywhsecolor.indexpseudonofilledgedmethodfilled_ellipse~cx~cy~w~h?(pseudo=-1)color=do_draw_fellimcxcywhcolor.indexpseudomethodborder_fill~x~y~border~fill=do_border_fillimxyborder.indexfill.indexmethodfill~x~ycolor=do_fillimxycolor.indexmethodset_antialiasedcol=self#colors#set_antialiasedtrue;do_set_antialiasedimcol.indexmethodset_antialiased_dont_blend~aacolor~dontblend=self#colors#set_antialiasedtrue;do_set_antialiased_dont_blendimaacolor.indexdontblend.indexmethodset_brushbr=self#colors#set_brushedtrue;do_set_brushimbr#get_imagemethodset_tileti=self#colors#set_tiledtrue;do_set_tileimti#get_imagemethodset_thicknessth=do_set_thicknessimthmethodset_clip~x1~y1~x2~y2=do_set_clipimx1y1x2y2methodletter~font~x~y~ccolor=do_draw_charimfontxyccolor.indexmethodletter_up~font~x~y~ccolor=do_draw_charuimfontxyccolor.indexmethodstring~font~x~y~scolor=do_draw_strimfontxyscolor.indexmethodstring_up~font~x~y~scolor=do_draw_struimfontxyscolor.indexmethodstring_ft~fg~fname~size~angle~x~ytext=do_draw_str_ftimfg.indexfnamesizeanglexytextmethodstring_ftex~fg~fname~size~angle~x~y?(flags=[||])?(spacing=1.05)?(charmap=FTExUnicode)text=do_draw_str_fteximfg.indexfnamesizeanglexyflagsspacingcharmaptextmethodsave_as_pngfilename=do_save_pngimfilenamemethodsave_as_jpeg?(quality=-1)filename=do_save_jpegimfilenamequalitymethodout_as_pngchannel=do_dump_pngimchannelmethodout_as_jpeg?(quality=-1)channel=do_dump_jpegimchannelqualitymethodset_pixel~x~ycolor=do_set_pixelimxycolor.indexmethodget_pixel~x~y=self#colors#get_color_by_index(do_get_pixelimxy)methodwidth=do_get_widthimmethodheight=do_get_heightimmethodin_range~x~y=x>=0&&x<=do_get_widthim&&y>=0&&y<=do_get_heightimmethodcopysrc~x~y~src_x~src_y~w~h=do_copyimsrc#get_image~x~y~src_x~src_y~w~hmethodcopy_resizedsrc~x~y~src_x~src_y~w~h~src_w~src_h=do_copy_resizedimsrc#get_image~x~y~src_x~src_y~w~h~src_w~src_hmethodcopy_resampledsrc~x~y~src_x~src_y~w~h~src_w~src_h=do_copy_resampledimsrc#get_image~x~y~src_x~src_y~w~h~src_w~src_hmethodcopy_rotatedsrc~x~y~src_x~src_y~w~h~angle=do_copy_rotatedimsrc#get_image~x~y~src_x~src_y~w~h~anglemethodcopy_mergesrc~x~y~src_x~src_y~w~h~pct=do_copy_mergeimsrc#get_image~x~y~src_x~src_y~w~h~pctmethodcopy_merge_graysrc~x~y~src_x~src_y~w~h~pct=do_copy_merge_grayimsrc#get_image~x~y~src_x~src_y~w~h~pctmethodpalette_copysrc=do_palette_copyimsrc#get_imageend(* 8-bit (indexed-color) image *)classgdImage8im=objectinheritgdImageimvalc_a=newgd_8bit_color_allocatorimmethodcolors=c_aend(* Truecolor image *)classgdImageTim=objectinheritgdImageimvalc_a=newgd_truecolor_allocatorimmethodcolors=c_aendletis_truecolorim=do_is_truecolorimletft_bbox~fname~size~angle~x~ytext=do_ft_bboxfnamesizeanglexytextletftex_bbox~fname~size~angle~x~y?(flags=[||])?(spacing=1.05)?(charmap=FTExUnicode)text=do_ftex_bboxfnamesizeanglexyflagsspacingcharmaptext(* Image creation functions *)letcreate~(x:int)~(y:int)=newgdImage8(do_image_createxy)letcreate_truecolor~(x:int)~(y:int)=newgdImageT(do_image_create_truecolorxy)letopen_pngfilename=letim=do_image_open_pngfilenameinifis_truecolorimthennewgdImageTimelsenewgdImage8imletopen_jpegfilename=newgdImageT(do_image_open_jpegfilename)