VMTK
Loading...
Searching...
No Matches
vtkvmtkPNGWriter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPNGWriter.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:47:47 $
7Version: $Revision: 1.6 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
22#include "vtkvmtkPNGWriter.h"
23#include "vtkUnsignedCharArray.h"
24#include "vtkImageData.h"
25#include "vtkPointData.h"
26#include "vtkErrorCode.h"
27#include "vtkObjectFactory.h"
28#include "vtkVersion.h"
29#include "vtkInformation.h"
30#include "vtkStreamingDemandDrivenPipeline.h"
31
32#include <math.h>
33
34#include <string.h>
35
36//Ari Edelkind, http://episec.com/people/edelkind/
37
38char b64string[] =
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40
41long base64_encode (char* to, char* from, unsigned int len)
42{
43 char *fromp = from;
44 char *top = to;
45 unsigned char cbyte;
46 unsigned char obyte;
47 char end[3];
48
49 for (; len >= 3; len -= 3) {
50 cbyte = *fromp++;
51 *top++ = b64string[(int)(cbyte >> 2)];
52 obyte = (cbyte << 4) & 0x30; /* 0011 0000 */
53
54 cbyte = *fromp++;
55 obyte |= (cbyte >> 4); /* 0000 1111 */
56 *top++ = b64string[(int)obyte];
57 obyte = (cbyte << 2) & 0x3C; /* 0011 1100 */
58
59 cbyte = *fromp++;
60 obyte |= (cbyte >> 6); /* 0000 0011 */
61 *top++ = b64string[(int)obyte];
62 *top++ = b64string[(int)(cbyte & 0x3F)];/* 0011 1111 */
63 }
64
65 if (len) {
66 end[0] = *fromp++;
67 if (--len) end[1] = *fromp++; else end[1] = 0;
68 end[2] = 0;
69
70 cbyte = end[0];
71 *top++ = b64string[(int)(cbyte >> 2)];
72 obyte = (cbyte << 4) & 0x30; /* 0011 0000 */
73
74 cbyte = end[1];
75 obyte |= (cbyte >> 4);
76 *top++ = b64string[(int)obyte];
77 obyte = (cbyte << 2) & 0x3C; /* 0011 1100 */
78
79 if (len) *top++ = b64string[(int)obyte];
80 else *top++ = '=';
81 *top++ = '=';
82 }
83 *top = 0;
84 return top - to;
85}
86
87
89
91{
92 this->Base64Image = NULL;
93 this->WriteToBase64 = 0;
94 this->FlipImage = 0;
95}
96
98{
99 if (this->Base64Image)
100 {
101 delete[] this->Base64Image;
102 this->Base64Image = NULL;
103 }
104}
105
107{
108 if (!this->WriteToBase64) {
109 Superclass::Write();
110 return;
111 }
112
113 int previousWriteToMemory = this->GetWriteToMemory();
114
115 this->WriteToMemoryOn();
116
117 //Superclass::Write();
118
119 this->SetErrorCode(vtkErrorCode::NoError);
120
121 // Error checking
122 if ( this->GetInput() == NULL )
123 {
124 vtkErrorMacro(<<"Write:Please specify an input!");
125 return;
126 }
127 if (!this->WriteToMemory && !this->GetFileName() && !this->GetFilePattern())
128 {
129 vtkErrorMacro(<<"Write:Please specify either a FileName or a file prefix and pattern");
130 this->SetErrorCode(vtkErrorCode::NoFileNameError);
131 return;
132 }
133
134 // Make sure the file name is allocated
135 this->InternalFileName =
136 new char[(this->GetFileName() ? strlen(this->GetFileName()) : 1) +
137 (this->GetFilePrefix() ? strlen(this->GetFilePrefix()) : 1) +
138 (this->GetFilePattern() ? strlen(this->GetFilePattern()) : 1) + 10];
139
140 // Fill in image information.
141 int *wExtent;
142 this->UpdateInformation();
143 vtkInformation *inInfoImage = this->GetInputPortInformation(0);
144 inInfoImage->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(), wExtent);
145
146 this->FileNumber = wExtent[4];
147 this->MinimumFileNumber = this->MaximumFileNumber = this->FileNumber;
148 this->FilesDeleted = 0;
149 this->UpdateProgress(0.0);
150 // loop over the z axis and write the slices
151 for (this->FileNumber = wExtent[4]; this->FileNumber <= wExtent[5];
152 ++this->FileNumber)
153 {
154 this->MaximumFileNumber = this->FileNumber;
155 int updatedExtent[6] = {wExtent[0], wExtent[1],
156 wExtent[2], wExtent[3],
157 this->FileNumber,
158 this->FileNumber};
159 // determine the name
160 if (this->GetFileName())
161 {
162 sprintf(this->InternalFileName,"%s",this->GetFileName());
163 }
164 else
165 {
166 if (this->GetFilePrefix())
167 {
168 sprintf(this->InternalFileName, this->GetFilePattern(),
169 this->GetFilePrefix(), this->FileNumber);
170 }
171 else
172 {
173 sprintf(this->InternalFileName, this->GetFilePattern(), this->FileNumber);
174 }
175 }
176
177 if (!this->FlipImage)
178 {
179 this->WriteSlice(this->GetInput(), updatedExtent);
180 }
181 else
182 {
183 vtkImageData* flippedInput = vtkImageData::New();
184 flippedInput->DeepCopy(this->GetInput());
185 vtkDataArray* inputArray = this->GetInput()->GetPointData()->GetScalars();
186 vtkDataArray* flippedInputArray = flippedInput->GetPointData()->GetScalars();
187 for (int i=0; i<wExtent[3]-wExtent[2]+1; i++)
188 {
189 for (int j=0; j<wExtent[1]-wExtent[0]+1; j++)
190 {
191 flippedInputArray->SetTuple((wExtent[3]-wExtent[2]-i)*(wExtent[1]-wExtent[0]+1)+j,inputArray->GetTuple(i*(wExtent[1]-wExtent[0]+1)+j));
192 }
193 }
194 this->WriteSlice(flippedInput, updatedExtent);
195 flippedInput->Delete();
196 }
197
198 if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
199 {
200 this->DeleteFiles();
201 break;
202 }
203 this->UpdateProgress((this->FileNumber - wExtent[4])/
204 (wExtent[5] - wExtent[4] + 1.0));
205 }
206 delete [] this->InternalFileName;
207 this->InternalFileName = NULL;
208
209 vtkUnsignedCharArray* result = this->GetResult();
210 char* data = (char*)result->GetPointer(0);
211 int length = result->GetNumberOfTuples() * result->GetNumberOfComponents();
212
213 char* flipped_data = new char[length];
214 if (this->FlipImage)
215 {
216 for (int i=0; i<length; i++)
217 {
218 flipped_data[i] = data[length-i-1];
219 }
220 data = flipped_data;
221 }
222
223 char* encoded_data = new char[2*length];
224 int output_length = base64_encode(encoded_data,data,length);
225 this->SetBase64Image(encoded_data);
226
227 delete[] encoded_data;
228 delete[] flipped_data;
229
230 this->SetWriteToMemory(previousWriteToMemory);
231}
232
233void vtkvmtkPNGWriter::PrintSelf(std::ostream& os, vtkIndent indent)
234{
235 vtkPNGWriter::PrintSelf(os,indent);
236}
Writes .png files.
virtual void SetBase64Image(const char *)
virtual void Write() override
void PrintSelf(std::ostream &os, vtkIndent indent) override
long base64_encode(char *to, char *from, unsigned int len)
vtkStandardNewMacro(vtkvmtkPNGWriter)
char b64string[]