1 /*
2 delaboratory - color correction utility
3 Copyright (C) 2011 Jacek Poplawski
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "image.h"
20 #include "channel_manager.h"
21 #include "logger.h"
22 #include "str.h"
23 #include "color_space_utils.h"
24 #include <cassert>
25
deImage(const deColorSpace & _colorSpace,deChannelManager & _channelManager)26 deImage::deImage(const deColorSpace& _colorSpace, deChannelManager& _channelManager)
27 :colorSpace(_colorSpace), channelManager(_channelManager)
28 {
29 logInfo("image constructor");
30 int i;
31 int s = getColorSpaceSize(colorSpace);
32 for (i = 0; i < MAX_COLOR_SPACE_SIZE; i++)
33 {
34 if (i < s)
35 {
36 channelsAllocated[i] = channelManager.reserveNewChannel();
37 }
38 else
39 {
40 channelsAllocated[i] = -1;
41 }
42 }
43 }
44
~deImage()45 deImage::~deImage()
46 {
47 logInfo("image destructor");
48 int i;
49 int s = getColorSpaceSize(colorSpace);
50 for (i = 0; i < s; i++)
51 {
52 int a = channelsAllocated[i];
53 logInfo("destroying channel " + str(a));
54 channelManager.freeChannel(a);
55 }
56 }
57
allocateChannels()58 void deImage::allocateChannels()
59 {
60 int s = getColorSpaceSize(colorSpace);
61 int i;
62 for (i = 0; i < s; i++)
63 {
64 channelManager.tryAllocateChannel(channelsAllocated[i]);
65 }
66 }
67
getChannelIndex(int n) const68 int deImage::getChannelIndex(int n) const
69 {
70 if (n < 0)
71 {
72 logError("deImage::getChannelIndex n: " + str(n));
73 }
74 else if (n >= MAX_COLOR_SPACE_SIZE)
75 {
76 logError("deImage::getChannelIndex n: " + str(n));
77 }
78 return channelsAllocated[n];
79 }
80
getColorSpace() const81 deColorSpace deImage::getColorSpace() const
82 {
83 return colorSpace;
84 }
85
updateChannelUsage(std::map<int,int> & channelUsage,int index) const86 void deImage::updateChannelUsage(std::map<int, int>& channelUsage, int index) const
87 {
88 int i;
89 int s = getColorSpaceSize(colorSpace);
90 for (i = 0; i < s; i++)
91 {
92 int c = channelsAllocated[i];
93 channelUsage[c] = index;
94 }
95 }
96
startRead(int channel) const97 const deValue* deImage::startRead(int channel) const
98 {
99 #ifdef DEBUG_LOG
100 logInfo("image start read " + str(channel));
101 #endif
102
103 if (channel < 0)
104 {
105 logError("image start read " + str(channel));
106 return NULL;
107 }
108 else if (channel >= MAX_COLOR_SPACE_SIZE)
109 {
110 logError("image start read " + str(channel));
111 return NULL;
112 }
113 int index = channelsAllocated[channel];
114
115 const deValue* values = channelManager.startRead(index);
116
117 return values;
118 }
119
finishRead(int channel) const120 void deImage::finishRead(int channel) const
121 {
122 #ifdef DEBUG_LOG
123 logInfo("image finish read " + str(channel));
124 #endif
125 if (channel < 0)
126 {
127 logError("image finish read " + str(channel));
128 return;
129 }
130 else if (channel >= MAX_COLOR_SPACE_SIZE)
131 {
132 logError("image finish read " + str(channel));
133 return;
134 }
135 int index = channelsAllocated[channel];
136 channelManager.finishRead(index);
137
138 }
139
startWrite(int channel)140 deValue* deImage::startWrite(int channel)
141 {
142 #ifdef DEBUG_LOG
143 logInfo("image start write " + str(channel));
144 #endif
145
146 if (channel < 0)
147 {
148 logError("image start write " + str(channel));
149 return NULL;
150 }
151 else if (channel >= MAX_COLOR_SPACE_SIZE)
152 {
153 logError("image start write " + str(channel));
154 return NULL;
155 }
156
157 int index = channelsAllocated[channel];
158
159 deValue* values = channelManager.startWrite(index);
160
161 return values;
162 }
163
finishWrite(int channel)164 void deImage::finishWrite(int channel)
165 {
166 #ifdef DEBUG_LOG
167 logInfo("image finish write " + str(channel));
168 #endif
169 if (channel < 0)
170 {
171 logError("image finish write " + str(channel));
172 return;
173 }
174 else if (channel >= MAX_COLOR_SPACE_SIZE)
175 {
176 logError("image finish write " + str(channel));
177 return;
178 }
179 int index = channelsAllocated[channel];
180 channelManager.finishWrite(index);
181
182 }
183
getChannelSize() const184 const deSize deImage::getChannelSize() const
185 {
186 return channelManager.getChannelSizeFromChannelManager();
187 }
188
189