Blender V4.3
image_converter.h
Go to the documentation of this file.
1// Copyright (c) 2009 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#ifndef LIBMV_IMAGE_IMAGE_CONVERTER_H
22#define LIBMV_IMAGE_IMAGE_CONVERTER_H
23
25
26namespace libmv {
27
28// The factor comes from http://www.easyrgb.com/
29// RGB to XYZ : Y is the luminance channel
30// var_R * 0.2126 + var_G * 0.7152 + var_B * 0.0722
31template <typename T>
32inline T RGB2GRAY(const T r, const T g, const T b) {
33 return static_cast<T>(r * 0.2126 + g * 0.7152 + b * 0.0722);
34}
35
36/*
37// Specialization for the uchar type. (that do not want to work...)
38template<>
39inline unsigned char RGB2GRAY<unsigned char>(const unsigned char r,
40 const unsigned char g,
41 const unsigned char b) {
42 return (unsigned char)(r * 0.2126 + g * 0.7152 + b * 0.0722 +0.5);
43}*/
44
45template <class ImageIn, class ImageOut>
46void Rgb2Gray(const ImageIn& imaIn, ImageOut* imaOut) {
47 // It is all fine to cnvert RGBA image here as well,
48 // all the additional channels will be nicely ignored.
49 assert(imaIn.Depth() >= 3);
50
51 imaOut->Resize(imaIn.Height(), imaIn.Width(), 1);
52 // Convert each RGB pixel into Gray value (luminance)
53
54 for (int j = 0; j < imaIn.Height(); ++j) {
55 for (int i = 0; i < imaIn.Width(); ++i) {
56 (*imaOut)(j, i) =
57 RGB2GRAY(imaIn(j, i, 0), imaIn(j, i, 1), imaIn(j, i, 2));
58 }
59 }
60}
61
62// Convert given float image to an unsigned char array of pixels.
63template <class Image>
64unsigned char* FloatImageToUCharArray(const Image& image) {
65 unsigned char* buffer =
66 new unsigned char[image.Width() * image.Height() * image.Depth()];
67
68 for (int y = 0; y < image.Height(); y++) {
69 for (int x = 0; x < image.Width(); x++) {
70 for (int d = 0; d < image.Depth(); d++) {
71 int index = (y * image.Width() + x) * image.Depth() + d;
72 buffer[index] = 255.0 * image(y, x, d);
73 }
74 }
75 }
76
77 return buffer;
78}
79
80} // namespace libmv
81
82#endif // LIBMV_IMAGE_IMAGE_CONVERTER_H
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
local_group_size(16, 16) .push_constant(Type b
void Rgb2Gray(const ImageIn &imaIn, ImageOut *imaOut)
unsigned char * FloatImageToUCharArray(const Image &image)
T RGB2GRAY(const T r, const T g, const T b)