-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlaumemoryobject.cpp
398 lines (352 loc) · 16.4 KB
/
laumemoryobject.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*********************************************************************************
* *
* Copyright (c) 2017, Dr. Daniel L. Lau *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* 3. All advertising materials mentioning features or use of this software *
* must display the following acknowledgement: *
* This product includes software developed by the <organization>. *
* 4. Neither the name of the <organization> nor the *
* names of its contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY Dr. Daniel L. Lau ''AS IS'' AND ANY *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
* DISCLAIMED. IN NO EVENT SHALL Dr. Daniel L. Lau BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
*********************************************************************************/
#include "laumemoryobject.h"
#ifdef Q_PROCESSOR_ARM
void *_mm_malloc(int size, int align)
{
Q_UNUSED(align);
return (malloc(size));
}
void _mm_free(void *pointer)
{
free(pointer);
}
#endif
using namespace libtiff;
int LAUMemoryObjectData::instanceCounter = 0;
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObjectData::LAUMemoryObjectData()
{
numRows = 0;
numCols = 0;
numChns = 0;
numByts = 0;
numFrms = 0;
stepBytes = 0;
frameBytes = 0;
buffer = nullptr;
numBytesTotal = 0;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObjectData::LAUMemoryObjectData(unsigned int cols, unsigned int rows, unsigned int chns, unsigned int byts, unsigned int frms)
{
numRows = rows;
numCols = cols;
numChns = chns;
numByts = byts;
numFrms = frms;
buffer = nullptr;
stepBytes = 0;
frameBytes = 0;
numBytesTotal = 0;
allocateBuffer();
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObjectData::LAUMemoryObjectData(unsigned long long bytes)
{
numRows = bytes;
numCols = 1;
numChns = 1;
numByts = 1;
numFrms = 1;
buffer = nullptr;
stepBytes = 0;
frameBytes = 0;
numBytesTotal = 0;
allocateBuffer();
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObjectData::LAUMemoryObjectData(const LAUMemoryObjectData &other)
{
// COPY OVER THE SIZE PARAMETERS FROM THE SUPPLIED OBJECT
numRows = other.numRows;
numCols = other.numCols;
numChns = other.numChns;
numByts = other.numByts;
numFrms = other.numFrms;
// SET THESE VARIABLES TO ZERO AND LET THEM BE MODIFIED IN THE ALLOCATION METHOD
buffer = nullptr;
stepBytes = 0;
frameBytes = 0;
numBytesTotal = 0;
allocateBuffer();
if (buffer) {
memcpy(buffer, other.buffer, numBytesTotal);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObjectData::~LAUMemoryObjectData()
{
if (buffer != nullptr) {
instanceCounter = instanceCounter - 1;
qDebug() << QString("LAUMemoryObjectData::~LAUMemoryObjectData() %1").arg(instanceCounter) << numRows << numCols << numChns << numByts << numBytesTotal;
_mm_free(buffer);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
void LAUMemoryObjectData::allocateBuffer()
{
// ALLOCATE SPACE FOR HOLDING PIXEL DATA BASED ON NUMBER OF CHANNELS AND BYTES PER PIXEL
numBytesTotal = (unsigned long long)numRows;
numBytesTotal *= (unsigned long long)numCols;
numBytesTotal *= (unsigned long long)numChns;
numBytesTotal *= (unsigned long long)numByts;
numBytesTotal *= (unsigned long long)numFrms;
if (numBytesTotal > 0) {
instanceCounter = instanceCounter + 1;
qDebug() << QString("LAUMemoryObjectData::allocateBuffer() %1").arg(instanceCounter) << numRows << numCols << numChns << numByts << numFrms;
stepBytes = numCols * numChns * numByts;
frameBytes = numCols * numChns * numByts * numRows;
buffer = _mm_malloc(numBytesTotal + 128, 16);
if (buffer == nullptr) {
qDebug() << QString("LAUVideoBufferData::allocateBuffer() MAJOR ERROR DID NOT ALLOCATE SPACE!!!");
qDebug() << QString("LAUVideoBufferData::allocateBuffer() MAJOR ERROR DID NOT ALLOCATE SPACE!!!");
qDebug() << QString("LAUVideoBufferData::allocateBuffer() MAJOR ERROR DID NOT ALLOCATE SPACE!!!");
}
}
return;
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObject::LAUMemoryObject(QString filename) : data(new LAUMemoryObjectData()), transformMatrix(QMatrix4x4()), anchorPt(QPoint(-1, -1)), elapsedTime(0)
{
// GET A FILE TO OPEN FROM THE USER IF NOT ALREADY PROVIDED ONE
if (filename.isNull()) {
QSettings settings;
QString directory = settings.value("LAUCalTagDialog::lastUsedDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
filename = QFileDialog::getOpenFileName(nullptr, QString("Load image from disk"), directory, QString("*.tif *.tiff"));
if (filename.isEmpty() == false) {
settings.setValue("LAUCalTagDialog::lastUsedDirectory", QFileInfo(filename).absolutePath());
} else {
return;
}
}
// IF WE HAVE A VALID TIFF FILE, LOAD FROM DISK
// OTHERWISE TRY TO CONNECT TO SCANNER
if (QFile::exists(filename)) {
// OPEN INPUT TIFF FILE FROM DISK
TIFF *inTiff = TIFFOpen(filename.toLatin1(), "r");
if (!inTiff) {
return;
}
load(inTiff);
TIFFClose(inTiff);
}
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
LAUMemoryObject::LAUMemoryObject(TIFF *inTiff) : data(new LAUMemoryObjectData()), transformMatrix(QMatrix4x4()), anchorPt(QPoint(-1, -1)), elapsedTime(0)
{
load(inTiff);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUMemoryObject::save(QString filename)
{
if (filename.isNull()) {
QSettings settings;
QString directory = settings.value(QString("LAUMemoryObject::lastUsedDirectory"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).toString();
int counter = 0;
do {
if (counter == 0) {
filename = QString("%1/Untitled.tif").arg(directory);
} else {
filename = QString("%1/Untitled%2.tif").arg(directory).arg(counter);
}
counter++;
} while (QFile::exists(filename));
filename = QFileDialog::getSaveFileName(nullptr, QString("Save image to disk (*.tif)"), filename, QString("*.tif;*.tiff"));
if (!filename.isNull()) {
settings.setValue(QString("LAUMemoryObject::lastUsedDirectory"), QFileInfo(filename).absolutePath());
if (filename.toLower().endsWith(QString(".bmp"))) {
filename.chop(4);
} else if (filename.toLower().endsWith(QString(".jpg"))) {
filename.chop(4);
} else if (filename.toLower().endsWith(QString(".jpeg"))) {
filename.chop(5);
} else if (filename.toLower().endsWith(QString(".png"))) {
filename.chop(4);
}
if (!filename.toLower().endsWith(QString(".tiff"))) {
if (!filename.toLower().endsWith(QString(".tif"))) {
filename = QString("%1.tif").arg(filename);
}
}
} else {
return (false);
}
}
// OPEN TIFF FILE FOR SAVING THE IMAGE
TIFF *outputTiff = TIFFOpen(filename.toLatin1(), "w");
if (!outputTiff) {
return (false);
}
// WRITE IMAGE TO CURRENT DIRECTORY
if (save(outputTiff)) {
// CLOSE TIFF FILE
TIFFClose(outputTiff);
return (true);
}
// CLOSE TIFF FILE
TIFFClose(outputTiff);
return (false);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUMemoryObject::save(TIFF *otTiff, int index)
{
// WRITE FORMAT PARAMETERS TO CURRENT TIFF DIRECTORY
TIFFSetField(otTiff, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(otTiff, TIFFTAG_IMAGEWIDTH, (unsigned long)width());
TIFFSetField(otTiff, TIFFTAG_IMAGELENGTH, (unsigned long)(height()*frames()));
TIFFSetField(otTiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
TIFFSetField(otTiff, TIFFTAG_XRESOLUTION, 72.0);
TIFFSetField(otTiff, TIFFTAG_YRESOLUTION, 72.0);
TIFFSetField(otTiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(otTiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(otTiff, TIFFTAG_SAMPLESPERPIXEL, (unsigned short)colors());
TIFFSetField(otTiff, TIFFTAG_BITSPERSAMPLE, (unsigned short)(8 * depth()));
if (colors() == 3) {
TIFFSetField(otTiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
} else {
TIFFSetField(otTiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
}
TIFFSetField(otTiff, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
TIFFSetField(otTiff, TIFFTAG_XPOSITION, qMax(0.0f, (float)anchorPt.x()));
TIFFSetField(otTiff, TIFFTAG_YPOSITION, qMax(0.0f, (float)anchorPt.y()));
TIFFSetField(otTiff, TIFFTAG_PREDICTOR, 2);
TIFFSetField(otTiff, TIFFTAG_ROWSPERSTRIP, 1);
if (depth() == sizeof(float)) {
// SEE IF WE HAVE TO TELL THE TIFF READER THAT WE ARE STORING
// PIXELS IN 32-BIT FLOATING POINT FORMAT
TIFFSetField(otTiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
}
// MAKE TEMPORARY BUFFER TO HOLD CURRENT ROW BECAUSE COMPRESSION DESTROYS
// WHATS EVER INSIDE THE BUFFER
unsigned char *tempBuffer = (unsigned char *)malloc(step());
for (unsigned int row = 0; row < height()*frames(); row++) {
memcpy(tempBuffer, constScanLine(row), step());
TIFFWriteScanline(otTiff, tempBuffer, row, 0);
}
free(tempBuffer);
// WRITE THE CURRENT DIRECTORY AND PREPARE FOR THE NEW ONE
TIFFWriteDirectory(otTiff);
// WRITE THE ELAPSED TIME STAMP TO AN EXIF TAG
uint64 dir_offset;
TIFFCreateEXIFDirectory(otTiff);
TIFFSetField(otTiff, EXIFTAG_SUBSECTIME, QString("%1").arg(elapsed()).toLatin1().data());
TIFFWriteCustomDirectory(otTiff, &dir_offset);
TIFFSetDirectory(otTiff, index);
TIFFSetField(otTiff, TIFFTAG_EXIFIFD, dir_offset);
TIFFRewriteDirectory(otTiff);
return (true);
}
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
bool LAUMemoryObject::load(TIFF *inTiff)
{
// LOAD INPUT TIFF FILE PARAMETERS IMPORTANT TO RESAMPLING THE IMAGE
unsigned long uLongVariable;
unsigned short uShortVariable;
// NUMBER OF FRAMES IS ALWAYS EQUAL TO ONE
data->numFrms = 1;
// GET THE HEIGHT AND WIDTH OF INPUT IMAGE IN PIXELS
TIFFGetField(inTiff, TIFFTAG_IMAGEWIDTH, &uLongVariable);
data->numCols = uLongVariable;
TIFFGetField(inTiff, TIFFTAG_IMAGELENGTH, &uLongVariable);
data->numRows = uLongVariable;
TIFFGetField(inTiff, TIFFTAG_SAMPLESPERPIXEL, &uShortVariable);
data->numChns = uShortVariable;
TIFFGetField(inTiff, TIFFTAG_BITSPERSAMPLE, &uShortVariable);
data->numByts = uShortVariable / 8;
// READ AND CHECK THE PHOTOMETRIC INTERPRETATION FIELD AND MAKE SURE ITS WHAT WE EXPECT
TIFFGetField(inTiff, TIFFTAG_PHOTOMETRIC, &uShortVariable);
if (uShortVariable != PHOTOMETRIC_SEPARATED && uShortVariable != PHOTOMETRIC_MINISBLACK) {
return (false);
}
// LOAD THE ANCHOR POINT
float xPos = -1.0f, yPos = -1.0f;
TIFFGetField(inTiff, TIFFTAG_XPOSITION, &xPos);
TIFFGetField(inTiff, TIFFTAG_YPOSITION, &yPos);
anchorPt = QPoint(qRound(xPos), qRound(yPos));
// ALLOCATE SPACE TO HOLD IMAGE DATA
data->allocateBuffer();
// READ DATA AS EITHER CHUNKY OR PLANAR FORMAT
if (data->buffer) {
short shortVariable;
TIFFGetField(inTiff, TIFFTAG_PLANARCONFIG, &shortVariable);
if (shortVariable == PLANARCONFIG_SEPARATE) {
unsigned char *tempBuffer = new unsigned char [step()];
for (unsigned int chn = 0; chn < colors(); chn++) {
for (unsigned int row = 0; row < height(); row++) {
unsigned char *pBuffer = scanLine(row);
TIFFReadScanline(inTiff, tempBuffer, (int)row, (int)chn);
for (unsigned int col = 0; col < width(); col++) {
((float *)pBuffer)[col * colors() + chn] = ((float *)tempBuffer)[col];
}
}
}
delete [] tempBuffer;
} else if (shortVariable == PLANARCONFIG_CONTIG) {
for (unsigned int row = 0; row < height(); row++) {
TIFFReadScanline(inTiff, (unsigned char *)scanLine(row), (int)row);
}
}
}
// GET THE ELAPSED TIME VALUE FROM THE EXIF TAG FOR SUBSECOND TIME
uint64 directoryOffset;
if (TIFFGetField(inTiff, TIFFTAG_EXIFIFD, &directoryOffset)) {
char *byteArray;
TIFFReadEXIFDirectory(inTiff, directoryOffset);
if (TIFFGetField(inTiff, EXIFTAG_SUBSECTIME, &byteArray)) {
setElapsed(QString(QByteArray(byteArray)).toInt());
}
}
return (true);
}