forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFVTiff.m
194 lines (148 loc) · 5.33 KB
/
FVTiff.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#include "FVTiff.h"
#include "tiffio.h"
#include "CoreFoundation/CFByteOrder.h"
#include "string.h"
#include <stdio.h>
static TIFFExtendProc _TIFFParentExtender = NULL;
static void _FVTIFFDefaultDirectory(TIFF *tif);
int FV_Read_DIM_INFO(const char* data, FV_MM_DIM_INFO* info)
{
memset(info, 0, sizeof(FV_MM_DIM_INFO));
strncpy(info->Name, data, FV_DIMNAME_LENGTH);
data += FV_DIMNAME_LENGTH;
info->Size = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
*((unsigned long long*) &(info->Origin)) = CFSwapInt64LittleToHost(*((unsigned long long*) data));
data += sizeof(unsigned long long);
*((unsigned long long*) &(info->Resolution)) = CFSwapInt64LittleToHost(*((unsigned long long*) data));
data += sizeof(unsigned long long);
strncpy(info->Units, data, FV_UNITS_LENGTH);
return 1;
}
int FV_Read_MM_HEAD(const char* data, FV_MM_HEAD* head) // for now I don't quite understand how the MM_HEAD is stored, so I will only fill in
{ // the members I feel kinda confident about
int i = 0;
memset(head, 0, sizeof(FV_MM_HEAD));
head->HeaderFlag = CFSwapInt16LittleToHost(*((unsigned short*) data));
data += sizeof(unsigned short);
head->Status = *((unsigned char*) data);
data += sizeof(unsigned char);
head->ImageType = *((unsigned char*) data);
data += sizeof(unsigned char);
strncpy(head->Name, data, FV_IMAGE_NAME_LENGTH);
data += FV_IMAGE_NAME_LENGTH;
head->Data = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
head->NumberOfColors = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
head->MM_256_Colors = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
head->MM_All_Colors = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
head->CommentSize = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
head->Comment = CFSwapInt32LittleToHost(*((unsigned int*) data));
data += sizeof(unsigned int);
for (i = 0; i < FV_SPATIAL_DIMENSION; i++)
{
FV_Read_DIM_INFO(data, &head->DimInfo[i]);
data += sizeof(FV_MM_DIM_INFO);
}
return 1;
}
NSXMLDocument * XML_from_FVTiff(NSString* srcFile)
{
NSXMLElement *rootElement = [[NSXMLElement alloc] initWithName:@"FVTiff Meta-Data"];
NSXMLDocument *xmlDocument = 0;
char* desc = 0;
int success = 0;
TIFF* tif = TIFFOpen([srcFile UTF8String], "r");
if(tif)
success = TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &desc);
if (success)
{
int i;
int descLen = strlen(desc);
int lineStart = 0;
char lineBuffer[256];
char* line = lineBuffer;
line[0] = 0;
NSMutableArray *elements = [NSMutableArray array];
for (i = 0; i < descLen; i++)
{
if (desc[i] == 0x0D || desc[i] == 0x0A)
{
int lineLen = i - lineStart;
if (lineLen > 0)
{
strncpy(line, desc + lineStart, lineLen);
line[lineLen] = 0;
if (line[0] == '[' && strcmp(line + (lineLen - 4), "End]") == 0) // new node
{
line[lineLen - 5] = 0;
line++;
NSXMLNode *element = [NSXMLNode elementWithName:[NSString stringWithCString:line] children:elements attributes:nil];
[rootElement addChild:element];
elements = [NSMutableArray array];
}
else if (line[0] != '[')
{
NSXMLNode *element;
char* value;
value = index(line, '=');
if (value)
{
value[0] = 0;
value++;
element = [NSXMLNode elementWithName:[NSString stringWithCString:line] children:nil attributes:nil];
[element setStringValue:[NSString stringWithCString:value]];
}
else
{
element = [NSXMLNode elementWithName:@"Comment" children:nil attributes:nil];
[element setStringValue:[NSString stringWithCString:line]];
}
[elements addObject:element];
}
}
lineStart = i + 1;
}
}
}
if(tif) TIFFClose(tif);
xmlDocument = [[NSXMLDocument alloc] initWithRootElement:rootElement];
[rootElement release];
return xmlDocument;
}
void FVTIFFInitialize(void)
{
static int first_time=1;
if (! first_time) return; /* Been there. Done that. */
first_time = 0;
/* Grab the inherited method and install */
_TIFFParentExtender = TIFFSetTagExtender(_FVTIFFDefaultDirectory);
// TIFFSetWarningHandler((TIFFErrorHandler) FV_EMPTY_TIFFWarning); <- This is crashing in 64-bit !!!
}
static void
_FVTIFFDefaultDirectory(TIFF *tif)
{
/* Install the extended Tag field info */
TIFFMergeFieldInfo(tif, FVTiffFieldInfo, sizeof(FVTiffFieldInfo) / sizeof(FVTiffFieldInfo[0]));
/* Since an FVTIFF client module may have overridden
* the default directory method, we call it now to
* allow it to set up the rest of its own methods.
*/
if (_TIFFParentExtender)
(*_TIFFParentExtender)(tif);
}
void FV_EMPTY_TIFFWarning(const char *module, const char *fmt, ...){}