Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.git] / source / blender / compositor / operations / COM_ViewerOperation.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_ViewerOperation.h"
24 #include "COM_SocketConnection.h"
25 #include "BLI_listbase.h"
26 #include "BKE_image.h"
27 #include "WM_api.h"
28 #include "WM_types.h"
29 #include "PIL_time.h"
30 #include "BLI_utildefines.h"
31 #include "BLI_math_color.h"
32 #include "BLI_math_vector.h"
33
34 extern "C" {
35         #include "MEM_guardedalloc.h"
36         #include "IMB_imbuf.h"
37         #include "IMB_imbuf_types.h"
38 }
39
40
41 ViewerOperation::ViewerOperation() : ViewerBaseOperation()
42 {
43         this->addInputSocket(COM_DT_COLOR);
44         this->addInputSocket(COM_DT_VALUE);
45         this->addInputSocket(COM_DT_VALUE);
46
47         this->m_imageInput = NULL;
48         this->m_alphaInput = NULL;
49         this->m_depthInput = NULL;
50 }
51
52 void ViewerOperation::initExecution()
53 {
54         // When initializing the tree during initial load the width and height can be zero.
55         this->m_imageInput = getInputSocketReader(0);
56         this->m_alphaInput = getInputSocketReader(1);
57         this->m_depthInput = getInputSocketReader(2);
58         this->m_doDepthBuffer = (this->m_depthInput != NULL);
59         ViewerBaseOperation::initExecution();
60 }
61
62 void ViewerOperation::deinitExecution()
63 {
64         this->m_imageInput = NULL;
65         this->m_alphaInput = NULL;
66         this->m_depthInput = NULL;
67         ViewerBaseOperation::deinitExecution();
68 }
69
70
71 void ViewerOperation::executeRegion(rcti *rect, unsigned int tileNumber)
72 {
73         float *buffer = this->m_outputBuffer;
74         float *depthbuffer = this->m_depthBuffer;
75         if (!buffer) return;
76         const int x1 = rect->xmin;
77         const int y1 = rect->ymin;
78         const int x2 = rect->xmax;
79         const int y2 = rect->ymax;
80         const int offsetadd = (this->getWidth() - (x2 - x1));
81         const int offsetadd4 = offsetadd * 4;
82         int offset = (y1 * this->getWidth() + x1);
83         int offset4 = offset * 4;
84         float alpha[4], depth[4];
85         int x;
86         int y;
87         bool breaked = false;
88
89         for (y = y1; y < y2 && (!breaked); y++) {
90                 for (x = x1; x < x2; x++) {
91                         this->m_imageInput->read(&(buffer[offset4]), x, y, COM_PS_NEAREST);
92                         if (this->m_alphaInput != NULL) {
93                                 this->m_alphaInput->read(alpha, x, y, COM_PS_NEAREST);
94                                 buffer[offset4 + 3] = alpha[0];
95                         }
96                         if (m_depthInput) {
97                                 this->m_depthInput->read(depth, x, y, COM_PS_NEAREST);
98                                 depthbuffer[offset] = depth[0];
99                         } 
100
101                         offset ++;
102                         offset4 += 4;
103                 }
104                 if (isBreaked()) {
105                         breaked = true;
106                 }
107                 offset += offsetadd;
108                 offset4 += offsetadd4;
109         }
110         updateImage(rect);
111 }