summaryrefslogtreecommitdiffstats
path: root/debian/fireflies/fireflies-2.08/libgfx/src/raster.cxx
blob: e5a15228dc5aac9d73b4c5702bf2331debce0e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/************************************************************************

  Raster image support.

  $Id: raster.cxx 427 2004-09-27 04:45:31Z garland $

 ************************************************************************/

#include <gfx/gfx.h>
#include <gfx/raster.h>

#include <string>
#include <cctype>
#include <cstring>

namespace gfx
{

ByteRaster::ByteRaster(const ByteRaster &img)
    : Raster<unsigned char>(img.width(), img.height(), img.channels())
{
    memcpy(head(), img.head(), img.length()*sizeof(unsigned char));
}

ByteRaster::ByteRaster(const FloatRaster &img)
    : Raster<unsigned char>(img.width(), img.height(), img.channels())
{
    for(int i=0; i<length(); i++ )
        (*this)[i] = (unsigned char) (255.0f * img[i]);
}

FloatRaster::FloatRaster(const ByteRaster &img)
    : Raster<float>(img.width(), img.height(), img.channels())
{
    for(int i=0; i<length(); i++)
	(*this)[i] = (float)img[i] / 255.0f;
}

FloatRaster::FloatRaster(const FloatRaster &img)
    : Raster<float>(img.width(), img.height(), img.channels())
{
    memcpy(head(), img.head(), img.length()*sizeof(float));
}

////////////////////////////////////////////////////////////////////////
//
// Table of supported formats
//



static char *img_names[] = {"PPM", "PNG", "TIFF", "JPEG"};
static char *img_ext[] = {"ppm", "png", "tif", "jpg"};

const char *image_type_name(int type)
	{ return type>=IMG_LIMIT ? NULL : img_names[type]; }

const char *image_type_ext(int type)
	{ return type>=IMG_LIMIT ? NULL : img_ext[type]; }


int infer_image_type(const char *filename)
{
    const char *ext = strrchr(filename, '.');
    if( !ext ) return -1;

    // Make sure extension is lower case
    std::string lo(ext+1);
    for(int i=0; i<lo.length(); i++)  lo[i] = tolower(lo[i]);

    // Search for extension in the table
    for(int typ=0; typ<IMG_LIMIT; typ++)
	if(lo == img_ext[typ])  return typ;

    // Test for alternatives
    if(lo=="tiff")  return IMG_TIFF;

    // Unknown type
    return -1;
}


bool write_image(const char *filename, const ByteRaster& img, int type)
{
    if( type<0 )
	type = infer_image_type(filename);

    switch( type )
    {
    case IMG_PNM:  return write_pnm_image(filename, img);
    case IMG_PNG:  return write_png_image(filename, img);
    case IMG_TIFF: return write_tiff_image(filename, img);
    case IMG_JPEG: return write_jpeg_image(filename, img);
    default:       return false;
    }
}

ByteRaster *read_image(const char *filename, int type)
{
    if( type<0 )
	type = infer_image_type(filename);

    switch( type )
    {
    case IMG_PNM:  return read_pnm_image(filename);
    case IMG_PNG:  return read_png_image(filename);
    case IMG_TIFF: return read_tiff_image(filename);
    case IMG_JPEG: return read_jpeg_image(filename);
    default:       return NULL;
    }
}

} // namespace gfx