File: exr.cpp

package info (click to toggle)
embree 4.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 100,656 kB
  • sloc: cpp: 228,918; xml: 40,944; ansic: 2,685; python: 812; sh: 635; makefile: 228; csh: 42
file content (70 lines) | stat: -rw-r--r-- 1,921 bytes parent folder | download
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
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "image.h"

#include <iostream>
#include <cstring>
#include <cstdio>

#define TINYEXR_USE_MINIZ 0
//#include "zlib.h"
//Or, if your project uses `stb_image[_write].h`, use their
//zlib implementation:
#define TINYEXR_USE_STB_ZLIB 1
#define TINYEXR_IMPLEMENTATION
#include "tinyexr.h"

namespace embree
{
  /*! read PFM file from disk */
  Ref<Image> loadEXR(const FileName& fileName)
  {
    float* rgba; // width * height * RGBA
    int width;
    int height;
    const char* err = NULL; // or nullptr in C++11

    int ret = LoadEXR(&rgba, &width, &height, fileName.str().c_str(), &err);
    if (ret != TINYEXR_SUCCESS) {
      if (err) {
        std::cerr << "ERR: " << err;
        FreeEXRErrorMessage(err);
      }
      THROW_RUNTIME_ERROR("Could not load image " + fileName.str())
    }

    /* create image and fill with data */
    Ref<Image> img = new Image4f(width,height,fileName);

    for (ssize_t y=0; y<height; y++) {
      for (ssize_t x=0; x<width; x++) {
        float* pix = rgba + (y * width + x) * 4;
        img->set(x,y,Color4(pix[0],pix[1],pix[2],1.0f));
      }
    }
    free(rgba);
    return img;
  }

  /*! store PFM file to disk */
  void storeEXR(const Ref<Image>& img, const FileName& fileName)
  {
    std::vector<Col3f> rgb(img->width * img->height);
    for (size_t y=0; y<img->height; ++y) {
        for (size_t x=0; x<img->width; ++x) {
            Color4 c = img->get(x, y);
            rgb[y * img->width + x] = Col3f(c.r, c.g, c.b);
        }
    }
    const char* err = NULL;
    int ret = SaveEXR((float*)rgb.data(), img->width, img->height, 3, 0, fileName.str().c_str(), &err);
    if (ret != TINYEXR_SUCCESS) {
      if (err) {
        std::cerr << "ERR: " << err;
        FreeEXRErrorMessage(err);
      }
      THROW_RUNTIME_ERROR("Could not save image " + fileName.str())
    }
  }
}