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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2008-07-17 20:25:39 UTC
  • mfrom: (1.3.2 upstream) (37 hardy)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20080717202539-1bw3w3nrsso7yj4z
* New upstream release
  - digiKam 0.9.4 Release Plan (KDE3) ~ 13 July 08 (Closes: #490144)
* DEB_CONFIGURE_EXTRA_FLAGS := --without-included-sqlite3
* Debhelper compatibility level V7
* Install pixmaps in debian/*.install
* Add debian/digikam.lintian-overrides

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2005-06-14
 
7
 * Description : A QImage loader for DImg framework.
 
8
 * 
 
9
 * Copyright (C) 2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 
10
 * Copyright (C) 2006-2007 by Caulier Gilles <caulier dot gilles at gmail dot com> 
 
11
 *
 
12
 * This program is free software; you can redistribute it
 
13
 * and/or modify it under the terms of the GNU General
 
14
 * Public License as published by the Free Software Foundation;
 
15
 * either version 2, or (at your option)
 
16
 * any later version.
 
17
 * 
 
18
 * This program is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
 * GNU General Public License for more details.
 
22
 * 
 
23
 * ============================================================ */
 
24
 
 
25
// Qt includes.
 
26
 
 
27
#include <qimage.h>
 
28
 
 
29
// Local includes.
 
30
 
 
31
#include "ddebug.h"
 
32
#include "dimg.h"
 
33
#include "dimgloaderobserver.h"
 
34
#include "qimageloader.h"
 
35
 
 
36
namespace Digikam
 
37
{
 
38
 
 
39
QImageLoader::QImageLoader(DImg* image)
 
40
            : DImgLoader(image)
 
41
{
 
42
}
 
43
 
 
44
bool QImageLoader::load(const QString& filePath, DImgLoaderObserver *observer)
 
45
{
 
46
    // Loading is opaque to us. No support for stopping from observer,
 
47
    // progress info are only pseudo values
 
48
    QImage image(filePath);
 
49
 
 
50
    if (observer)
 
51
        observer->progressInfo(m_image, 0.9);
 
52
 
 
53
    if (image.isNull())
 
54
    {
 
55
        DDebug() << "Cannot loading \"" << filePath << "\" using DImg::QImageLoader!" << endl;
 
56
        return false;
 
57
    }
 
58
 
 
59
    m_hasAlpha    = image.hasAlphaBuffer();
 
60
    QImage target = image.convertDepth(32);
 
61
    
 
62
    uint w      = target.width();
 
63
    uint h      = target.height();
 
64
    uchar* data = new uchar[w*h*4];
 
65
    uint*  sptr = (uint*)target.bits();
 
66
    uchar* dptr = data;
 
67
    
 
68
    for (uint i = 0 ; i < w*h ; i++)
 
69
    {
 
70
        dptr[0] = qBlue(*sptr);
 
71
        dptr[1] = qGreen(*sptr);
 
72
        dptr[2] = qRed(*sptr);
 
73
        dptr[3] = qAlpha(*sptr);
 
74
        
 
75
        dptr += 4;
 
76
        sptr++;
 
77
    }
 
78
 
 
79
    if (observer)
 
80
        observer->progressInfo(m_image, 1.0);
 
81
 
 
82
    imageWidth()  = w;
 
83
    imageHeight() = h;
 
84
    imageData()   = data;
 
85
 
 
86
    // We considering that PNG is the most representative format of an image loaded by Qt
 
87
    imageSetAttribute("format", "PNG");
 
88
 
 
89
    return true;
 
90
}
 
91
 
 
92
bool QImageLoader::save(const QString& filePath, DImgLoaderObserver *observer)
 
93
{
 
94
    QVariant qualityAttr = imageGetAttribute("quality");
 
95
    int quality = qualityAttr.isValid() ? qualityAttr.toInt() : 90;
 
96
    
 
97
    if (quality < 0)
 
98
        quality = 90;
 
99
    if (quality > 100)
 
100
        quality = 100;
 
101
 
 
102
    QVariant formatAttr = imageGetAttribute("format");
 
103
    QCString format     = formatAttr.toCString();
 
104
 
 
105
    QImage image = m_image->copyQImage();
 
106
 
 
107
    if (observer)
 
108
        observer->progressInfo(m_image, 0.1);
 
109
 
 
110
    // Saving is opaque to us. No support for stopping from observer,
 
111
    // progress info are only pseudo values
 
112
    bool success = image.save(filePath, format.upper(), quality);
 
113
    if (observer && success)
 
114
        observer->progressInfo(m_image, 1.0);
 
115
 
 
116
    imageSetAttribute("format", format.upper());
 
117
 
 
118
    return success;
 
119
}
 
120
 
 
121
bool QImageLoader::hasAlpha() const
 
122
{
 
123
    return m_hasAlpha;
 
124
}
 
125
 
 
126
}  // NameSpace Digikam