Blender V4.3
array_nd.cc
Go to the documentation of this file.
1// Copyright (c) 2007, 2008 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21#include <cmath>
22#include <iostream>
23#include "libmv/image/image.h"
24
25namespace libmv {
26
27void FloatArrayToScaledByteArray(const Array3Df& float_array,
28 Array3Du* byte_array,
29 bool automatic_range_detection) {
30 byte_array->ResizeLike(float_array);
31 float minval = HUGE_VAL;
32 float maxval = -HUGE_VAL;
33 if (automatic_range_detection) {
34 for (int i = 0; i < float_array.Height(); ++i) {
35 for (int j = 0; j < float_array.Width(); ++j) {
36 for (int k = 0; k < float_array.Depth(); ++k) {
37 minval = std::min(minval, float_array(i, j, k));
38 maxval = std::max(maxval, float_array(i, j, k));
39 }
40 }
41 }
42 } else {
43 minval = 0;
44 maxval = 1;
45 }
46 for (int i = 0; i < float_array.Height(); ++i) {
47 for (int j = 0; j < float_array.Width(); ++j) {
48 for (int k = 0; k < float_array.Depth(); ++k) {
49 float unscaled = (float_array(i, j, k) - minval) / (maxval - minval);
50 (*byte_array)(i, j, k) = (unsigned char)(255 * unscaled);
51 }
52 }
53 }
54}
55
57 Array3Df* float_array) {
58 float_array->ResizeLike(byte_array);
59 for (int i = 0; i < byte_array.Height(); ++i) {
60 for (int j = 0; j < byte_array.Width(); ++j) {
61 for (int k = 0; k < byte_array.Depth(); ++k) {
62 (*float_array)(i, j, k) = float(byte_array(i, j, k)) / 255.0f;
63 }
64 }
65 }
66}
67
68void SplitChannels(const Array3Df& input,
69 Array3Df* channel0,
70 Array3Df* channel1,
71 Array3Df* channel2) {
72 assert(input.Depth() >= 3);
73 channel0->Resize(input.Height(), input.Width());
74 channel1->Resize(input.Height(), input.Width());
75 channel2->Resize(input.Height(), input.Width());
76 for (int row = 0; row < input.Height(); ++row) {
77 for (int column = 0; column < input.Width(); ++column) {
78 (*channel0)(row, column) = input(row, column, 0);
79 (*channel1)(row, column) = input(row, column, 1);
80 (*channel2)(row, column) = input(row, column, 2);
81 }
82 }
83}
84
85void PrintArray(const Array3Df& array) {
86 using namespace std;
87
88 printf("[\n");
89 for (int r = 0; r < array.Height(); ++r) {
90 printf("[");
91 for (int c = 0; c < array.Width(); ++c) {
92 if (array.Depth() == 1) {
93 printf("%11f, ", array(r, c));
94 } else {
95 printf("[");
96 for (int k = 0; k < array.Depth(); ++k) {
97 printf("%11f, ", array(r, c, k));
98 }
99 printf("],");
100 }
101 }
102 printf("],\n");
103 }
104 printf("]\n");
105}
106
107} // namespace libmv
3D array (row, column, channel).
Definition array_nd.h:332
void Resize(int height, int width, int depth=1)
Definition array_nd.h:341
int Depth() const
Definition array_nd.h:347
int Height() const
Definition array_nd.h:345
int Width() const
Definition array_nd.h:346
void ResizeLike(const ArrayND< D, N > &other)
Definition array_nd.h:109
#define printf
void SplitChannels(const Array3Df &input, Array3Df *channel0, Array3Df *channel1, Array3Df *channel2)
Definition array_nd.cc:68
void ByteArrayToScaledFloatArray(const Array3Du &byte_array, Array3Df *float_array)
Convert a byte array into a float array by dividing values by 255.
Definition array_nd.cc:56
void PrintArray(const Array3Df &array)
Definition array_nd.cc:85
void FloatArrayToScaledByteArray(const Array3Df &float_array, Array3Du *byte_array, bool automatic_range_detection)
Definition array_nd.cc:27