Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_ppm / mptr_ppm.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** ppm Target Module
4 ** $Id: mptr_ppm.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
5 **
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "mptr_ppm.h"
32 #include <synfig/importer.h>
33 #include <synfig/time.h>
34 #include <synfig/surface.h>
35 #include <synfig/general.h>
36 #include <synfig/smartfile.h>
37
38 #include <cstdio>
39 #include <algorithm>
40 #include <functional>
41
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 using namespace synfig;
47 using namespace std;
48 using namespace etl;
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_IMPORTER_INIT(ppm_mptr);
53 SYNFIG_IMPORTER_SET_NAME(ppm_mptr,"ppm_mptr");
54 SYNFIG_IMPORTER_SET_EXT(ppm_mptr,"ppm");
55 SYNFIG_IMPORTER_SET_VERSION(ppm_mptr,"0.1");
56 SYNFIG_IMPORTER_SET_CVS_ID(ppm_mptr,"$Id: mptr_ppm.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
57
58 /* === M E T H O D S ======================================================= */
59
60 ppm_mptr::ppm_mptr(const char *file)
61 {
62         filename=file;
63 }
64
65 ppm_mptr::~ppm_mptr()
66 {
67 }
68
69 bool
70 ppm_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback *cb)
71 {
72         SmartFILE file(fopen(filename.c_str(),"rb"));
73         if(!file)
74         {
75                 if(cb)cb->error("pp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str())); 
76                 return false;
77         }
78         int w,h;
79         float divisor;
80         
81         if(fgetc(file.get())!='P' || fgetc(file.get())!='6')
82         {
83                 if(cb)cb->error("pp_mptr::GetFrame(): "+strprintf(_("%s was not in PPM format"),filename.c_str())); 
84                 return false;
85         }
86         
87         fgetc(file.get());
88         fscanf(file.get(),"%d %d\n",&w,&h);
89         fscanf(file.get(),"%f",&divisor);
90         fgetc(file.get());
91         
92         int x;
93         int y;
94         surface.set_wh(w,h);
95         for(y=0;y<surface.get_h();y++)
96                 for(x=0;x<surface.get_w();x++)
97                 {
98 /*
99                         surface[y][x]=Color(
100                                 (float)(unsigned char)fgetc(file)/divisor,
101                                 (float)(unsigned char)fgetc(file)/divisor,
102                                 (float)(unsigned char)fgetc(file)/divisor,
103                                 1.0
104                         );
105 */
106                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file.get()));
107                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file.get()));
108                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file.get()));
109                         surface[y][x]=Color(
110                                 r,
111                                 g,
112                                 b,
113                                 1.0
114                         );
115                 }
116         return true;
117 }