Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file rgba32.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310(***********************************************************************)(* *)(* Objective Caml *)(* *)(* François Pessaux, projet Cristal, INRIA Rocquencourt *)(* Pierre Weis, projet Cristal, INRIA Rocquencourt *)(* Jun Furuse, projet Cristal, INRIA Rocquencourt *)(* *)(* Copyright 1999-2004, *)(* Institut National de Recherche en Informatique et en Automatique. *)(* Distributed only by permission. *)(* *)(***********************************************************************)(* $Id: rgba32.ml,v 1.5 2009/07/04 03:39:28 furuse Exp $ *)openUtilmoduleE=structopenColortypet=Color.rgbaletbytes_per_pixel=4letgetstrpos={color={r=str@%pos;g=str@%pos+1;b=str@%pos+2};alpha=str@%pos+3}letsetstrpost=str<<pos&char_of_intt.color.r;str<<pos+1&char_of_intt.color.g;str<<pos+2&char_of_intt.color.b;str<<pos+3&char_of_intt.alphaletmaket=letstr=Bytes.createbytes_per_pixelinsetstr0t;strendmoduleRI=Genimage.MakeRawImage(E)typerawimage=RI.ttypeelt=Color.rgbatypet={width:int;height:int;rawimage:RI.t;mutableinfos:Info.infolist;}moduleC=structtyperawimage=RI.ttypecontainer=tletrawimagex=x.rawimageletcreate_defaultwidthheightrawimage={width=width;height=height;rawimage=rawimage;infos=[];}letcreate_duplicatesrcwidthheightrawimage={width=width;height=height;rawimage=rawimage;infos=src.infos;}endmoduleIMAGE=Genimage.Make(RI)(C)letcreate_withwidthheightinfosdata={width=width;height=height;rawimage=RI.create_withwidthheightdata;infos=infos;}letcreate_with_scanlineswidthheightinfosdata={width=width;height=height;rawimage=RI.create_with_scanlineswidthheightdata;infos=infos;}letrawimage=C.rawimageletcreate=IMAGE.createletmake=IMAGE.makeletdump=IMAGE.dumpletunsafe_access=IMAGE.unsafe_accessletget_strip=IMAGE.get_stripletset_strip=IMAGE.set_stripletget_scanline=IMAGE.get_scanlineletset_scanline=IMAGE.set_scanlineletunsafe_get=IMAGE.unsafe_getletunsafe_set=IMAGE.unsafe_setletget=IMAGE.getletset=IMAGE.setletdestroy=IMAGE.destroyletcopy=IMAGE.copyletsub=IMAGE.subletblit=IMAGE.blitletmap=IMAGE.mapletblocks=IMAGE.blocksletdump_block=IMAGE.dump_blockopenColor(* image resize with smoothing *)(* good result for reducing *)letresize_reduceprogimgnwnh=letnewimage=createnwnhinletxscale=floatnw/.floatimg.widthinletyscale=floatnh/.floatimg.heightinletxs=Array.initnw(funx->letsx=truncate(floatx/.xscale)inletex=truncate((floatx+.0.99)/.xscale)inletdx=ex-sx+1in(sx,ex,dx))inletys=Array.initnh(funy->letsy=truncate(floaty/.yscale)inletey=truncate((floaty+.0.99)/.yscale)inletdy=ey-sy+1in(sy,ey,dy))inforx=0tonw-1doletsx,ex,dx=xs.(x)infory=0tonh-1doletsy,ey,dy=ys.(y)inletsize=dx*dyinletsr=ref0andsg=ref0andsb=ref0andsa=ref0inforxx=sxtoexdoforyy=sytoeydoletc=unsafe_getimgxxyyinsr:=!sr+c.color.r;sg:=!sg+c.color.g;sb:=!sb+c.color.b;sa:=!sa+c.alphadonedone;unsafe_setnewimagexy{color={r=!sr/size;g=!sg/size;b=!sb/size};alpha=!sa/size;}done;matchprogwith|Somep->p(float(x+1)/.floatnw)|None->()done;newimageletresize_enlargeprogimgnwnh=letnewimage=createnwnhinletxscale=floatnw/.floatimg.widthinletyscale=floatnh/.floatimg.heightinletww=truncate(ceilxscale)andwh=truncate(ceilyscale)inletweight=Array.initww(funx->Array.initwh(funy->letx0=x-ww/2andy0=y-wh/2inletx1=x0+ww-1andy1=y0+wh-1inArray.init3(funxx->Array.init3(funyy->letmx0=(xx-1)*wwandmy0=(yy-1)*whinletmx1=mx0+ww-1andmy1=my0+wh-1inletcx0=ifx0<mx0thenmx0elsex0inletcy0=ify0<my0thenmy0elsey0inletcx1=ifx1>mx1thenmx1elsex1inletcy1=ify1>my1thenmy1elsey1inletdx=cx1-cx0+1anddy=cy1-cy0+1inletdx=ifdx<0then0elsedxanddy=ifdy<0then0elsedyindx*dy))))inletwsum=Array.initww(funx->Array.initwh(funy->letsum=ref0inArray.iter(Array.iter(funw->sum:=!sum+w))weight.(x).(y);if!sum=0thenfailwith"resize_enlarge wsum";!sum))inletxs=Array.initimg.width(funx->letsx=truncate(floatx*.xscale)inletex=truncate(float(x+1)*.xscale)-1inletdx=ex-sx+1inifdx>wwthenfailwith"resize_enlarge";(sx,ex,dx))inletys=Array.initimg.height(funy->letsy=truncate(floaty*.yscale)inletey=truncate(float(y+1)*.yscale)-1inletdy=ey-sy+1inifdy>whthenfailwith"resize_enlarge";(sy,ey,dy))inletquerycxy=ifx<0||y<0||x>=img.width||y>=img.heightthencelseunsafe_getimgxyinfory=0toimg.height-1doletsy,_ey,dy=ys.(y)inforx=0toimg.width-1doletsx,_ex,dx=xs.(x)inletcolors=letc=unsafe_getimgxyinArray.init3(fundx->Array.init3(fundy->queryc(x+dx-1)(y+dy-1)))inforxx=0todx-1doforyy=0tody-1doletsr=ref0andsg=ref0andsb=ref0andsa=ref0inletweight=weight.(xx).(yy)inletwsum=wsum.(xx).(yy)inforxxx=0to2doforyyy=0to2doletc=colors.(xxx).(yyy)insr:=!sr+c.color.r*weight.(xxx).(yyy);sg:=!sg+c.color.g*weight.(xxx).(yyy);sb:=!sb+c.color.b*weight.(xxx).(yyy);sa:=!sa+c.alpha*weight.(xxx).(yyy);donedone;unsafe_setnewimage(sx+xx)(sy+yy){color={r=!sr/wsum;g=!sg/wsum;b=!sb/wsum};alpha=!sa/wsum}donedonedone;matchprogwith|Somep->p(float(y+1)/.floatimg.height)|None->()done;newimageletresizeprogimgnwnh=letxscale=floatnw/.floatimg.widthinletyscale=floatnh/.floatimg.heightinifxscale>=1.0&&yscale>=1.0thenresize_enlargeprogimgnwnhelseifxscale<=1.0&&yscale<=1.0thenresize_reduceprogimgnwnhelseresize_reduceprogimgnwnh(*
(* image resize with smoothing *)
let resize prog img nw nh =
let newimage = create nw nh in
let xscale = float nw /. float img.width in
let yscale = float nh /. float img.height in
for y = 0 to nh - 1 do
for x = 0 to nw - 1 do
let sx = truncate (float x /. xscale)
and sy = truncate (float y /. yscale)
in
let ex = truncate ((float x +. 0.99) /. xscale)
and ey = truncate ((float y +. 0.99) /. yscale)
in
(*
let ex = if ex >= img.width then img.width - 1 else ex
and ey = if ey >= img.height then img.height - 1 else ey
in
*)
let size = (ex - sx + 1) * (ey - sy + 1) in
let sr = ref 0
and sg = ref 0
and sb = ref 0
and sa = ref 0
in
for xx = sx to ex do
for yy = sy to ey do
let c = unsafe_get img xx yy in
sr := !sr + c.color.r;
sg := !sg + c.color.g;
sb := !sb + c.color.b;
sa := !sa + c.alpha
done
done;
unsafe_set newimage x y { color = { r = (!sr/size);
g = (!sg/size);
b = (!sb/size) };
alpha = (!sa/size) }
done;
match prog with
Some p -> p (float (y + 1) /. float img.height)
| None -> ()
done;
newimage
*)