File: image.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 (90 lines) | stat: -rw-r--r-- 2,658 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "image.h"
#include "../../../common/sys/estring.h"

#include <map>
#include <iostream>


namespace embree
{
  double compareImages(Ref<Image> image0, Ref<Image> image1)
  {
    /* compare image size */
    const size_t width = image0->width;
    const size_t height = image0->height;
    if (image1->width != width) return inf;
    if (image1->height != height) return inf;

    /* compare both images */
    double diff = 0.0;
    for (size_t y=0; y<height; y++)
    {
      for (size_t x=0; x<width; x++)
      {
        const Color c0 = image0->get(x,y);
        const Color c1 = image1->get(x,y);
        diff += sqr(fabs(c0.r - c1.r))/3.0f;
        diff += sqr(fabs(c0.g - c1.g))/3.0f;
        diff += sqr(fabs(c0.b - c1.b))/3.0f;
      }
    }

    return diff;
  }
  
  /*! loads an image from a file with auto-detection of format */
  Ref<Image> loadImageFromDisk(const FileName& fileName)
  {
    std::string ext = toLowerCase(fileName.ext());

    if (ext == "bmp" ) return loadSTB(fileName);
    if (ext == "png" ) return loadSTB(fileName);
    if (ext == "jpg" ) return loadSTB(fileName);

    if (ext == "exr" ) return loadEXR(fileName);
    
    if (ext == "pfm" ) return loadPFM(fileName);
    if (ext == "ppm" ) return loadPPM(fileName);
    if (ext == "tga" ) return loadTGA(fileName);
    THROW_RUNTIME_ERROR("image format " + ext + " not supported");
  }

  static std::map<std::string,Ref<Image> > image_cache;
  
  /*! loads an image from a file with auto-detection of format */
  Ref<Image> loadImage(const FileName& fileName, bool cache)
  {
    if (!cache)
      return loadImageFromDisk(fileName);

    if (image_cache.find(fileName) == image_cache.end())
      image_cache[fileName] = loadImageFromDisk(fileName);

    return image_cache[fileName];
  }

  /*! stores an image to file with auto-detection of format */
  void storeImage(const Ref<Image>& img, const FileName& fileName)
  {
    std::string ext = toLowerCase(fileName.ext());

    if (ext == "bmp" ) { storeSTB(img, fileName);  return; }
    if (ext == "png" ) { storeSTB(img, fileName);  return; }
    if (ext == "jpg" ) { storeSTB(img, fileName);  return; }

    if (ext == "exr" ) { storeEXR(img, fileName);  return; }

    if (ext == "pfm" ) { storePFM(img, fileName);  return; }
    if (ext == "ppm" ) { storePPM(img, fileName);  return; }
    if (ext == "tga" ) { storeTga(img, fileName);  return; }
    THROW_RUNTIME_ERROR("image format " + ext + " not supported");
  }

  /*! template instantiations */
  template class ImageT<Col3uc>;
  template class ImageT<Col3f>;

}