File: oiio.cpp

package info (click to toggle)
embree 3.12.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,412 kB
  • sloc: cpp: 173,822; xml: 3,737; ansic: 2,955; python: 1,628; sh: 480; makefile: 193; csh: 42
file content (76 lines) | stat: -rw-r--r-- 2,487 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
71
72
73
74
75
76
// Copyright 2009-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#ifdef USE_OPENIMAGEIO

#include "image.h"
#include <iostream>
#include <vector>

/*! include OpenImageIO headers */
#include <OpenImageIO/imageio.h>
OIIO_NAMESPACE_USING

namespace embree
{
  Ref<Image> loadOIIO(const FileName& fileName)
  {
    std::unique_ptr<ImageInput> in = ImageInput::open(fileName.str().c_str());
    if (!in)
      THROW_RUNTIME_ERROR("error opening file " + fileName.str());

    const ImageSpec& spec = in->spec();
    size_t width = spec.width;
    size_t height = spec.height;
    size_t channels = spec.nchannels;
    std::vector<unsigned char> pixels(width*height*channels);
    in->read_image(TypeDesc::UINT8, pixels.data());
    in->close();

    Image* out = new Image4uc(width, height, fileName);
    const float rcpMaxColor = 1.f/255.f;
    for (size_t y = 0; y < height; y++)
    {
      for (size_t x = 0; x < width; x++)
      {
        float r = (channels > 0) ? (float(pixels[(y*width+x)*channels+0]) * rcpMaxColor) : 0.f;
        float g = (channels > 1) ? (float(pixels[(y*width+x)*channels+1]) * rcpMaxColor) : r;
        float b = (channels > 2) ? (float(pixels[(y*width+x)*channels+2]) * rcpMaxColor) : ((channels == 1) ? r : 0.f);
        float a = (channels > 3) ? (float(pixels[(y*width+x)*channels+3]) * rcpMaxColor) : 1.f;
        out->set(x, height-y-1, Color4(r,g,b,a)); // flip image
      }
    }

    return out;
  }

  void storeOIIO(const Ref<Image>& img, const FileName& fileName)
  {
    std::unique_ptr<ImageOutput> out = ImageOutput::create(fileName.c_str());
    if (!out) THROW_RUNTIME_ERROR("unsupported output file format " + fileName.str());

    std::vector<unsigned char> pixels(img->width*img->height*3);
    const float maxColor = 255.f;
    for (size_t y = 0; y < img->height; y++)
    {
      for (size_t x = 0; x < img->width; x++)
      {
        Color4 c = img->get(x, y);
        pixels[(y*img->width+x)*3+0] = (unsigned char)(clamp(c.r) * maxColor);
        pixels[(y*img->width+x)*3+1] = (unsigned char)(clamp(c.g) * maxColor);
        pixels[(y*img->width+x)*3+2] = (unsigned char)(clamp(c.b) * maxColor);
      }
    }

    ImageSpec spec(int(img->width), int(img->height), 3, TypeDesc::UINT8);
    if (!out->open(fileName.c_str(), spec))
    {
      THROW_RUNTIME_ERROR("error opening file " + fileName.str());
    }
    out->write_image(TypeDesc::UINT8, pixels.data());
    out->close();
  }
}

#endif // USE_OPENIMAGEIO