VMTK
transferFunction.cpp
1 /*
2  * transferFunction.cpp: constrĂ³i diferentes funcoes de transferencia
3  * que mapeiam os valores escalares da imagem 3D em
4  * propriedades opticas (niveis de cinza/cores e
5  * e opacidade)
6  *
7  * Copyright (C) 2013 Wu Shin-Ting, FEEC, Unicamp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 #include <iostream>
24 #include <string.h>
25 #include "transferFunction.h"
26 #include <cmath>
27 
28 #ifndef SQR
29 #define SQR(a) ((a) * (a))
30 #endif
31 #ifndef CUB
32 #define CUB(a) ((a) * (a) * (a))
33 #endif
34 
37 void TransferFunction::GetGrayScaleTFE (int min, int max, int *elemSize, unsigned char **ptr) {
38  double aux;
39 
40  *elemSize = max-min+2;
41 
42  *ptr = new unsigned char[*elemSize];
43  aux = 1./(max-min+1)*9;
44 
45  (*ptr)[0] = 0;
46  for (int i = 1 ; i < *elemSize; i++)
47  (*ptr)[i] = (unsigned char)(log(i*aux+1.)*255);
48 }
49 
51 void TransferFunction::GetGrayScaleTFL (int min, int max, int *elemSize, unsigned char **ptr) {
52  double aux;
53 
54  *elemSize = max-min+2;
55 
56  *ptr = new unsigned char[*elemSize];
57  aux = 1./(max-min+1);
58 
59  (*ptr)[0] = 0;
60  for (int i = 1 ; i < *elemSize; i++)
61  (*ptr)[i] = (unsigned char)(i*aux*255);
62 }
63 
64 void TransferFunction::GetGrayScaleTFS (int min, int max, int *elemSize, unsigned char **ptr) {
65  double aux;
66 
67  *elemSize = max-min+2;
68 
69  *ptr = new unsigned char[*elemSize];
70  aux = 1./(max-min+1);
71 
72  (*ptr)[0] = 0;
73  for (int i = 1 ; i < *elemSize; i++)
74  (*ptr)[i] = (unsigned char)(SQR(i*aux)*255);
75 }
76 
77 void TransferFunction::GetGrayScaleTFC (int min, int max, int *elemSize, unsigned char **ptr) {
78  double aux;
79 
80  *elemSize = max-min+2;
81 
82  *ptr = new unsigned char[*elemSize];
83  aux = 1./(max-min+1);
84 
85  (*ptr)[0] = 0;
86  for (int i = 1 ; i < *elemSize; i++)
87  (*ptr)[i] = (unsigned char)(CUB(i*aux)*255);
88 }
89 
90 // Constroi uma funcao de transferencia que mapeia em [0,255] os valores
91 // do intervalo [umin,umax] do dominio [0,2^{nbitsalloc}]
92 void TransferFunction::GetGrayScaleTFD (int min, int max, int *elemSize, unsigned char **ptr, unsigned short nbitsalloc) {
93  double aux;
94  int node1, node2;
95 
96  *elemSize = 256;
97 
98  *ptr = new unsigned char[*elemSize];
99 
100  aux = (1.*pow(2,8))/pow(2,nbitsalloc);
101 
102  node1 = (int)(min*aux);
103  node2 = (int)(max*aux);
104 
105  for (int i = 0 ; i < node1; i++)
106  (*ptr)[i] = 0;
107  for (int i = node1 ; i < node2+1; i++)
108  (*ptr)[i] = (unsigned char)((1.0*(i-node1))/(node2-node1)*255);
109  for (int i = node2+1; i < *elemSize; i++)
110  (*ptr)[i] = 0;
111 }
112 
113 void TransferFunction::GetGrayScaleTF (int tag, int min, int max,
114  int *elemSize, unsigned char **ptr) {
115  double aux;
116 
117  switch (tag) {
118  case 0:
119  GetGrayScaleTFE (min, max, elemSize, ptr);
120  break;
121  case 1:
122  GetGrayScaleTFL (min, max, elemSize, ptr);
123  break;
124  case 2:
125  GetGrayScaleTFS (min, max, elemSize, ptr);
126  break;
127  case 3:
128  GetGrayScaleTFC (min, max, elemSize, ptr);
129  break;
130  }
131 }
132 
133 void TransferFunction::GetGrayScaleTF (int tag, int min, int max,
134  int *elemSize, unsigned char **ptr, unsigned short nbitsalloc) {
135  double aux;
136 
137  switch (tag) {
138  case 4:
139  GetGrayScaleTFD (min, max, elemSize, ptr, nbitsalloc);
140  break;
141  }
142 }
143 
144 void TransferFunction::GetRGBTF (int tag, int min, int max,
145  int *elemSize, unsigned char **ptr) {
146 
147  // Variavel tag reservado para extensoes futuras
148 
149  unsigned char tabelaCor [] = {
150  0, 0, 0,
151  51, 0, 0,
152  102, 0, 0,
153  153, 0, 0,
154  204, 0, 0,
155  255, 0 , 0,
156  255, 51, 0,
157  255, 102, 0,
158  255, 153, 0,
159  255, 204, 0,
160  255, 255, 0,
161  204, 255, 0,
162  153, 255, 0,
163  102, 255, 0,
164  51, 255, 0,
165  0, 255, 0,
166  0, 255, 51,
167  0, 255, 102,
168  0, 255, 153,
169  0, 255, 204,
170  0, 255, 255,
171  0, 204, 255,
172  0, 153, 255,
173  0, 102, 255,
174  0, 51, 255,
175  0, 0, 255,
176  51, 0, 255,
177  102, 0, 255,
178  153, 0, 255,
179  204, 0, 255,
180  255, 0, 255,
181  255, 51, 255,
182  255, 102, 255,
183  255, 153, 255,
184  255, 204, 244,
185  255, 255, 255
186  };
187 
188  *elemSize = sizeof(tabelaCor);
189 
190  *ptr = new unsigned char[*elemSize];
191 
192  memcpy (*ptr, tabelaCor, *elemSize);
193 }