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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-21 23:19:11 UTC
  • mfrom: (1.2.33 upstream) (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20101221231911-z9jip7s5aht1jqn9
Tags: 2:1.7.0-1ubuntu1
* Merge from Debian Experimental. Remaining Ubuntu changes:
  - Export .pot name and copy to plugins in debian/rules
  - Version build-depends on kipi-plugins-dev to ensure build is against the
    same version on all archs
* Drop debian/patches/kubuntu_01_linker.diff, incoporated upstream
* Remove patches directory and unused patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
namespace Digikam
80
80
{
81
81
 
82
 
static bool CallbackForLibPGF(double percent, bool escapeAllowed, void *data)
 
82
static bool CallbackForLibPGF(double percent, bool escapeAllowed, void* data)
83
83
{
84
84
    if (data)
85
85
    {
86
 
        PGFLoader *d = static_cast<PGFLoader*>(data);
87
 
        if (d) return d->progressCallback(percent, escapeAllowed);
 
86
        PGFLoader* d = static_cast<PGFLoader*>(data);
 
87
 
 
88
        if (d)
 
89
        {
 
90
            return d->progressCallback(percent, escapeAllowed);
 
91
        }
88
92
    }
 
93
 
89
94
    return false;
90
95
}
91
96
 
92
97
PGFLoader::PGFLoader(DImg* image)
93
 
         : DImgLoader(image)
 
98
    : DImgLoader(image)
94
99
{
95
100
    m_hasAlpha   = false;
96
101
    m_sixteenBit = false;
97
102
    m_observer   = 0;
98
103
}
99
104
 
100
 
bool PGFLoader::load(const QString& filePath, DImgLoaderObserver *observer)
 
105
bool PGFLoader::load(const QString& filePath, DImgLoaderObserver* observer)
101
106
{
102
107
    m_observer = observer;
103
108
    readMetadata(filePath, DImg::PGF);
104
109
 
105
 
    FILE *file = fopen(QFile::encodeName(filePath), "rb");
 
110
    FILE* file = fopen(QFile::encodeName(filePath), "rb");
 
111
 
106
112
    if (!file)
107
113
    {
108
114
        kDebug() << "Error: Could not open source file.";
140
146
#else
141
147
    HANDLE fd = CreateFile(QFile::encodeName(filePath), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
142
148
#endif
 
149
 
143
150
    if (fd == INVALID_HANDLE_VALUE)
144
151
    {
145
152
        loadingFailed();
146
153
        return false;
147
154
    }
 
155
 
148
156
#else
149
157
    int fd = open(QFile::encodeName(filePath), O_RDONLY);
 
158
 
150
159
    if (fd == -1)
151
160
    {
152
161
        loadingFailed();
153
162
        return false;
154
163
    }
 
164
 
155
165
#endif
156
166
 
157
167
    CPGFFileStream stream(fd);
226
236
 
227
237
        int width   = pgf.Width();
228
238
        int height  = pgf.Height();
229
 
        uchar *data = 0;
 
239
        uchar* data = 0;
230
240
 
231
241
        QSize originalSize(width, height);
232
242
 
237
247
            int scaledLoadingSize = 0;
238
248
            int level             = 0;
239
249
            QVariant attribute = imageGetAttribute("scaledLoadingSize");
 
250
 
240
251
            if (attribute.isValid() && pgf.Levels() > 0)
241
252
            {
242
253
                scaledLoadingSize = attribute.toInt();
243
254
                int i, w, h;
 
255
 
244
256
                for (i=pgf.Levels()-1 ; i>=0 ; --i)
245
257
                {
246
258
                    w = pgf.Width(i);
247
259
                    h = pgf.Height(i);
 
260
 
248
261
                    if (qMin(w, h) >= scaledLoadingSize)
 
262
                    {
249
263
                        break;
 
264
                    }
250
265
                }
251
266
 
252
267
                if (i >= 0)
255
270
                    height = h;
256
271
                    level  = i;
257
272
                    kDebug() << "Loading PGF scaled version at level " << i
258
 
                                  << " (" << w << " x " << h << ") for size "
259
 
                                  << scaledLoadingSize;
 
273
                             << " (" << w << " x " << h << ") for size "
 
274
                             << scaledLoadingSize;
260
275
                }
261
276
            }
262
277
 
263
278
            if (m_sixteenBit)
264
 
                data = new uchar[width*height*8];  // 16 bits/color/pixel
 
279
            {
 
280
                data = new uchar[width*height*8];    // 16 bits/color/pixel
 
281
            }
265
282
            else
266
 
                data = new uchar[width*height*4];  // 8 bits/color/pixel
 
283
            {
 
284
                data = new uchar[width*height*4];    // 8 bits/color/pixel
 
285
            }
267
286
 
268
287
            // Fill all with 255 including alpha channel.
269
288
            memset(data, sizeof(data), 0xFF);
276
295
                          CallbackForLibPGF, this);
277
296
 
278
297
            if (observer)
 
298
            {
279
299
                observer->progressInfo(m_image, 1.0);
 
300
            }
280
301
        }
281
302
 
282
303
        imageWidth()  = width;
295
316
 
296
317
        return true;
297
318
    }
298
 
    catch(IOException& e)
 
319
    catch (IOException& e)
299
320
    {
300
321
        int err = e.error;
301
 
        if (err >= AppError) err -= AppError;
 
322
 
 
323
        if (err >= AppError)
 
324
        {
 
325
            err -= AppError;
 
326
        }
 
327
 
302
328
        kDebug() << "Error: Opening and reading PGF image failed (" << err << ")!";
303
329
 
304
330
#ifdef WIN32
310
336
        loadingFailed();
311
337
        return false;
312
338
    }
313
 
    catch(std::bad_alloc& e)
 
339
    catch (std::bad_alloc& e)
314
340
    {
315
341
        kError() << "Failed to allocate memory for loading" << filePath << e.what();
316
342
 
325
351
    }
326
352
}
327
353
 
328
 
bool PGFLoader::save(const QString& filePath, DImgLoaderObserver *observer)
 
354
bool PGFLoader::save(const QString& filePath, DImgLoaderObserver* observer)
329
355
{
330
356
    m_observer = observer;
331
357
 
335
361
#else
336
362
    HANDLE fd = CreateFile(QFile::encodeName(filePath), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
337
363
#endif
 
364
 
338
365
    if (fd == INVALID_HANDLE_VALUE)
339
366
    {
340
367
        kDebug() << "Error: Could not open destination file.";
341
368
        return false;
342
369
    }
 
370
 
343
371
#elif defined(__POSIX__)
344
372
    int fd = open(QFile::encodeName(filePath), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
373
 
345
374
    if (fd == -1)
346
375
    {
347
376
        kDebug() << "Error: Could not open destination file.";
348
377
        return false;
349
378
    }
 
379
 
350
380
#endif
351
381
 
352
382
    try
424
454
#endif
425
455
 
426
456
        if (observer)
 
457
        {
427
458
            observer->progressInfo(m_image, 1.0);
 
459
        }
428
460
 
429
461
        imageSetAttribute("savedformat", "PGF");
430
462
        saveMetadata(filePath);
431
463
 
432
464
        return true;
433
465
    }
434
 
    catch(IOException& e)
 
466
    catch (IOException& e)
435
467
    {
436
468
        int err = e.error;
437
 
        if (err >= AppError) err -= AppError;
 
469
 
 
470
        if (err >= AppError)
 
471
        {
 
472
            err -= AppError;
 
473
        }
 
474
 
438
475
        kDebug() << "Error: Opening and saving PGF image failed (" << err << ")!";
439
476
 
440
477
#ifdef WIN32
463
500
    {
464
501
        m_observer->progressInfo(m_image, percent);
465
502
 
466
 
    if (escapeAllowed)
467
 
        return (!m_observer->continueQuery(m_image));
 
503
        if (escapeAllowed)
 
504
        {
 
505
            return (!m_observer->continueQuery(m_image));
 
506
        }
468
507
    }
469
508
 
470
509
    return false;