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 "flatten_layers.h"
20
21 #include "progress_dialog.h"
22 #include "logger.h"
23 #include "layer_stack.h"
24 #include "base_layer.h"
25 #include "channel_manager.h"
26 #include "image_io.h"
27 #include "str.h"
28 #include <map>
29
flattenLayers(int view,deProgressDialog & progressDialog,const std::string & fileName,const std::string & type,bool saveAll,deLayerStack & layerStack,deChannelManager & previewChannelManager)30 bool flattenLayers(int view, deProgressDialog& progressDialog, const std::string& fileName, const std::string& type, bool saveAll, deLayerStack& layerStack, deChannelManager& previewChannelManager)
31 {
32 bool result = true;
33 logInfo("flattenLayers");
34
35 std::map<int, int> channelUsage;
36 int i;
37 int n = layerStack.getSize();
38 for (i = 0; i < n; i++)
39 {
40 const deBaseLayer* layer = layerStack.startReadLayer(i);
41
42 layer->updateChannelUsage(channelUsage, i);
43
44 layerStack.finishReadLayer(i);
45 }
46
47 unsigned int index;
48 int progress = 0;
49 for (index = 0; index <= (unsigned int)view; index++)
50 {
51 logInfo("flattenLayers index: " + str(index));
52 std::map<int, int>::iterator i;
53 int previous = index - 1;
54 if (previous >= 0)
55 {
56 for (i = channelUsage.begin(); i != channelUsage.end(); i++)
57 {
58 int c = i->first;
59 int l = i->second;
60 if (l == previous)
61 {
62 logInfo("deallocate " + str(c));
63 previewChannelManager.tryDeallocateChannel(c);
64 }
65 }
66 }
67
68 deBaseLayer* layer = layerStack.getLayer(index);
69
70 std::string label = str(index);
71
72 progressDialog.update(progress, label);
73
74 layer->allocateChannels();
75
76 logInfo("flattenLayers process index: " + str(index));
77 bool r = layer->processFull();
78 if (r)
79 {
80 if (saveAll)
81 {
82 const deImage& image = layer->getLayerImage();
83 const std::string f = insertIndex(fileName, index);
84 saveImage(f, image, type, previewChannelManager);
85 }
86 }
87 else
88 {
89 logError("flattenLayers - error on layer " + str(index));
90 result = false;
91 // stop loop
92 index = view + 1;
93 }
94
95 if (view > 0)
96 {
97 progress = 100 * index / view;
98 }
99 else
100 {
101 progress = 100;
102 }
103
104 progressDialog.update(progress, label);
105 }
106
107 progressDialog.update(100, "finished");
108
109 if ((result) && (!saveAll))
110 {
111 logInfo("flattenLayers save final image");
112 // take the final image
113 deBaseLayer* layer = layerStack.getLayer(view);
114 const deImage& image = layer->getLayerImage();
115
116 // save it
117 result = saveImage(fileName, image, type, previewChannelManager);
118 }
119
120 logInfo("flattenLayers DONE");
121
122 return result;
123 }
124
125