12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788(*
* This file is part of Imagelib.
*
* Imagelib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Imabelib 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Imabelib. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2014 Rodolphe Lepigre.
*)openStdlibopenImageUtilopenImagemoduleReadJPG:ReadImage=structletextensions=["jpg";"jpeg";"jpe";"jif";"jfif";"jfi"]letread_markerich=letff=chunk_byteichinifff<>0xffthenraise(Corrupted_image"Expected marker...");letrecread_first_not_ffich=letc=chunk_byteichinifc=0xffthenread_first_not_ffichelsecinletc=read_first_not_ffichinifc=0thenraise(Corrupted_image"0xFF00 is not a valid marker...");cletread_header_dataich=(* Read magic number (i.e. marker 0xffd8) *)letmagic=read_markerichinifmagic<>0xd8thenraise(Corrupted_image"First marker should be SOI...");(* Read other header sections *)letrecread_header_sectionsichacc=letmrk=read_markerichinletsz=get_bytesich2inletsize=((int_of_charsz.[0])lsl8)lor(int_of_charsz.[1])inletdata=get_bytesich(size-2)inifmrk=0xdathen(* SOS reached *)List.rev((mrk,data)::acc)elseread_header_sectionsich((mrk,data)::acc)inread_header_sectionsich[](* Read the size of a XCF image.
* Arguments:
* - fn : filename.
* Returns a couble (width, height).
* Note: the image is not checked for inconsistency, only the signature and
* header are checked.
*)letsizeich=letheaders=read_header_dataichinclose_chunk_readerich;letrecfind_SOFls=matchlswith|[]->raise(Corrupted_image"No SOFn marker...");|(m,d)::bs->ifm>=0xc0&&m<=0xcf&&m<>0xc4&&m<>0xccthen(m-0xc0,d)elsefind_SOFbsinlet(_,sof)=find_SOFheadersin(*
Printf.fprintf stderr "SOF%i: " n;
show_string_hex sof;
*)letheight=((int_of_charsof.[1])lsl8)lor(int_of_charsof.[2])inletwidth=((int_of_charsof.[3])lsl8)lor(int_of_charsof.[4])inwidth,heightletparsefile_=raise(Not_yet_implemented"ImageJPG.openfile")(* TODO *)endincludeReadJPG