~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/imageio/format/exr.cc

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-04-14 23:42:12 UTC
  • Revision ID: james.westby@ubuntu.com-20110414234212-kuffcz5wiu18v6ra
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of darktable,
 
3
    copyright (c) 2010 Henrik Andersson.
 
4
 
 
5
    darktable is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation, either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    darktable is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with darktable.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include <stdlib.h>
 
20
#include <stdio.h>
 
21
#include <memory.h>
 
22
#include <OpenEXR/ImfFrameBuffer.h>
 
23
#include <OpenEXR/ImfTiledOutputFile.h>
 
24
#include <OpenEXR/ImfChannelList.h>
 
25
#include <OpenEXR/ImfStandardAttributes.h>
 
26
#include "common/darktable.h"
 
27
#include "common/exif.h"
 
28
#include "common/colorspaces.h"
 
29
#include "common/imageio_module.h"
 
30
 
 
31
#ifdef HAVE_CONFIG_H
 
32
#include "config.h"
 
33
#endif
 
34
 
 
35
/** Special BLOB attribute implementation.*/
 
36
namespace Imf
 
37
{
 
38
typedef struct Blob
 
39
{
 
40
  uint32_t size;
 
41
  uint8_t *data;
 
42
} Blob;
 
43
typedef Imf::TypedAttribute<Imf::Blob> BlobAttribute;
 
44
template <> const char *BlobAttribute::staticTypeName()
 
45
{
 
46
  return "blob";
 
47
}
 
48
template <> void BlobAttribute::writeValueTo (OStream &os, int version) const
 
49
{
 
50
  Xdr::write <StreamIO> (os, _value.size);
 
51
  Xdr::write <StreamIO> (os, (char *)_value.data,_value.size);
 
52
}
 
53
 
 
54
template <> void BlobAttribute::readValueFrom (IStream &is, int size, int version)
 
55
{
 
56
  Xdr::read <StreamIO> (is, _value.size);
 
57
  Xdr::read <StreamIO> (is, (char *)_value.data, _value.size);
 
58
}
 
59
}
 
60
 
 
61
#ifdef __cplusplus
 
62
extern "C"
 
63
{
 
64
#endif
 
65
 
 
66
  DT_MODULE(1)
 
67
 
 
68
  typedef struct dt_imageio_exr_t
 
69
  {
 
70
    int max_width, max_height;
 
71
    int width, height;
 
72
  }
 
73
  dt_imageio_exr_t;
 
74
 
 
75
  int write_image (dt_imageio_exr_t *exr, const char *filename, const float *in, void *exif, int exif_len, int imgid)
 
76
  {
 
77
    Imf::BlobAttribute::registerAttributeType();
 
78
    Imf::Blob exif_blob= {0};
 
79
    exif_blob.size=exif_len;
 
80
    exif_blob.data=(uint8_t *)exif;
 
81
    Imf::Header header(exr->width,exr->height,1,Imath::V2f (0, 0),1,Imf::INCREASING_Y,Imf::PIZ_COMPRESSION);
 
82
    header.insert("comment",Imf::StringAttribute("Developed using Darktable "PACKAGE_VERSION));
 
83
    header.insert("exif", Imf::BlobAttribute(exif_blob));
 
84
    header.channels().insert("R",Imf::Channel(Imf::FLOAT));
 
85
    header.channels().insert("B",Imf::Channel(Imf::FLOAT));
 
86
    header.channels().insert("G",Imf::Channel(Imf::FLOAT));
 
87
    header.setTileDescription(Imf::TileDescription(100, 100, Imf::ONE_LEVEL));
 
88
    Imf::TiledOutputFile file(filename, header);
 
89
    Imf::FrameBuffer data;
 
90
 
 
91
    uint32_t channelsize=(exr->width*exr->height);
 
92
    float *red=(float *)malloc(channelsize*sizeof(float));
 
93
    float *green=(float *)malloc(channelsize*sizeof(float));
 
94
    float *blue=(float *)malloc(channelsize*sizeof(float));
 
95
 
 
96
    for(uint32_t j=0; j<channelsize; j++)
 
97
    {
 
98
      red[j]=in[j*4+0];
 
99
    }
 
100
    data.insert("R",Imf::Slice(Imf::FLOAT,(char *)red,sizeof(float)*1,sizeof(float)*exr->width));
 
101
 
 
102
    for(uint32_t j=0; j<channelsize; j++)
 
103
    {
 
104
      blue[j]=in[j*4+2];
 
105
    }
 
106
    data.insert("B",Imf::Slice(Imf::FLOAT,(char *)blue,sizeof(float)*1,sizeof(float)*exr->width));
 
107
 
 
108
    for(uint32_t j=0; j<channelsize; j++)
 
109
    {
 
110
      green[j]=in[j*4+1];
 
111
    }
 
112
    data.insert("G",Imf::Slice(Imf::FLOAT,(char *)green,sizeof(float)*1,sizeof(float)*exr->width));
 
113
 
 
114
    file.setFrameBuffer(data);
 
115
    file.writeTiles (0, file.numXTiles() - 1, 0, file.numYTiles() - 1);
 
116
    free(red);
 
117
    free(green);
 
118
    free(blue);
 
119
    return 1;
 
120
  }
 
121
 
 
122
  void*
 
123
  get_params(dt_imageio_module_format_t *self, int *size)
 
124
  {
 
125
    *size = sizeof(dt_imageio_module_data_t);
 
126
    dt_imageio_exr_t *d = (dt_imageio_exr_t *)malloc(sizeof(dt_imageio_exr_t));
 
127
    return d;
 
128
  }
 
129
 
 
130
  void
 
131
  free_params(dt_imageio_module_format_t *self, void *params)
 
132
  {
 
133
    free(params);
 
134
  }
 
135
 
 
136
  int
 
137
  set_params(dt_imageio_module_format_t *self, void *params, int size)
 
138
  {
 
139
    if(size != sizeof(dt_imageio_module_data_t)) return 1;
 
140
    return 0;
 
141
  }
 
142
 
 
143
  int bpp(dt_imageio_module_data_t *p)
 
144
  {
 
145
    return 32;
 
146
  }
 
147
 
 
148
  const char*
 
149
  mime(dt_imageio_module_data_t *data)
 
150
  {
 
151
    return "image/openexr";
 
152
  }
 
153
 
 
154
  const char*
 
155
  extension(dt_imageio_module_data_t *data)
 
156
  {
 
157
    return "exr";
 
158
  }
 
159
 
 
160
  const char*
 
161
  name ()
 
162
  {
 
163
    return _("openexr");
 
164
  }
 
165
 
 
166
// TODO: some quality/compression stuff?
 
167
  void gui_init    (dt_imageio_module_format_t *self) {}
 
168
  void gui_cleanup (dt_imageio_module_format_t *self) {}
 
169
  void gui_reset   (dt_imageio_module_format_t *self) {}
 
170
 
 
171
 
 
172
 
 
173
#ifdef __cplusplus
 
174
}
 
175
#endif