1141cc406Sopenharmony_ci/* Load an ICC profile for embedding in an output file 2141cc406Sopenharmony_ci Copyright (C) 2017 Aaron Muir Hamilton <aaron@correspondwith.me> 3141cc406Sopenharmony_ci 4141cc406Sopenharmony_ci This program is free software; you can redistribute it and/or 5141cc406Sopenharmony_ci modify it under the terms of the GNU General Public License as 6141cc406Sopenharmony_ci published by the Free Software Foundation; either version 2 of the 7141cc406Sopenharmony_ci License, or (at your option) any later version. 8141cc406Sopenharmony_ci 9141cc406Sopenharmony_ci This program is distributed in the hope that it will be useful, but 10141cc406Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 11141cc406Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12141cc406Sopenharmony_ci General Public License for more details. 13141cc406Sopenharmony_ci 14141cc406Sopenharmony_ci You should have received a copy of the GNU General Public License 15141cc406Sopenharmony_ci along with this program. If not, see <https://www.gnu.org/licenses/>. 16141cc406Sopenharmony_ci*/ 17141cc406Sopenharmony_ci 18141cc406Sopenharmony_ci#include "../include/sane/config.h" 19141cc406Sopenharmony_ci 20141cc406Sopenharmony_ci#include <stdlib.h> 21141cc406Sopenharmony_ci#include <stdio.h> 22141cc406Sopenharmony_ci#include <sys/stat.h> 23141cc406Sopenharmony_ci 24141cc406Sopenharmony_civoid * 25141cc406Sopenharmony_cisanei_load_icc_profile (const char *path, size_t *size) 26141cc406Sopenharmony_ci{ 27141cc406Sopenharmony_ci FILE *fd = NULL; 28141cc406Sopenharmony_ci size_t stated_size = 0; 29141cc406Sopenharmony_ci void *profile = NULL; 30141cc406Sopenharmony_ci struct stat s; 31141cc406Sopenharmony_ci 32141cc406Sopenharmony_ci fd = fopen(path, "r"); 33141cc406Sopenharmony_ci 34141cc406Sopenharmony_ci if (!fd) 35141cc406Sopenharmony_ci { 36141cc406Sopenharmony_ci fprintf(stderr, "Could not open ICC profile %s\n", path); 37141cc406Sopenharmony_ci } 38141cc406Sopenharmony_ci else 39141cc406Sopenharmony_ci { 40141cc406Sopenharmony_ci fstat(fileno(fd), &s); 41141cc406Sopenharmony_ci stated_size = 16777216 * fgetc(fd) + 65536 * fgetc(fd) + 256 * fgetc(fd) + fgetc(fd); 42141cc406Sopenharmony_ci rewind(fd); 43141cc406Sopenharmony_ci 44141cc406Sopenharmony_ci if (stated_size > (size_t) s.st_size) 45141cc406Sopenharmony_ci { 46141cc406Sopenharmony_ci fprintf(stderr, "Ignoring ICC profile because file %s is shorter than the profile\n", path); 47141cc406Sopenharmony_ci } 48141cc406Sopenharmony_ci else 49141cc406Sopenharmony_ci { 50141cc406Sopenharmony_ci profile = malloc(stated_size); 51141cc406Sopenharmony_ci 52141cc406Sopenharmony_ci if (fread(profile, stated_size, 1, fd) != 1) 53141cc406Sopenharmony_ci { 54141cc406Sopenharmony_ci fprintf(stderr, "Error reading ICC profile %s\n", path); 55141cc406Sopenharmony_ci free(profile); 56141cc406Sopenharmony_ci } 57141cc406Sopenharmony_ci else 58141cc406Sopenharmony_ci { 59141cc406Sopenharmony_ci fclose(fd); 60141cc406Sopenharmony_ci *size = stated_size; 61141cc406Sopenharmony_ci return profile; 62141cc406Sopenharmony_ci } 63141cc406Sopenharmony_ci } 64141cc406Sopenharmony_ci fclose(fd); 65141cc406Sopenharmony_ci } 66141cc406Sopenharmony_ci return NULL; 67141cc406Sopenharmony_ci} 68