1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5
6
7 //----------------------------------------------------------------------------
8 //
9 // Add a preview image to an OpenEXR file.
10 //
11 //----------------------------------------------------------------------------
12
13
14 #include "makePreview.h"
15
16 #include <ImfInputFile.h>
17 #include <ImfOutputFile.h>
18 #include <ImfTiledOutputFile.h>
19 #include <ImfRgbaFile.h>
20 #include <ImfPreviewImage.h>
21 #include <ImfArray.h>
22 #include <ImathMath.h>
23 #include <ImathFun.h>
24 #include <math.h>
25 #include <iostream>
26 #include <algorithm>
27
28 #include <OpenEXRConfig.h>
29 using namespace OPENEXR_IMF_NAMESPACE;
30 using namespace IMATH_NAMESPACE;
31 using namespace std;
32
33
34 namespace {
35
36 float
knee(float x,float f)37 knee (float x, float f)
38 {
39 return log (x * f + 1) / f;
40 }
41
42
43 unsigned char
gamma(half h,float m)44 gamma (half h, float m)
45 {
46 //
47 // Conversion from half to unsigned char pixel data,
48 // with gamma correction. The conversion is the same
49 // as in the exrdisplay program's ImageView class,
50 // except with defog, kneeLow, and kneeHigh fixed
51 // at 0.0, 0.0, and 5.0 respectively.
52 //
53
54 float x = max (0.f, h * m);
55
56 if (x > 1)
57 x = 1 + knee (x - 1, 0.184874f);
58
59 return (unsigned char) (IMATH_NAMESPACE::clamp (std::pow (x, 0.4545f) * 84.66f,
60 0.f,
61 255.f));
62 }
63
64
65 void
generatePreview(const char inFileName[],float exposure,int previewWidth,int & previewHeight,Array2D<PreviewRgba> & previewPixels)66 generatePreview (const char inFileName[],
67 float exposure,
68 int previewWidth,
69 int &previewHeight,
70 Array2D <PreviewRgba> &previewPixels)
71 {
72 //
73 // Read the input file
74 //
75
76 RgbaInputFile in (inFileName);
77
78 Box2i dw = in.dataWindow();
79 float a = in.pixelAspectRatio();
80 int w = dw.max.x - dw.min.x + 1;
81 int h = dw.max.y - dw.min.y + 1;
82
83 Array2D <Rgba> pixels (h, w);
84 in.setFrameBuffer (ComputeBasePointer (&pixels[0][0], dw), 1, w);
85 in.readPixels (dw.min.y, dw.max.y);
86
87 //
88 // Make a preview image
89 //
90
91 previewHeight = max (int (h / (w * a) * previewWidth + .5f), 1);
92 previewPixels.resizeErase (previewHeight, previewWidth);
93
94 double fx = (previewWidth > 1)? (double (w - 1) / (previewWidth - 1)): 1;
95 double fy = (previewHeight > 1)? (double (h - 1) / (previewHeight - 1)): 1;
96 float m = std::pow (2.f, IMATH_NAMESPACE::clamp (exposure + 2.47393f, -20.f, 20.f));
97
98 for (int y = 0; y < previewHeight; ++y)
99 {
100 for (int x = 0; x < previewWidth; ++x)
101 {
102 PreviewRgba &preview = previewPixels[y][x];
103 const Rgba &pixel = pixels[int (y * fy + .5f)][int (x * fx + .5f)];
104
105 preview.r = gamma (pixel.r, m);
106 preview.g = gamma (pixel.g, m);
107 preview.b = gamma (pixel.b, m);
108 preview.a = int (IMATH_NAMESPACE::clamp (pixel.a * 255.f, 0.f, 255.f) + .5f);
109 }
110 }
111 }
112
113 } // namespace
114
115
116 void
makePreview(const char inFileName[],const char outFileName[],int previewWidth,float exposure,bool verbose)117 makePreview (const char inFileName[],
118 const char outFileName[],
119 int previewWidth,
120 float exposure,
121 bool verbose)
122 {
123 if (verbose)
124 cout << "generating preview image" << endl;
125
126 Array2D <PreviewRgba> previewPixels;
127 int previewHeight;
128
129 generatePreview (inFileName,
130 exposure,
131 previewWidth,
132 previewHeight,
133 previewPixels);
134
135 InputFile in (inFileName);
136 Header header = in.header();
137
138 header.setPreviewImage
139 (PreviewImage (previewWidth, previewHeight, &previewPixels[0][0]));
140
141 if (verbose)
142 cout << "copying " << inFileName << " to " << outFileName << endl;
143
144 if (header.hasTileDescription())
145 {
146 TiledOutputFile out (outFileName, header);
147 out.copyPixels (in);
148 }
149 else
150 {
151 OutputFile out (outFileName, header);
152 out.copyPixels (in);
153 }
154
155 if (verbose)
156 cout << "done." << endl;
157 }
158