1 /*
2 * This file is part of RawTherapee.
3 *
4 * Copyright (c) 2004-2017 Gabor Horvath <hgabor@rawtherapee.com>
5 *
6 * RawTherapee is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * RawTherapee is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with RawTherapee. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <memory>
21
22 #include "labimage.h"
23
24 namespace rtengine
25 {
26
LabImage(int w,int h)27 LabImage::LabImage (int w, int h) : W(w), H(h)
28 {
29 allocLab(w, h);
30 }
31
~LabImage()32 LabImage::~LabImage ()
33 {
34 deleteLab();
35 }
36
CopyFrom(LabImage * Img)37 void LabImage::CopyFrom(LabImage *Img)
38 {
39 memcpy(data, Img->data, W * H * 3 * sizeof(float));
40 }
41
getPipetteData(float & v1,float & v2,float & v3,int posX,int posY,int squareSize)42 void LabImage::getPipetteData (float &v1, float &v2, float &v3, int posX, int posY, int squareSize)
43 {
44 float accumulator_L = 0.f;
45 float accumulator_a = 0.f;
46 float accumulator_b = 0.f;
47 unsigned long int n = 0;
48 int halfSquare = squareSize / 2;
49
50 for (int iy = posY - halfSquare; iy < posY - halfSquare + squareSize; ++iy) {
51 for (int ix = posX - halfSquare; ix < posX - halfSquare + squareSize; ++ix) {
52 if (ix >= 0 && iy >= 0 && ix < W && iy < H) {
53 accumulator_L += L[iy][ix];
54 accumulator_a += a[iy][ix];
55 accumulator_b += b[iy][ix];
56 ++n;
57 }
58 }
59 }
60
61 v1 = n ? accumulator_L / float(n) : 0.f;
62 v2 = n ? accumulator_a / float(n) : 0.f;
63 v3 = n ? accumulator_b / float(n) : 0.f;
64 }
65
allocLab(size_t w,size_t h)66 void LabImage::allocLab(size_t w, size_t h)
67 {
68 L = new float*[h];
69 a = new float*[h];
70 b = new float*[h];
71
72 data = new float [w * h * 3];
73 float * index = data;
74
75 for (size_t i = 0; i < h; i++) {
76 L[i] = index + i * w;
77 }
78
79 index += w * h;
80
81 for (size_t i = 0; i < h; i++) {
82 a[i] = index + i * w;
83 }
84
85 index += w * h;
86
87 for (size_t i = 0; i < h; i++) {
88 b[i] = index + i * w;
89 }
90 }
91
deleteLab()92 void LabImage::deleteLab()
93 {
94 delete [] L;
95 delete [] a;
96 delete [] b;
97 delete [] data;
98 }
99
reallocLab()100 void LabImage::reallocLab()
101 {
102 allocLab(W, H);
103 }
104
105 }
106