Enable $Id$ expansion.
[synfig.git] / synfig-core / trunk / src / modules / mod_ppm / mptr_ppm.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mptr_ppm.cpp
3 **      \brief ppm Target Module
4 **
5 **      \legal
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 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "mptr_ppm.h"
33 #include <synfig/importer.h>
34 #include <synfig/time.h>
35 #include <synfig/surface.h>
36 #include <synfig/general.h>
37 #include <synfig/smartfile.h>
38
39 #include <cstdio>
40 #include <algorithm>
41 #include <functional>
42
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_IMPORTER_INIT(ppm_mptr);
54 SYNFIG_IMPORTER_SET_NAME(ppm_mptr,"ppm");
55 SYNFIG_IMPORTER_SET_EXT(ppm_mptr,"ppm");
56 SYNFIG_IMPORTER_SET_VERSION(ppm_mptr,"0.1");
57 SYNFIG_IMPORTER_SET_CVS_ID(ppm_mptr,"$Id$");
58
59 /* === M E T H O D S ======================================================= */
60
61 ppm_mptr::ppm_mptr(const char *file)
62 {
63         filename=file;
64 }
65
66 ppm_mptr::~ppm_mptr()
67 {
68 }
69
70 bool
71 ppm_mptr::get_frame(synfig::Surface &surface,Time, synfig::ProgressCallback *cb)
72 {
73         SmartFILE file(fopen(filename.c_str(),"rb"));
74         if(!file)
75         {
76                 if(cb)cb->error("pp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
77                 return false;
78         }
79         int w,h;
80         float divisor;
81
82         if(fgetc(file.get())!='P' || fgetc(file.get())!='6')
83         {
84                 if(cb)cb->error("pp_mptr::GetFrame(): "+strprintf(_("%s was not in PPM format"),filename.c_str()));
85                 return false;
86         }
87
88         fgetc(file.get());
89         fscanf(file.get(),"%d %d\n",&w,&h);
90         fscanf(file.get(),"%f",&divisor);
91         fgetc(file.get());
92
93         int x;
94         int y;
95         surface.set_wh(w,h);
96         for(y=0;y<surface.get_h();y++)
97                 for(x=0;x<surface.get_w();x++)
98                 {
99 /*
100                         surface[y][x]=Color(
101                                 (float)(unsigned char)fgetc(file)/divisor,
102                                 (float)(unsigned char)fgetc(file)/divisor,
103                                 (float)(unsigned char)fgetc(file)/divisor,
104                                 1.0
105                         );
106 */
107                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file.get()));
108                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file.get()));
109                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file.get()));
110                         surface[y][x]=Color(
111                                 r,
112                                 g,
113                                 b,
114                                 1.0
115                         );
116                 }
117         return true;
118 }