~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to libs/dimg/loaders/rawloader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2010-04-09 21:30:01 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100409213001-4bfyibrd359rn7o3
Tags: 2:1.2.0-0ubuntu1
* New upstream release (LP: #560576)
* Remove all patches, fixed upstream
  - Remove quilt build-depend

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 * Description : A digital camera RAW files loader for DImg
8
8
 *               framework using an external dcraw instance.
9
9
 *
10
 
 * Copyright (C) 2005-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
11
 
 * Copyright (C) 2005-2008 by Marcel Wiesweg <marcel.wiesweg@gmx.de>
 
10
 * Copyright (C) 2005-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
 
11
 * Copyright (C) 2005-2010 by Marcel Wiesweg <marcel.wiesweg@gmx.de>
12
12
 *
13
13
 * This program is free software; you can redistribute it
14
14
 * and/or modify it under the terms of the GNU General
23
23
 *
24
24
 * ============================================================ */
25
25
 
26
 
 
27
26
#include "rawloader.moc"
28
27
 
29
28
// C++ includes
45
44
#include "dimgloaderobserver.h"
46
45
#include "icctransform.h"
47
46
#include "imagehistogram.h"
48
 
#include "imagecurves.h"
49
47
#include "imagelevels.h"
50
 
#include "bcgmodifier.h"
51
 
#include "whitebalance.h"
 
48
#include "curvesfilter.h"
 
49
#include "bcgfilter.h"
 
50
#include "wbfilter.h"
52
51
#include "globals.h"
53
52
 
54
53
namespace Digikam
62
61
    m_observer            = 0;
63
62
}
64
63
 
65
 
bool RAWLoader::load(const QString& filePath, DImgLoaderObserver *observer)
 
64
bool RAWLoader::load(const QString& filePath, DImgLoaderObserver* observer)
66
65
{
67
66
    m_observer = observer;
68
67
 
130
129
        m_observer->progressInfo(m_image, value);
131
130
}
132
131
 
133
 
#if KDCRAW_VERSION < 0x000400
134
 
bool RAWLoader::checkToCancelReceivingData()
135
 
{
136
 
    return (m_observer ? m_observer->isShuttingDown() : false);
137
 
}
138
 
 
139
 
void RAWLoader::setReceivingDataProgress(double value)
140
 
{
141
 
    if (m_observer)
142
 
        m_observer->progressInfo(m_image, value);
143
 
}
144
 
#endif
145
 
 
146
132
bool RAWLoader::loadedFromDcraw(QByteArray data, int width, int height, int rgbmax,
147
 
                                DImgLoaderObserver *observer)
 
133
                                DImgLoaderObserver* observer)
148
134
{
149
135
    int checkpoint = 0;
150
136
 
178
164
            {
179
165
                if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)     // Intel
180
166
                {
181
 
                    #if KDCRAW_VERSION < 0x000400
182
 
                    dst[0] = (unsigned short)((src[4]*256 + src[5]) * fac);      // Blue
183
 
                    dst[1] = (unsigned short)((src[2]*256 + src[3]) * fac);      // Green
184
 
                    dst[2] = (unsigned short)((src[0]*256 + src[1]) * fac);      // Red
185
 
                    #else
186
167
                    dst[0] = (unsigned short)((src[5]*256 + src[4]) * fac);      // Blue
187
168
                    dst[1] = (unsigned short)((src[3]*256 + src[2]) * fac);      // Green
188
169
                    dst[2] = (unsigned short)((src[1]*256 + src[0]) * fac);      // Red
189
 
                    #endif
190
170
                }
191
171
                else
192
172
                {
201
181
            }
202
182
        }
203
183
 
204
 
#if KDCRAW_VERSION < 0x000400
205
 
        // ----------------------------------------------------------
206
 
 
207
 
        // Special case: if Color Management is not used here, output color space is in sRGB* color space
208
 
        // RAW decoded image is a linear-histogram image with 16 bits color depth.
209
 
        // No auto white balance and no gamma adjustments are performed. Image is a black hole.
210
 
        // We need to reproduce all dcraw 8 bits color depth adjustments here.
211
 
 
212
 
        if (m_rawDecodingSettings.outputColorSpace != DRawDecoding::RAWCOLOR)
213
 
        {
214
 
            ImageHistogram histogram(image, width, height, true);
215
 
            histogram.calculate();
216
 
 
217
 
            int perc, val, total;
218
 
            float white=0.0, r, gamma=2.222222;
219
 
            unsigned short lut[65536];
220
 
 
221
 
            // Search 99th percentile white level.
222
 
 
223
 
            perc = (int)(width * height * 0.01);
224
 
            kDebug() << "White Level: " << perc;
225
 
            for (int c = 1 ; c < 4 ; ++c)
226
 
            {
227
 
                total = 0;
228
 
                for (val = 65535 ; val > 256 ; --val)
229
 
                    if ((total += (int)histogram.getValue(c, val)) > perc)
230
 
                        break;
231
 
 
232
 
                if (white < val) white = (float)val;
233
 
            }
234
 
 
235
 
            white *= 1.0 / m_rawDecodingSettings.brightness;
236
 
 
237
 
            kDebug() << "White Point: " << white;
238
 
 
239
 
            // Compute the Gamma lut accordingly.
240
 
 
241
 
            for (int i=0; i < 65536; ++i)
242
 
            {
243
 
                r = i / white;
244
 
                val = (int)(65536.0 * (r <= 0.018 ? r*4.5 : pow(r, 1.0/gamma) * 1.099-0.099));
245
 
                if (val > 65535) val = 65535;
246
 
                lut[i] = val;
247
 
            }
248
 
 
249
 
            //  Apply Gamma lut to the whole image.
250
 
 
251
 
            unsigned short *im = (unsigned short *)image;
252
 
            for (int i = 0; i < width*height; ++i)
253
 
            {
254
 
                im[0] = lut[im[0]];      // Blue
255
 
                im[1] = lut[im[1]];      // Green
256
 
                im[2] = lut[im[2]];      // Red
257
 
                im += 4;
258
 
            }
259
 
        }
260
 
#endif
261
184
        // ----------------------------------------------------------
262
185
 
263
186
        imageData() = (uchar *)image;
359
282
    return true;
360
283
}
361
284
 
362
 
void RAWLoader::postProcess(DImgLoaderObserver *observer)
 
285
void RAWLoader::postProcess(DImgLoaderObserver* observer)
363
286
{
364
287
    // emulate LibRaw custom output profile
365
288
    if (!m_customOutputProfile.isNull())
378
301
 
379
302
    if (m_customRawSettings.exposureComp != 0.0 || m_customRawSettings.saturation != 1.0)
380
303
    {
381
 
        WhiteBalance wb(m_rawDecodingSettings.sixteenBitsImage);
382
 
        wb.whiteBalance(imageData(), imageWidth(), imageHeight(), m_rawDecodingSettings.sixteenBitsImage,
383
 
                        0.0,                                // black
384
 
                        m_customRawSettings.exposureComp,   // exposure
385
 
                        6500.0,                             // temperature (neutral)
386
 
                        1.0,                                // green
387
 
                        0.5,                                // dark
388
 
                        1.0,                                // gamma
389
 
                        m_customRawSettings.saturation);    // saturation
 
304
        WBContainer settings;
 
305
        settings.temperature  = 6500.0;
 
306
        settings.dark         = 0.5;
 
307
        settings.black        = 0.0;
 
308
        settings.exposition   = m_customRawSettings.exposureComp;
 
309
        settings.gamma        = 1.0;
 
310
        settings.saturation   = m_customRawSettings.saturation;
 
311
        settings.green        = 1.0;
 
312
        WBFilter wb(m_image, 0L, settings);
 
313
        wb.startFilterDirectly();
 
314
        m_image->putImageData(wb.getTargetImage().bits());       
390
315
    }
 
316
    
391
317
    if (observer) observer->progressInfo(m_image, 0.92F);
392
318
 
393
319
    if (m_customRawSettings.lightness != 0.0 ||
394
320
        m_customRawSettings.contrast  != 1.0 ||
395
321
        m_customRawSettings.gamma     != 1.0)
396
322
    {
397
 
        BCGModifier bcg;
398
 
        bcg.setBrightness(m_customRawSettings.lightness);
399
 
        bcg.setContrast(m_customRawSettings.contrast);
400
 
        bcg.setGamma(m_customRawSettings.gamma);
401
 
        bcg.applyBCG(imageData(), imageWidth(), imageHeight(), m_rawDecodingSettings.sixteenBitsImage);
 
323
        BCGContainer settings;
 
324
        settings.brightness = m_customRawSettings.lightness;
 
325
        settings.contrast   = m_customRawSettings.contrast;
 
326
        settings.gamma      = m_customRawSettings.gamma;
 
327
        BCGFilter bcg(m_image, 0L, settings);
 
328
        bcg.startFilterDirectly();
 
329
        m_image->putImageData(bcg.getTargetImage().bits());        
402
330
    }
 
331
    
403
332
    if (observer) observer->progressInfo(m_image, 0.94F);
404
333
 
405
334
    if (!m_customRawSettings.curveAdjust.isEmpty())
406
335
    {
407
 
        DImg tmp(imageWidth(), imageHeight(), m_rawDecodingSettings.sixteenBitsImage);
408
 
        ImageCurves curves(m_rawDecodingSettings.sixteenBitsImage);
409
 
        curves.setCurvePoints(LuminosityChannel, m_customRawSettings.curveAdjust);
410
 
        curves.curvesCalculateCurve(LuminosityChannel);
411
 
        curves.curvesLutSetup(AlphaChannel);
412
 
        curves.curvesLutProcess(imageData(), tmp.bits(), imageWidth(), imageHeight());
413
 
        memcpy(imageData(), tmp.bits(), tmp.numBytes());
 
336
        CurvesContainer settings;
 
337
        settings.curvesType   = ImageCurves::CURVE_SMOOTH;
 
338
        settings.lumCurveVals = m_customRawSettings.curveAdjust;
 
339
        CurvesFilter curves(m_image, 0L, settings);
 
340
        curves.startFilterDirectly();
 
341
        m_image->putImageData(curves.getTargetImage().bits());
414
342
    }
 
343
    
415
344
    if (observer) observer->progressInfo(m_image, 0.96F);
416
345
 
417
346
    if (!m_customRawSettings.levelsAdjust.isEmpty())
431
360
        levels.levelsLutProcess(imageData(), tmp.bits(), imageWidth(), imageHeight());
432
361
        memcpy(imageData(), tmp.bits(), tmp.numBytes());
433
362
    }
 
363
    
434
364
    if (observer) observer->progressInfo(m_image, 0.98F);
435
365
}
436
366