Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_ppm / mptr_ppm.cpp
1 /*! ========================================================================
2 ** Sinfg
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 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #ifdef USING_PCH
24 #       include "pch.h"
25 #else
26 #ifdef HAVE_CONFIG_H
27 #       include <config.h>
28 #endif
29
30 #include "mptr_ppm.h"
31 #include <sinfg/importer.h>
32 #include <sinfg/time.h>
33 #include <sinfg/surface.h>
34 #include <sinfg/general.h>
35 #include <sinfg/smartfile.h>
36
37 #include <cstdio>
38 #include <algorithm>
39 #include <functional>
40
41 #endif
42
43 /* === M A C R O S ========================================================= */
44
45 using namespace sinfg;
46 using namespace std;
47 using namespace etl;
48
49 /* === G L O B A L S ======================================================= */
50
51 SINFG_IMPORTER_INIT(ppm_mptr);
52 SINFG_IMPORTER_SET_NAME(ppm_mptr,"ppm_mptr");
53 SINFG_IMPORTER_SET_EXT(ppm_mptr,"ppm");
54 SINFG_IMPORTER_SET_VERSION(ppm_mptr,"0.1");
55 SINFG_IMPORTER_SET_CVS_ID(ppm_mptr,"$Id: mptr_ppm.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $");
56
57 /* === M E T H O D S ======================================================= */
58
59 ppm_mptr::ppm_mptr(const char *file)
60 {
61         filename=file;
62 }
63
64 ppm_mptr::~ppm_mptr()
65 {
66 }
67
68 bool
69 ppm_mptr::get_frame(sinfg::Surface &surface,Time, sinfg::ProgressCallback *cb)
70 {
71         SmartFILE file(fopen(filename.c_str(),"rb"));
72         if(!file)
73         {
74                 if(cb)cb->error("pp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str())); 
75                 return false;
76         }
77         int w,h;
78         float divisor;
79         
80         if(fgetc(file.get())!='P' || fgetc(file.get())!='6')
81         {
82                 if(cb)cb->error("pp_mptr::GetFrame(): "+strprintf(_("%s was not in PPM format"),filename.c_str())); 
83                 return false;
84         }
85         
86         fgetc(file.get());
87         fscanf(file.get(),"%d %d\n",&w,&h);
88         fscanf(file.get(),"%f",&divisor);
89         fgetc(file.get());
90         
91         int x;
92         int y;
93         surface.set_wh(w,h);
94         for(y=0;y<surface.get_h();y++)
95                 for(x=0;x<surface.get_w();x++)
96                 {
97 /*
98                         surface[y][x]=Color(
99                                 (float)(unsigned char)fgetc(file)/divisor,
100                                 (float)(unsigned char)fgetc(file)/divisor,
101                                 (float)(unsigned char)fgetc(file)/divisor,
102                                 1.0
103                         );
104 */
105                         float r=gamma().r_U8_to_F32((unsigned char)fgetc(file.get()));
106                         float g=gamma().g_U8_to_F32((unsigned char)fgetc(file.get()));
107                         float b=gamma().b_U8_to_F32((unsigned char)fgetc(file.get()));
108                         surface[y][x]=Color(
109                                 r,
110                                 g,
111                                 b,
112                                 1.0
113                         );
114                 }
115         return true;
116 }