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
|
/*****************************************************************************/
// Copyright 2011 Adobe Systems Incorporated
// All Rights Reserved.
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in
// accordance with the terms of the Adobe license agreement accompanying it.
/*****************************************************************************/
/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_jpeg_image.cpp#1 $ */
/* $DateTime: 2012/05/30 13:28:51 $ */
/* $Change: 832332 $ */
/* $Author: tknoll $ */
/*****************************************************************************/
#include "dng_jpeg_image.h"
#include "dng_abort_sniffer.h"
#include "dng_area_task.h"
#include "dng_assertions.h"
#include "dng_host.h"
#include "dng_ifd.h"
#include "dng_image.h"
#include "dng_image_writer.h"
#include "dng_memory_stream.h"
#include "dng_mutex.h"
/*****************************************************************************/
dng_jpeg_image::dng_jpeg_image ()
: fImageSize ()
, fTileSize ()
, fUsesStrips (false)
, fJPEGTables ()
, fJPEGData ()
{
}
/*****************************************************************************/
class dng_jpeg_image_encode_task : public dng_area_task
{
private:
dng_host &fHost;
dng_image_writer &fWriter;
const dng_image &fImage;
dng_jpeg_image &fJPEGImage;
uint32 fTileCount;
const dng_ifd &fIFD;
dng_mutex fMutex;
uint32 fNextTileIndex;
public:
dng_jpeg_image_encode_task (dng_host &host,
dng_image_writer &writer,
const dng_image &image,
dng_jpeg_image &jpegImage,
uint32 tileCount,
const dng_ifd &ifd)
: fHost (host)
, fWriter (writer)
, fImage (image)
, fJPEGImage (jpegImage)
, fTileCount (tileCount)
, fIFD (ifd)
, fMutex ("dng_jpeg_image_encode_task")
, fNextTileIndex (0)
{
fMinTaskArea = 16 * 16;
fUnitCell = dng_point (16, 16);
fMaxTileSize = dng_point (16, 16);
}
void Process (uint32 /* threadIndex */,
const dng_rect & /* tile */,
dng_abort_sniffer *sniffer)
{
AutoPtr<dng_memory_block> compressedBuffer;
AutoPtr<dng_memory_block> uncompressedBuffer;
AutoPtr<dng_memory_block> subTileBlockBuffer;
AutoPtr<dng_memory_block> tempBuffer;
uint32 uncompressedSize = fIFD.fTileLength *
fIFD.fTileWidth *
fIFD.fSamplesPerPixel;
uncompressedBuffer.Reset (fHost.Allocate (uncompressedSize));
uint32 tilesAcross = fIFD.TilesAcross ();
while (true)
{
uint32 tileIndex;
{
dng_lock_mutex lock (&fMutex);
if (fNextTileIndex == fTileCount)
{
return;
}
tileIndex = fNextTileIndex++;
}
dng_abort_sniffer::SniffForAbort (sniffer);
uint32 rowIndex = tileIndex / tilesAcross;
uint32 colIndex = tileIndex % tilesAcross;
dng_rect tileArea = fIFD.TileArea (rowIndex, colIndex);
dng_memory_stream stream (fHost.Allocator ());
fWriter.WriteTile (fHost,
fIFD,
stream,
fImage,
tileArea,
1,
compressedBuffer,
uncompressedBuffer,
subTileBlockBuffer,
tempBuffer);
fJPEGImage.fJPEGData [tileIndex].Reset (stream.AsMemoryBlock (fHost.Allocator ()));
}
}
private:
// Hidden copy constructor and assignment operator.
dng_jpeg_image_encode_task (const dng_jpeg_image_encode_task &);
dng_jpeg_image_encode_task & operator= (const dng_jpeg_image_encode_task &);
};
/*****************************************************************************/
void dng_jpeg_image::Encode (dng_host &host,
const dng_negative &negative,
dng_image_writer &writer,
const dng_image &image)
{
#if qDNGValidate
dng_timer timer ("Encode JPEG Proxy time");
#endif
DNG_ASSERT (image.PixelType () == ttByte, "Cannot JPEG encode non-byte image");
fImageSize = image.Bounds ().Size ();
dng_ifd ifd;
ifd.fImageWidth = fImageSize.h;
ifd.fImageLength = fImageSize.v;
ifd.fSamplesPerPixel = image.Planes ();
ifd.fBitsPerSample [0] = 8;
ifd.fBitsPerSample [1] = 8;
ifd.fBitsPerSample [2] = 8;
ifd.fBitsPerSample [3] = 8;
ifd.fPhotometricInterpretation = piLinearRaw;
ifd.fCompression = ccLossyJPEG;
ifd.FindTileSize (512 * 512 * ifd.fSamplesPerPixel);
fTileSize.h = ifd.fTileWidth;
fTileSize.v = ifd.fTileLength;
// Need a higher quality for raw proxies than non-raw proxies,
// since users often perform much greater color changes. Also, use
// we are targeting a "large" size proxy (larger than 5MP pixels), or this
// is a full size proxy, then use a higher quality.
bool useHigherQuality = (uint64) ifd.fImageWidth *
(uint64) ifd.fImageLength > 5000000 ||
image.Bounds ().Size () == negative.OriginalDefaultFinalSize ();
if (negative.ColorimetricReference () == crSceneReferred)
{
ifd.fCompressionQuality = useHigherQuality ? 11 : 10;
}
else
{
ifd.fCompressionQuality = useHigherQuality ? 10 : 8;
}
uint32 tilesAcross = ifd.TilesAcross ();
uint32 tilesDown = ifd.TilesDown ();
uint32 tileCount = tilesAcross * tilesDown;
fJPEGData.Reset (new dng_jpeg_image_tile_ptr [tileCount]);
uint32 threadCount = Min_uint32 (tileCount,
host.PerformAreaTaskThreads ());
dng_jpeg_image_encode_task task (host,
writer,
image,
*this,
tileCount,
ifd);
host.PerformAreaTask (task,
dng_rect (0, 0, 16, 16 * threadCount));
}
/*****************************************************************************/
class dng_jpeg_image_find_digest_task : public dng_area_task
{
private:
const dng_jpeg_image &fJPEGImage;
uint32 fTileCount;
dng_fingerprint *fDigests;
dng_mutex fMutex;
uint32 fNextTileIndex;
public:
dng_jpeg_image_find_digest_task (const dng_jpeg_image &jpegImage,
uint32 tileCount,
dng_fingerprint *digests)
: fJPEGImage (jpegImage)
, fTileCount (tileCount)
, fDigests (digests)
, fMutex ("dng_jpeg_image_find_digest_task")
, fNextTileIndex (0)
{
fMinTaskArea = 16 * 16;
fUnitCell = dng_point (16, 16);
fMaxTileSize = dng_point (16, 16);
}
void Process (uint32 /* threadIndex */,
const dng_rect & /* tile */,
dng_abort_sniffer *sniffer)
{
while (true)
{
uint32 tileIndex;
{
dng_lock_mutex lock (&fMutex);
if (fNextTileIndex == fTileCount)
{
return;
}
tileIndex = fNextTileIndex++;
}
dng_abort_sniffer::SniffForAbort (sniffer);
dng_md5_printer printer;
printer.Process (fJPEGImage.fJPEGData [tileIndex]->Buffer (),
fJPEGImage.fJPEGData [tileIndex]->LogicalSize ());
fDigests [tileIndex] = printer.Result ();
}
}
private:
// Hidden copy constructor and assignment operator.
dng_jpeg_image_find_digest_task (const dng_jpeg_image_find_digest_task &);
dng_jpeg_image_find_digest_task & operator= (const dng_jpeg_image_find_digest_task &);
};
/*****************************************************************************/
dng_fingerprint dng_jpeg_image::FindDigest (dng_host &host) const
{
uint32 tileCount = TileCount ();
uint32 arrayCount = tileCount + (fJPEGTables.Get () ? 1 : 0);
AutoArray<dng_fingerprint> digests (new dng_fingerprint [arrayCount]);
// Compute digest of each compressed tile.
{
uint32 threadCount = Min_uint32 (tileCount,
host.PerformAreaTaskThreads ());
dng_jpeg_image_find_digest_task task (*this,
tileCount,
digests.Get ());
host.PerformAreaTask (task,
dng_rect (0, 0, 16, 16 * threadCount));
}
// Compute digest of JPEG tables, if any.
if (fJPEGTables.Get ())
{
dng_md5_printer printer;
printer.Process (fJPEGTables->Buffer (),
fJPEGTables->LogicalSize ());
digests [tileCount] = printer.Result ();
}
// Combine digests into a single digest.
{
dng_md5_printer printer;
for (uint32 k = 0; k < arrayCount; k++)
{
printer.Process (digests [k].data,
dng_fingerprint::kDNGFingerprintSize);
}
return printer.Result ();
}
}
/*****************************************************************************/
|