-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPRTC.cpp
235 lines (197 loc) · 7.12 KB
/
PRTC.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <jni.h> // JNI header provided by JDK
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <omp.h>
#include <fstream>
#include "parallelreadtiff.h"
#include "helperfunctions.h"
#include "edu_abc_berkeley_PRTC.h"
JNIEXPORT void JNICALL Java_edu_abc_berkeley_PRTC_parallelReadTiffC (JNIEnv *env, jobject thisObj, jstring fileName, jobject tiffJ){
const char* fName = env->GetStringUTFChars(fileName, NULL);
void* tiff = env->GetDirectBufferAddress(tiffJ);
if(!tiff){
printf("NULLPTR RECIEVED FROM JAVA\n");
return;
}
//((uint8_t*)tiff)[0] = 1;
readTiffParallelWrapperSet(fName,tiff);
}
JNIEXPORT jobjectArray JNICALL Java_edu_abc_berkeley_PRTC_parallelReadTiffUINT8 (JNIEnv *env, jobject thisObj, jstring fileName){
const char* fName = env->GetStringUTFChars(fileName, NULL);
uint8_t* arrP = (uint8_t*)readTiffParallelWrapperNoXYFlip(fName);
uint64_t* arrDims = getImageSize(fName);
if(isImageJIm(fName)){
uint64_t tempZ = imageJImGetZ(fName);
if(tempZ) arrDims[2] = tempZ;
}
env->ReleaseStringUTFChars(fileName, fName);
uint64_t zSize = arrDims[0]*arrDims[1];
// Get the char array class
jclass cls = env->FindClass("[B");
jbyteArray iniVal = env->NewByteArray(arrDims[2]);
// Main Array to be returned
jobjectArray outer = env->NewObjectArray(arrDims[2], cls, iniVal);
for(uint64_t i = 0; i < arrDims[2]; i++){
jbyteArray inner = env->NewByteArray(zSize);
if(zSize <= INT_MAX/2){
env->SetByteArrayRegion(inner, 0, zSize, (int8_t*)arrP+(i*zSize));
}
else{
//ImageJ can only do half of INTMAX into a region at a time maybe?
int32_t batchSize = (zSize-1)/3+1;
int32_t cSize = batchSize;
for(int j = 0; j < 3; j++){
if((j+1)*batchSize > zSize) cSize = zSize-(j*batchSize);
env->SetByteArrayRegion(inner, j*batchSize, cSize, (int8_t*)arrP+(i*zSize)+(j*batchSize));
}
}
env->SetObjectArrayElement(outer, i, inner);
env->DeleteLocalRef(inner);
}
free(arrP);
free(arrDims);
return outer;
}
JNIEXPORT jobjectArray JNICALL Java_edu_abc_berkeley_PRTC_parallelReadTiffUINT16 (JNIEnv *env, jobject thisObj, jstring fileName){
const char* fName = env->GetStringUTFChars(fileName, NULL);
uint16_t* arrP = (uint16_t*)readTiffParallelWrapperNoXYFlip(fName);
uint64_t* arrDims = getImageSize(fName);
if(isImageJIm(fName)){
uint64_t tempZ = imageJImGetZ(fName);
if(tempZ) arrDims[2] = tempZ;
}
env->ReleaseStringUTFChars(fileName, fName);
uint64_t zSize = arrDims[0]*arrDims[1];
// Get the short array class
jclass cls = env->FindClass("[S");
jshortArray iniVal = env->NewShortArray(arrDims[2]);
// Main Array to be returned
jobjectArray outer = env->NewObjectArray(arrDims[2], cls, iniVal);
env->DeleteLocalRef(iniVal);
env->DeleteLocalRef(cls);
for(uint64_t i = 0; i < arrDims[2]; i++){
jshortArray inner = env->NewShortArray(zSize);
if(zSize <= INT_MAX/2){
env->SetShortArrayRegion(inner, 0, zSize, (int16_t*)arrP+(i*zSize));
}
else{
//ImageJ can only do half of INTMAX into a region at a time maybe?
int32_t batchSize = (zSize-1)/3+1;
int32_t cSize = batchSize;
for(int j = 0; j < 3; j++){
if((j+1)*batchSize > zSize) cSize = zSize-(j*batchSize);
env->SetShortArrayRegion(inner, j*batchSize, cSize, (int16_t*)arrP+(i*zSize)+(j*batchSize));
}
}
env->SetObjectArrayElement(outer, i, inner);
env->DeleteLocalRef(inner);
}
free(arrP);
free(arrDims);
return outer;
}
JNIEXPORT jobjectArray JNICALL Java_edu_abc_berkeley_PRTC_parallelReadTiffFLOAT (JNIEnv *env, jobject thisObj, jstring fileName){
const char* fName = env->GetStringUTFChars(fileName, NULL);
float* arrP = (float*)readTiffParallelWrapperNoXYFlip(fName);
uint64_t* arrDims = getImageSize(fName);
if(isImageJIm(fName)){
uint64_t tempZ = imageJImGetZ(fName);
if(tempZ) arrDims[2] = tempZ;
}
env->ReleaseStringUTFChars(fileName, fName);
uint64_t zSize = arrDims[0]*arrDims[1];
// Get the float array class
jclass cls = env->FindClass("[F");
jfloatArray iniVal = env->NewFloatArray(arrDims[2]);
// Main Array to be returned
jobjectArray outer = env->NewObjectArray(arrDims[2], cls, iniVal);
for(uint64_t i = 0; i < arrDims[2]; i++){
jfloatArray inner = env->NewFloatArray(zSize);
if(zSize <= INT_MAX/2){
env->SetFloatArrayRegion(inner, 0, zSize, arrP+(i*zSize));
}
else{
//ImageJ can only do half of INTMAX into a region at a time maybe?
int32_t batchSize = (zSize-1)/3+1;
int32_t cSize = batchSize;
for(int j = 0; j < 3; j++){
if((j+1)*batchSize > zSize) cSize = zSize-(j*batchSize);
env->SetFloatArrayRegion(inner, j*batchSize, cSize, arrP+(i*zSize)+(j*batchSize));
}
}
env->SetObjectArrayElement(outer, i, inner);
env->DeleteLocalRef(inner);
}
free(arrP);
free(arrDims);
return outer;
}
// Image stack does not accept double so we convert to a float for now
JNIEXPORT jobjectArray JNICALL Java_edu_abc_berkeley_PRTC_parallelReadTiffDOUBLE (JNIEnv *env, jobject thisObj, jstring fileName){
const char* fName = env->GetStringUTFChars(fileName, NULL);
double* arrPT = (double*)readTiffParallelWrapperNoXYFlip(fName);
uint64_t* arrDims = getImageSize(fName);
if(isImageJIm(fName)){
uint64_t tempZ = imageJImGetZ(fName);
if(tempZ) arrDims[2] = tempZ;
}
env->ReleaseStringUTFChars(fileName, fName);
uint64_t zSize = arrDims[0]*arrDims[1];
uint64_t arrSize = zSize*arrDims[2];
// convert the double array to float
float* arrP = (float*)malloc(arrSize*sizeof(float));
#pragma omp parallel for
for(uint64_t i = 0; i < arrSize; i++){
arrP[i] = (float)arrPT[i];
}
free(arrPT);
// Get the float array class
jclass cls = env->FindClass("[F");
jfloatArray iniVal = env->NewFloatArray(arrDims[2]);
// Main Array to be returned
jobjectArray outer = env->NewObjectArray(arrDims[2], cls, iniVal);
for(uint64_t i = 0; i < arrDims[2]; i++){
jfloatArray inner = env->NewFloatArray(zSize);
if(zSize <= INT_MAX/2){
env->SetFloatArrayRegion(inner, 0, zSize, arrP+(i*zSize));
}
else{
//ImageJ can only do half of INTMAX into a region at a time maybe?
int32_t batchSize = (zSize-1)/3+1;
int32_t cSize = batchSize;
for(int j = 0; j < 3; j++){
if((j+1)*batchSize > zSize) cSize = zSize-(j*batchSize);
env->SetFloatArrayRegion(inner, j*batchSize, cSize, arrP+(i*zSize)+(j*batchSize));
}
}
env->SetObjectArrayElement(outer, i, inner);
env->DeleteLocalRef(inner);
}
free(arrP);
free(arrDims);
return outer;
}
JNIEXPORT jlong JNICALL Java_edu_abc_berkeley_PRTC_getDataType (JNIEnv *env, jobject thisObj,jstring fileName)
{
const char* fName = env->GetStringUTFChars(fileName, NULL);
uint64_t bits = getDataType(fName);
env->ReleaseStringUTFChars(fileName, fName);
return bits;
}
JNIEXPORT jlongArray JNICALL Java_edu_abc_berkeley_PRTC_getImageDims (JNIEnv *env, jobject thisObj, jstring fileName)
{
const char* fName = env->GetStringUTFChars(fileName, NULL);
uint64_t* dims = getImageSize(fName);
env->ReleaseStringUTFChars(fileName, fName);
jlongArray rval = env->NewLongArray(3);
#ifdef __APPLE__
env->SetLongArrayRegion(rval,0,3,(long int*)dims);
#else
env->SetLongArrayRegion(rval,0,3,(int64_t*)dims);
#endif
free(dims);
return rval;
}