/**************************************************************************** Copyright (C) 2010 Laszlo Simon This file is part of the HidraVFX project. HidraVFX 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. HidraVFX 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 HidraVFX. If not, see . ****************************************************************************/ #include #include #include #include #include "layer.h" #include "pnm.h" int pfm_load(char *inFile, tLayerF *image) { char buffer[1024]; FILE *in = NULL; int x, y, w, h; float factor; char c; if (inFile != NULL) { in = (0 == strcmp(inFile, "--")) ? stdin : fopen(inFile, "rb"); } if (in == NULL) return(1); fgets(buffer, 1024, in); sscanf(buffer, "P%c", &c); while ((fgets(buffer, 1024, in) != NULL) && buffer[0] == '#'); sscanf(buffer, "%d %d", &w, &h); while ((fgets(buffer, 1024, in) != NULL) && buffer[0] == '#'); sscanf(buffer, "%f", &factor); printf("P%c, %d x %d, %f\n", c, w, h, factor); *image = layerF(w, h); assert(sizeof(float) == 4); for (y = image->t; y < image->t + image->h; y++) { for (x = image->l; x < image->l + image->w; x++) { float ch0, ch1, ch2; fread(&ch0, sizeof(float), 1, in); fread(&ch1, sizeof(float), 1, in); fread(&ch2, sizeof(float), 1, in); ((image->ch[0])[y])[x] = ch0; ((image->ch[1])[y])[x] = ch1; ((image->ch[2])[y])[x] = ch2; ((image->ch[3])[y])[x] = 1.0; } } fclose(in); return(0); } int pfm_save(char *outFile, tLayerF *image) { FILE *out; int x, y; int res = 0; if (outFile != NULL) { out = fopen(outFile, "wb"); if (out == NULL) return(1); } else { out = stdout; } fprintf(out, "PF%c", 10); fprintf(out, "%d %d%c", image->w, image->h, 10); fprintf(out, "-1.0%c", 10); for (y = image->t; y < image->t + image->h; y++) { for (x = image->l; x < image->l + image->w; x++) { fwrite(&((image->ch[0][y])[x]), sizeof(float), 1, out); fwrite(&((image->ch[1][y])[x]), sizeof(float), 1, out); fwrite(&((image->ch[2][y])[x]), sizeof(float), 1, out); } } fclose(out); return(res); }