package camlimages
Image processing library
Install
Dune Dependency
Authors
Maintainers
Sources
camlimages-5.0.4.tar.bz2
sha256=1c9a68bdc3d348c9f859d490dadf384926213e47a584159832f7fc4a20242865
md5=1ddba74d210b86a899b5d6565f45c2dc
doc/src/camlimages.core/images.ml.html
Source file images.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
(***********************************************************************) (* *) (* 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: images.ml,v 1.3 2008/06/16 22:35:42 furuse Exp $ *) (* The image data structure definition. *) (**************************************************************** Exceptions *) exception Out_of_image (* exception for illegal point access *) exception Wrong_image_type (* exception for illegal internal image type *) exception Wrong_file_type (* exception for unsupported image FILE format *) (* Update region.error *) let () = Region.error := (fun () -> raise Out_of_image) (************************************************************* Generic image *) type t = | Index8 of Index8.t | Rgb24 of Rgb24.t | Index16 of Index16.t | Rgba32 of Rgba32.t | Cmyk32 of Cmyk32.t (* Generic image type *) type sequence = { seq_width: int; seq_height: int; seq_frames: frame list; seq_loops: int } and frame = { frame_left: int; frame_top: int; frame_image: t; frame_delay: int (* mili secs *) } (******************************************************************** Colors *) (* Colors: the copies of color.mli *) type rgb = Color.rgb = { mutable r: int; mutable g: int; mutable b: int } type rgba = Color.rgba = { color: rgb; mutable alpha: int } type cmyk = Color.cmyk = { mutable c : int; mutable m : int; mutable y : int; mutable k : int } type 'a map = 'a Color.map = { mutable max: int; (* maximum number allowed in the color map (-1 = unlimited) *) mutable map: 'a array } (********************************************************* Image file format *) (* Image formats *) type format = | Gif | Bmp | Jpeg | Tiff | Png | Xpm | Ppm | Ps (************************************************ Image file name extensions *) let extension = function | Gif -> "gif" | Bmp -> "bmp" | Jpeg -> "jpg" | Tiff -> "tif" | Png -> "png" | Xpm -> "xpm" | Ppm -> "ppm" | Ps -> "eps" let get_extension s = try let dotpos = String.rindex s '.' in String.sub s 0 dotpos, String.sub s (dotpos + 1) (String.length s - dotpos - 1) with | _ -> s, "" let guess_extension s = let s = String.lowercase s in match s with | "gif" -> Gif | "bmp" -> Bmp | "jpeg" | "jpg" -> Jpeg | "tiff" | "tif" -> Tiff | "png" -> Png | "xpm" -> Xpm | "ppm" | "pgm" | "pbm" -> Ppm | "eps" | "ps" | "epsf" | "epsi" -> Ps | _ -> raise Not_found let guess_format file = guess_extension (snd (get_extension file)) (******************************************** Image file header informations *) type colormodel = Info.colormodel = | Gray | RGB | Index | GrayA | RGBA | YCbCr | CMYK (* Infos attached to bitmaps *) type info = Info.info = | Info_DPI of float (* dot per inch *) | Info_BigEndian | Info_LittleEndian (* endianness of image file *) | Info_ColorModel of colormodel (* color model of image file *) | Info_Depth of int (* Image bit depth *) | Info_Corrupted (* For corrupted PNG files *) (* Info query *) let rec dpi = function | [] -> None | Info_DPI dpi :: _ -> Some dpi | _ :: xs -> dpi xs (* Image file header *) type header = { header_width : int; header_height : int; header_infos : info list } (**************************************************** Image file I/O options *) (* Load options *) type load_option = | Load_Progress of (float -> unit) (* For progress meters *) | Load_Resolution of float * float (* Pixel/Inch for rasterization of PS *) | Load_only_the_first_frame (* Save options *) type save_option = | Save_Quality of int (* Save quality for Jpeg compression *) | Save_Progress of (float -> unit) (* For progress meters *) | Save_Interlace (* Interlaced Gif *) (* Option queries *) let rec load_progress = function | [] -> None | Load_Progress p :: _ -> Some p | _ :: xs -> load_progress xs let rec load_resolution = function | [] -> None | Load_Resolution (px, py) :: _ -> Some (px, py) | _ :: xs -> load_resolution xs let rec save_progress = function | [] -> None | Save_Progress p :: _ -> Some p | _ :: xs -> save_progress xs let rec save_interlace = function | [] -> false | Save_Interlace :: _ -> true | _ :: xs -> save_interlace xs let rec save_quality = function | [] -> None | Save_Quality q :: _ -> Some q | _ :: xs -> save_quality xs (******************************** The type for methods of image file formats *) type format_methods = { check_header: (string -> header); load: (string -> load_option list -> t) option; save: (string -> save_option list -> t -> unit) option; load_sequence: (string -> load_option list -> sequence) option; save_sequence: (string -> save_option list -> sequence -> unit) option; } let methods_list = ref [] let file_format filename = let result = ref None in try List.iter (fun (format, methods) -> try result := Some (format, methods.check_header filename); raise Exit with | Wrong_file_type -> ()) !methods_list; raise Wrong_file_type with | Exit -> match !result with | Some r -> r | None -> assert false (************************************************ Generic image manupilation *) let add_methods format methods = methods_list := (format, methods) :: !methods_list let load filename load_options = let result = ref None in try List.iter (fun (_format, methods) -> try let _ = methods.check_header filename in match methods.load with Some load -> result := Some (load filename load_options); raise Exit | None -> raise Wrong_file_type with | Wrong_file_type -> ()) !methods_list; raise Wrong_file_type with | Exit -> match !result with | Some r -> r | None -> assert false let save filename formatopt save_options t = try let format = match formatopt with | Some format -> format | None -> guess_format filename in let methods = List.assoc format !methods_list in match methods.save with Some save -> save filename save_options t | None -> raise Wrong_file_type with | Not_found -> raise Wrong_file_type let size img = match img with | Index8 bmp -> bmp.Index8.width, bmp.Index8.height | Index16 bmp -> bmp.Index16.width, bmp.Index16.height | Rgb24 bmp -> bmp.Rgb24.width, bmp.Rgb24.height | Rgba32 bmp -> bmp.Rgba32.width, bmp.Rgba32.height | Cmyk32 bmp -> bmp.Cmyk32.width, bmp.Cmyk32.height let width img = match img with | Index8 bmp -> bmp.Index8.width | Index16 bmp -> bmp.Index16.width | Rgb24 bmp -> bmp.Rgb24.width | Rgba32 bmp -> bmp.Rgba32.width | Cmyk32 bmp -> bmp.Cmyk32.width let height img = match img with | Index8 bmp -> bmp.Index8.height | Index16 bmp -> bmp.Index16.height | Rgb24 bmp -> bmp.Rgb24.height | Rgba32 bmp -> bmp.Rgba32.height | Cmyk32 bmp -> bmp.Cmyk32.height let destroy img = match img with | Index8 bmp -> Index8.destroy bmp | Rgb24 bmp -> Rgb24.destroy bmp | Index16 bmp -> Index16.destroy bmp | Rgba32 bmp -> Rgba32.destroy bmp | Cmyk32 bmp -> Cmyk32.destroy bmp let sub img x y w h = let f = match img with | Index8 img -> (fun x y w h -> Index8 (Index8.sub img x y w h)) | Rgb24 img -> (fun x y w h -> Rgb24 (Rgb24.sub img x y w h)) | Index16 img -> (fun x y w h -> Index16 (Index16.sub img x y w h)) | Rgba32 img -> (fun x y w h -> Rgba32 (Rgba32.sub img x y w h)) | Cmyk32 img -> (fun x y w h -> Cmyk32 (Cmyk32.sub img x y w h)) in f x y w h let copy img = sub img 0 0 (width img) (height img) let blit src sx sy dst dx dy = let f = match src, dst with | Index8 src, Index8 dst -> (fun sx sy -> Index8.blit src sx sy dst) | Rgb24 src, Rgb24 dst -> (fun sx sy -> Rgb24.blit src sx sy dst) | Index16 src, Index16 dst -> (fun sx sy -> Index16.blit src sx sy dst) | Rgba32 src, Rgba32 dst -> (fun sx sy -> Rgba32.blit src sx sy dst) | Cmyk32 src, Cmyk32 dst -> (fun sx sy -> Cmyk32.blit src sx sy dst) | _ -> raise (Invalid_argument "Images.blit") in f sx sy dx dy (* image sequences *) let make_sequence t = { seq_width = width t; seq_height = height t; seq_frames = [ { frame_left= 0; frame_top= 0; frame_image= t; frame_delay= 0 } ]; seq_loops = 0 } let unoptimize_sequence seq = (* sequence must be non-empty *) let coe = function | Index8 t -> Rgba32 (Index8.to_rgba32 t) | Index16 t -> Rgba32 (Index16.to_rgba32 t) | t -> t in { seq with seq_frames = begin let _, result = let head_frame = List.hd seq.seq_frames in List.fold_left (fun (previmage, result) frame -> let newimage = copy previmage in let src = coe frame.frame_image in begin match src, newimage with | Rgb24 _, _ | Cmyk32 _, _ -> (* non transparent *) blit src 0 0 newimage frame.frame_left frame.frame_top (width src) (height src) | Rgba32 src32, Rgba32 dst32 -> (* transparent *) Rgba32.map Color.Rgba.merge src32 0 0 dst32 frame.frame_left frame.frame_top (width src) (height src) | _ -> assert false end; (newimage, { frame_left = 0; frame_top = 0; frame_image = newimage; frame_delay = frame.frame_delay } :: result)) (coe head_frame.frame_image, [head_frame]) (List.tl seq.seq_frames) in List.rev result end } let load_sequence filename load_options = let result = ref None in try List.iter (fun (_format, methods) -> try let _ = methods.check_header filename in match methods.load_sequence, methods.load with | Some load, _ -> result := Some (load filename load_options); raise Exit | None, Some load -> result := Some (make_sequence (load filename load_options)); | None, None -> raise Wrong_file_type with | Wrong_file_type -> ()) !methods_list; raise Wrong_file_type with | Exit -> match !result with | Some r -> r | None -> assert false let save_sequence filename formatopt save_options seq = try let format = match formatopt with | Some format -> format | None -> guess_format filename in let methods = List.assoc format !methods_list in match methods.save_sequence with Some save -> save filename save_options seq | None -> raise Wrong_file_type with | Not_found -> raise Wrong_file_type let blocks img = match img with | Index8 img -> Index8.blocks img | Rgb24 img -> Rgb24.blocks img | Index16 img -> Index16.blocks img | Rgba32 img -> Rgba32.blocks img | Cmyk32 img -> Cmyk32.blocks img
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>