~ubuntu-branches/ubuntu/intrepid/digikam/intrepid

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-07-17 20:25:39 UTC
  • mfrom: (1.3.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20080717202539-ttj4and3ds1ldgn1
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

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-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
11
 
 * Copyright (C) 2005-2007 by Marcel Wiesweg <marcel.wiesweg@gmx.de>
 
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>
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
// C++ includes.
 
27
 
 
28
#include <cmath>
 
29
 
26
30
// QT includes.
27
31
 
28
32
#include <qcstring.h>
29
33
 
 
34
// KDE includes.
 
35
 
 
36
#include <kstandarddirs.h>
 
37
 
30
38
// Local includes.
31
39
 
32
40
#include "ddebug.h"
 
41
#include "imagehistogram.h"
33
42
#include "dimg.h"
34
43
#include "dimgloaderobserver.h"
35
44
#include "rawloader.h"
57
66
    // I hope when porting to Qt4, all the event loop stuff (and this problem) can be removed.
58
67
    if (imageGetAttribute("noeventloop").isValid())
59
68
        return false;
60
 
    
 
69
 
61
70
    readMetadata(filePath, DImg::RAW);
62
 
  
 
71
 
63
72
    // NOTE: Here, we don't check a possible embedded work-space color profile using 
64
73
    // the method checkExifWorkingColorSpace() like with JPEG, PNG, and TIFF loaders, 
65
74
    // because RAW file are always in linear mode.
66
 
    
 
75
 
67
76
    int width, height, rgbmax;
68
77
    QByteArray data;
69
78
    if (!KDcrawIface::KDcraw::decodeRAWImage(filePath, m_rawDecodingSettings, 
133
142
            }
134
143
        }
135
144
 
 
145
        // ----------------------------------------------------------
 
146
 
 
147
        // Special case : if Color Management is not used here, output color space is in sRGB* color space
 
148
        // RAW decoded image is a linear-histogram image with 16 bits color depth. 
 
149
        // No auto white balance and no gamma adjustemnts are performed. Image is a black hole.
 
150
        // We need to reproduce all dcraw 8 bits color depth adjustements here.
 
151
 
 
152
        if (m_rawDecodingSettings.outputColorSpace != KDcrawIface::RawDecodingSettings::RAWCOLOR)
 
153
        {
 
154
            ImageHistogram histogram(image, width, height, true);
 
155
 
 
156
            int perc, val, total;
 
157
            float white=0.0, r;
 
158
            unsigned short lut[65536];
 
159
 
 
160
            // Search 99th percentile white level.
 
161
 
 
162
            perc = (int)(width * height * 0.01);
 
163
            DDebug() << "White Level: " << perc << endl;
 
164
            for (int c = 1 ; c < 4 ; c++)
 
165
            {
 
166
                total = 0;
 
167
                for (val = 65535 ; val > 256 ; --val)
 
168
                    if ((total += (int)histogram.getValue(c, val)) > perc) 
 
169
                        break;
 
170
 
 
171
                if (white < val) white = (float)val;
 
172
            }
 
173
            DDebug() << "White Point: " << white << endl;
 
174
 
 
175
            // Compute the Gamma lut accordingly.
 
176
 
 
177
            for (int i=0; i < 65536; i++) 
 
178
            {
 
179
                r = i / white;
 
180
                val = (int)(65536.0 * (r <= 0.018 ? r*4.5 : pow(r,0.45)*1.099-0.099));
 
181
                if (val > 65535) val = 65535;
 
182
                lut[i] = val;
 
183
            }
 
184
 
 
185
            //  Apply Gamma lut to the whole image.
 
186
 
 
187
            unsigned short *im = (unsigned short *)image;
 
188
            for (int i = 0; i < width*height; i++)
 
189
            {
 
190
                im[0] = lut[im[0]];      // Blue
 
191
                im[1] = lut[im[1]];      // Green
 
192
                im[2] = lut[im[2]];      // Red
 
193
                im += 4;
 
194
            }
 
195
        }
 
196
 
 
197
        // ----------------------------------------------------------
 
198
 
136
199
        imageData() = (uchar *)image;
137
200
    }
138
201
    else        // 8 bits image
170
233
            }
171
234
        }
172
235
 
 
236
        // NOTE: if Color Management is not used here, output color space is in sRGB* color space.
 
237
        // Gamma and White balance are previously adjusted by dcraw in 8 bits color depth.
 
238
 
173
239
        imageData() = image;
174
240
    }
175
241
 
176
242
    //----------------------------------------------------------
 
243
    // Assign the right color-space profile.
 
244
 
 
245
    KGlobal::dirs()->addResourceType("profiles", KGlobal::dirs()->kde_default("data") + "digikam/profiles");
 
246
    switch(m_rawDecodingSettings.outputColorSpace)
 
247
    {
 
248
        case KDcrawIface::RawDecodingSettings::SRGB:
 
249
        {
 
250
            QString directory = KGlobal::dirs()->findResourceDir("profiles", "srgb.icm");
 
251
            m_image->getICCProfilFromFile(directory + "srgb.icm"); 
 
252
            break;
 
253
        }
 
254
        case KDcrawIface::RawDecodingSettings::ADOBERGB:
 
255
        {
 
256
            QString directory = KGlobal::dirs()->findResourceDir("profiles", "adobergb.icm");
 
257
            m_image->getICCProfilFromFile(directory + "adobergb.icm"); 
 
258
            break;
 
259
        }
 
260
        case KDcrawIface::RawDecodingSettings::WIDEGAMMUT:
 
261
        {
 
262
            QString directory = KGlobal::dirs()->findResourceDir("profiles", "widegamut.icm");
 
263
            m_image->getICCProfilFromFile(directory + "widegamut.icm"); 
 
264
            break;
 
265
        }
 
266
        case KDcrawIface::RawDecodingSettings::PROPHOTO:
 
267
        {
 
268
            QString directory = KGlobal::dirs()->findResourceDir("profiles", "prophoto.icm");
 
269
            m_image->getICCProfilFromFile(directory + "prophoto.icm"); 
 
270
            break;
 
271
        }
 
272
        default:
 
273
            // No icc color-space profile to assign in RAW color mode.
 
274
            break;
 
275
    }
 
276
 
 
277
    //----------------------------------------------------------
177
278
 
178
279
    imageWidth()  = width;
179
280
    imageHeight() = height;
183
284
}
184
285
 
185
286
}  // NameSpace Digikam
186
 
 
187