-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathImage.m
More file actions
206 lines (183 loc) · 5.03 KB
/
Copy pathImage.m
File metadata and controls
206 lines (183 loc) · 5.03 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// Image.m
// RMi
//
// Created by Marcelo da Mata on 06/02/2013.
// Copyright (c) 2013 Marcelo da Mata. All rights reserved.
//
#import "Image.h"
@implementation Image
@synthesize dd = dd;
@synthesize fi = fi;
-(id)init:(DicomDecoder*) decoder{
dd = decoder;
fi = [dd getFileInfo];
width = [fi getWidth];
height = [fi getHeight];
skipCount = [fi getOffset];
[self loadPixels];
[self create8BitImage];
return self;
}
-(void)loadPixels {
char* bytes = [dd getBytes];
location = [dd getPosition];
switch ([fi getType]) {
case 1://case GRAY16_SIGNED
case 2://case GRAY16_UNSIGNED
bytesPerPixel = 2;
[self skip:bytes];
[self read16biImage:bytes];
break;
default:
break;
}
}
-(void)skip:(char *)bytes {
byteCount = ((long long)width)*height*bytesPerPixel;
if([fi getType]==BITMAP) {
int scan = width/8, pad = width%8;
if (pad>0) {
scan++;
}
byteCount = scan*height;
}
nPixels = width*height;
bufferSize = (int)(byteCount/25L);
if (bufferSize<8192) {
bufferSize = 8192;
} else {
bufferSize = (bufferSize/8192)*8192;
}
location = skipCount;
}
-(void)read16biImage:(char *)bytes {
/*
if(fi->compression>COMPRESSION_NONE || (fi->stripOffsets!=nil && fi->stripOffsets->lenght>1)) {
return [self readCompressed16bitImage?bytes];
}
se for ler alguma imagem comprimida deve implementar esse if que se encontra no arquivo ImageReader.java na linha 102.
*/
int pixelRead;
unsigned char buffer[bufferSize];
pixels = malloc(sizeof(short)*nPixels);
long long totalRead = 0L;
int base = 0;
int count;
//int value;
int bufferCount;
while (totalRead<byteCount) {
if ((totalRead+bufferSize)>byteCount) {
bufferSize = (int)(byteCount-totalRead);
}
bufferCount = 0;
while (bufferCount<bufferSize) {//preenche o buffer
count = [self read:buffer :bufferSize-bufferCount :bytes];
if (count==0) {
if (bufferCount>0) {
for (int i=bufferCount; i<bufferSize; i++) {
buffer[i] = 0;
}
}
totalRead = bufferCount;
//erro de fim de arquivo se for preciso ver arquivo ImageReader.java na linha 122.
break;
}
bufferCount += count;
}
totalRead += bufferSize;
//show progress aqui, ImageReader.java linha 128.
pixelRead = bufferSize/bytesPerPixel;
if ([fi getIntelByteOrder]) {
if ([fi getType]==GRAY16_SIGNED) {
for (int i=base,j=0; i<(base+pixelRead); i++,j+=2) {
pixels[i] = (short)((((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff))+32768);
}
} else {
for (int i=base,j=0; i<(base+pixelRead); i++,j+=2) {
pixels[i] = (short)((((buffer[j+1]&0xff)<<8) | (buffer[j]&0xff)));
}
}
} else {
if ([fi getType]==GRAY16_SIGNED) {
for (int i=base,j=0; i<(base+pixelRead); i++,j+=2) {
pixels[i] = (short)((((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff))+32768);
}
} else {
for (int i=base,j=0; i<(base+pixelRead); i++,j+=2) {
pixels[i] = (short)((((buffer[j]&0xff)<<8) | (buffer[j+1]&0xff)));
}
}
}
base += pixelRead;
}
}
-(int)read:(unsigned char *)buffer :(int)len :(char*)bytesFile {
int numBytes = 0;
int pos = 0;
while (location < [dd getBufferLenght] && pos < bufferSize) {
buffer[pos] = bytesFile[location];
location++;
pos++;
numBytes++;
}
return numBytes;
}
-(char*)create8BitImage {
int size = width*height;
pixels8 = malloc(size * sizeof(char));
int value;
int min = [self getMin], max = [self getMax];
double scale = 256.0/(max-min+1);
for (int i=0; i<size; i++) {
value = (pixels[i]&0xffff)-min;
if (value<0) {
value = 0;
}
value = (int)(value*scale+0.5);
if (value>255) {
value = 255;
}
pixels8[i] = (char)value;
}
numPixels = size;
return pixels8;
}
-(int)getMax {
if(!minMaxSet) {
[self findMinMax];
}
return maxPixel;
}
-(int)getMin {
if(!minMaxSet) {
[self findMinMax];
}
return minPixel;
}
-(void)findMinMax {
int size = width*height;
int value;
minPixel = 65535;
maxPixel = 0;
for (int i=0; i<size; i++) {
value = pixels[i]&0xffff;
if (value<minPixel) {
minPixel = value;
}
if (value > maxPixel) {
maxPixel = value;
}
}
minMaxSet = true;
}
-(int)getHeight {
return height;
}
-(int)getWidth {
return width;
}
-(char*)getPixels8 {
return pixels8;
}
@end