1 2(********************************************************************) 3(* *) 4(* imagefile.s7i Support for various image file formats. *) 5(* Copyright (C) 2021 Thomas Mertes *) 6(* *) 7(* This file is part of the Seed7 Runtime Library. *) 8(* *) 9(* The Seed7 Runtime Library is free software; you can *) 10(* redistribute it and/or modify it under the terms of the GNU *) 11(* Lesser General Public License as published by the Free Software *) 12(* Foundation; either version 2.1 of the License, or (at your *) 13(* option) any later version. *) 14(* *) 15(* The Seed7 Runtime Library is distributed in the hope that it *) 16(* will be useful, but WITHOUT ANY WARRANTY; without even the *) 17(* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *) 18(* PURPOSE. See the GNU Lesser General Public License for more *) 19(* details. *) 20(* *) 21(* You should have received a copy of the GNU Lesser General *) 22(* Public License along with this program; if not, write to the *) 23(* Free Software Foundation, Inc., 51 Franklin Street, *) 24(* Fifth Floor, Boston, MA 02110-1301, USA. *) 25(* *) 26(********************************************************************) 27 28 29 include "png.s7i"; 30 include "gif.s7i"; 31 include "jpeg.s7i"; 32 include "bmp.s7i"; 33 include "ico.s7i"; 34 include "ppm.s7i"; 35 36 37(** 38 * Reads an image file into a pixmap. 39 * The file is checked for magic numbers and the corresponding read 40 * function is used, to read the actual image. 41 * @param imageFile File that contains an image. 42 * @return A pixmap with the image, or 43 * PRIMITIVE_WINDOW.value if the file is not in a valid 44 * image file format. 45 *) 46const func PRIMITIVE_WINDOW: readImage (inout file: imageFile) is func 47 result 48 var PRIMITIVE_WINDOW: pixmap is PRIMITIVE_WINDOW.value; 49 local 50 var string: magic is ""; 51 begin 52 block 53 magic := gets(imageFile, length(PPM_MAGIC)); 54 if magic = BMP_MAGIC then 55 seek(imageFile, 1); 56 pixmap := readBmp(imageFile); 57 elsif magic = PPM_MAGIC then 58 seek(imageFile, 1); 59 pixmap := readPpm(imageFile); 60 else 61 magic &:= gets(imageFile, length(JPEG_MAGIC) - length(PPM_MAGIC)); 62 if magic = JPEG_MAGIC then 63 seek(imageFile, 1); 64 pixmap := readJpeg(imageFile); 65 else 66 magic &:= gets(imageFile, length(ICO_MAGIC) - length(JPEG_MAGIC)); 67 if magic = ICO_MAGIC then 68 seek(imageFile, 1); 69 pixmap := readIco(imageFile); 70 else 71 magic &:= gets(imageFile, length(GIF_MAGIC_87) - length(ICO_MAGIC)); 72 if magic = GIF_MAGIC_87 or magic = GIF_MAGIC_89 then 73 seek(imageFile, 1); 74 pixmap := readGif(imageFile); 75 else 76 magic &:= gets(imageFile, length(PNG_MAGIC) - length(GIF_MAGIC_87)); 77 if magic = PNG_MAGIC then 78 seek(imageFile, 1); 79 pixmap := readPng(imageFile); 80 end if; 81 end if; 82 end if; 83 end if; 84 end if; 85 exception 86 otherwise: noop; 87 end block; 88 end func; 89 90 91(** 92 * Reads an image file with the given ''imageFileName'' into a pixmap. 93 * The file is checked for magic numbers and the corresponding read 94 * function is used, to read the actual image. The file extension of 95 * ''imageFileName'' is not used to decide about the image file type. 96 * @param imageFileName Name of the image file. 97 * @return A pixmap with the image, or 98 * PRIMITIVE_WINDOW.value if the file cannot be opened or 99 * is not in a valid image file format. 100 *) 101const func PRIMITIVE_WINDOW: readImage (in string: imageFileName) is func 102 result 103 var PRIMITIVE_WINDOW: pixmap is PRIMITIVE_WINDOW.value; 104 local 105 var file: imageFile is STD_NULL; 106 begin 107 imageFile := open(imageFileName, "r"); 108 if imageFile <> STD_NULL then 109 pixmap := readImage(imageFile); 110 close(imageFile); 111 end if; 112 end func; 113