123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166openCtypesopenForeignopenTsdlmoduleImage=structtype'aresult='aSdl.resultleterror()=Error(`Msg(Sdl.get_error()))letbool=view~read:((<>)0)~write:(funb->comparebfalse)intmoduleInit=structtypet=Unsigned.uint32leti=Unsigned.UInt32.of_intlet(+)=Unsigned.UInt32.logorlettestfm=Unsigned.UInt32.(compare(logandfm)zero<>0)leteqff'=Unsigned.UInt32.(compareff'=0)letempty=i0letjpg=i1letpng=i2lettif=i4letwebp=i8end(* This "hack" seems to be necessary for linux if you want to use
#require "tsdl-image"
in the toplevel, see
https://github.com/ocamllabs/ocaml-ctypes/issues/70 *)letdllib:Dl.libraryoption=if!Sys.interactivethen(letfilename=matchBuild_config.systemwith|"macosx"->"libSDL2_image-2.0.0.dylib"|_->"libSDL2_image-2.0.so.0"inSomeDl.(dlopen~filename~flags:[RTLD_NOW]))elseNoneletforeign=foreign?from:dllibletinit=foreign"IMG_Init"(uint32_t@->returninguint32_t)letquit=foreign"IMG_Quit"(void@->returningvoid)letsurface=view~read:Sdl.unsafe_surface_of_ptr~write:Sdl.unsafe_ptr_of_surfacenativeintlettexture_result=letreadv=ifNativeint.(comparevzero)=0thenerror()elseOk(Sdl.unsafe_texture_of_ptrv)andwrite=function|Okv->Sdl.unsafe_ptr_of_texturev|Error_->raw_address_of_ptrnullinview~read~writenativeintletsurface_result=letreadv=ifNativeint.(comparevzero)=0thenerror()elseOk(Sdl.unsafe_surface_of_ptrv)andwrite=function|Okv->Sdl.unsafe_ptr_of_surfacev|Error_->raw_address_of_ptrnullinview~read~writenativeintletrw_ops=view~read:Sdl.unsafe_rw_ops_of_ptr~write:Sdl.unsafe_ptr_of_rw_opsnativeintletrenderer=view~read:Sdl.unsafe_renderer_of_ptr~write:Sdl.unsafe_ptr_of_renderernativeintletload=foreign"IMG_Load"(string@->returningsurface_result)letload_rw=foreign"IMG_Load_RW"(rw_ops@->bool@->returningsurface_result)typeformat=Ico|Cur|Bmp|Gif|Jpg|Lbm|Pcx|Png|Pnm|Tif|Xcf|Xpm|Xv|Webp|Tgaletstring_of_format=function|Ico->"ICO"|Cur->"CUR"|Bmp->"BMP"|Gif->"GIF"|Jpg->"JPG"|Lbm->"LBM"|Pcx->"PCX"|Png->"PNG"|Pnm->"PNM"|Tif->"TIF"|Xcf->"XCF"|Xpm->"XPM"|Xv->"XV"|Webp->"WEBP"|Tga->"TGA"letload_typed_rw=foreign"IMG_LoadTyped_RW"(rw_ops@->bool@->string@->returningsurface_result)letload_typed_rwrbf=load_typed_rwrb(string_of_formatf)letload_texture=foreign"IMG_LoadTexture"(renderer@->string@->returningtexture_result)letload_texture_rw=foreign"IMG_LoadTexture_RW"(renderer@->rw_ops@->bool@->returningtexture_result)letload_texture_typed_rw=foreign"IMG_LoadTextureTyped_RW"(renderer@->rw_ops@->bool@->string@->returningtexture_result)letload_texture_typed_rwrobf=load_texture_typed_rwrob(string_of_formatf)letis_ico=foreign"IMG_isICO"(rw_ops@->returningbool)letis_cur=foreign"IMG_isCUR"(rw_ops@->returningbool)letis_bmp=foreign"IMG_isBMP"(rw_ops@->returningbool)letis_gif=foreign"IMG_isGIF"(rw_ops@->returningbool)letis_jpg=foreign"IMG_isJPG"(rw_ops@->returningbool)letis_lbm=foreign"IMG_isLBM"(rw_ops@->returningbool)letis_pcx=foreign"IMG_isPCX"(rw_ops@->returningbool)letis_png=foreign"IMG_isPNG"(rw_ops@->returningbool)letis_pnm=foreign"IMG_isPNM"(rw_ops@->returningbool)letis_tif=foreign"IMG_isTIF"(rw_ops@->returningbool)letis_xcf=foreign"IMG_isXCF"(rw_ops@->returningbool)letis_xpm=foreign"IMG_isXPM"(rw_ops@->returningbool)letis_xv=foreign"IMG_isXV"(rw_ops@->returningbool)letis_webp=foreign"IMG_isWEBP"(rw_ops@->returningbool)letis_formatfmt=matchfmtwith|Ico->is_ico|Cur->is_cur|Bmp->is_bmp|Gif->is_gif|Jpg->is_jpg|Lbm->is_lbm|Pcx->is_pcx|Png->is_png|Pnm->is_pnm|Tif->is_tif|Xcf->is_xcf|Xpm->is_xpm|Xv->is_xv|Webp->is_webp|Tga->failwith"TGA cannot safely be detected"letload_ico_rw=foreign"IMG_LoadICO_RW"(rw_ops@->returningsurface_result)letload_cur_rw=foreign"IMG_LoadCUR_RW"(rw_ops@->returningsurface_result)letload_bmp_rw=foreign"IMG_LoadBMP_RW"(rw_ops@->returningsurface_result)letload_gif_rw=foreign"IMG_LoadGIF_RW"(rw_ops@->returningsurface_result)letload_jpg_rw=foreign"IMG_LoadJPG_RW"(rw_ops@->returningsurface_result)letload_lbm_rw=foreign"IMG_LoadLBM_RW"(rw_ops@->returningsurface_result)letload_pcx_rw=foreign"IMG_LoadPCX_RW"(rw_ops@->returningsurface_result)letload_png_rw=foreign"IMG_LoadPNG_RW"(rw_ops@->returningsurface_result)letload_pnm_rw=foreign"IMG_LoadPNM_RW"(rw_ops@->returningsurface_result)letload_tga_rw=foreign"IMG_LoadTGA_RW"(rw_ops@->returningsurface_result)letload_tif_rw=foreign"IMG_LoadTIF_RW"(rw_ops@->returningsurface_result)letload_xcf_rw=foreign"IMG_LoadXCF_RW"(rw_ops@->returningsurface_result)letload_xpm_rw=foreign"IMG_LoadXPM_RW"(rw_ops@->returningsurface_result)letload_xv_rw=foreign"IMG_LoadXV_RW"(rw_ops@->returningsurface_result)letload_webp_rw=foreign"IMG_LoadWEBP_RW"(rw_ops@->returningsurface_result)letload_format_rw=function|Ico->load_ico_rw|Cur->load_cur_rw|Bmp->load_bmp_rw|Gif->load_gif_rw|Jpg->load_jpg_rw|Lbm->load_lbm_rw|Pcx->load_pcx_rw|Png->load_png_rw|Pnm->load_pnm_rw|Tif->load_tif_rw|Xcf->load_xcf_rw|Xpm->load_xpm_rw|Xv->load_xv_rw|Webp->load_webp_rw|Tga->load_tga_rwletread_xpm_from_array=foreign"IMG_ReadXPMFromArray"(string@->returningsurface_result)letsave_png=foreign"IMG_SavePNG"(surface@->string@->returningint)letsave_png_rw=foreign"IMG_SavePNG_RW"(surface@->rw_ops@->bool@->returningint)end