Ignore:
Timestamp:
May 5, 2011, 5:36:53 AM (14 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/image/qpnghandler.cpp

    r651 r846  
    11/****************************************************************************
    22**
    3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
    44** All rights reserved.
    55** Contact: Nokia Corporation (qt-info@nokia.com)
     
    5151#include <qvector.h>
    5252
     53#ifdef QT_USE_BUNDLED_LIBPNG
     54#include <../../3rdparty/libpng/png.h>
     55#include <../../3rdparty/libpng/pngconf.h>
     56#else
    5357#include <png.h>
    5458#include <pngconf.h>
     59#endif
    5560
    5661#ifdef Q_OS_WINCE
     
    7782  Never to grayscale.
    7883*/
     84
     85class QPngHandlerPrivate
     86{
     87public:
     88    enum State {
     89        Ready,
     90        ReadHeader,
     91        ReadingEnd,
     92        Error
     93    };
     94
     95    QPngHandlerPrivate(QPngHandler *qq)
     96        : gamma(0.0), quality(2), png_ptr(0), info_ptr(0),
     97          end_info(0), row_pointers(0), state(Ready), q(qq)
     98    { }
     99
     100    float gamma;
     101    int quality;
     102    QString description;
     103
     104    png_struct *png_ptr;
     105    png_info *info_ptr;
     106    png_info *end_info;
     107    png_byte **row_pointers;
     108
     109    bool readPngHeader();
     110    bool readPngImage(QImage *image);
     111
     112    QImage::Format readImageFormat();
     113
     114    State state;
     115
     116    QPngHandler *q;
     117};
     118
    79119
    80120#if defined(Q_C_CALLBACKS)
     
    114154void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
    115155{
    116     QIODevice *in = (QIODevice *)png_get_io_ptr(png_ptr);
     156    QPngHandlerPrivate *d = (QPngHandlerPrivate *)png_get_io_ptr(png_ptr);
     157    QIODevice *in = d->q->device();
     158
     159    if (d->state == QPngHandlerPrivate::ReadingEnd && !in->isSequential() && (in->size() - in->pos()) < 4 && length == 4) {
     160        // Workaround for certain malformed PNGs that lack the final crc bytes
     161        uchar endcrc[4] = { 0xae, 0x42, 0x60, 0x82 };
     162        qMemCopy(data, endcrc, 4);
     163        in->seek(in->size());
     164        return;
     165    }
    117166
    118167    while (length) {
     
    163212    int bit_depth;
    164213    int color_type;
     214    png_bytep trans_alpha = 0;
     215    png_color_16p trans_color_p = 0;
     216    int num_trans;
     217    png_colorp palette = 0;
     218    int num_palette;
    165219    png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
    166220
    167221    if (color_type == PNG_COLOR_TYPE_GRAY) {
    168222        // Black & White or 8-bit grayscale
    169         if (bit_depth == 1 && info_ptr->channels == 1) {
     223        if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) {
    170224            png_set_invert_mono(png_ptr);
    171225            png_read_update_info(png_ptr, info_ptr);
     
    208262                image.setColor(i, qRgba(c,c,c,0xff));
    209263            }
    210             if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
    211 #if PNG_LIBPNG_VER_MAJOR < 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4)
    212                 const int g = info_ptr->trans_values.gray;
    213 #else
    214                 const int g = info_ptr->trans_color.gray;
    215 #endif
     264            if (png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color_p) && trans_color_p) {
     265                const int g = trans_color_p->gray;
    216266                if (g < ncols) {
    217267                    image.setColor(g, 0);
     
    220270        }
    221271    } else if (color_type == PNG_COLOR_TYPE_PALETTE
    222                && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
    223                && info_ptr->num_palette <= 256)
     272               && png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)
     273               && num_palette <= 256)
    224274    {
    225275        // 1-bit and 8-bit color
     
    234284                return;
    235285        }
    236         image.setColorCount(info_ptr->num_palette);
     286        png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
     287        image.setColorCount(num_palette);
    237288        int i = 0;
    238         if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
    239             while (i < info_ptr->num_trans) {
     289        if (png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color_p) && trans_alpha) {
     290            while (i < num_trans) {
    240291                image.setColor(i, qRgba(
    241                     info_ptr->palette[i].red,
    242                     info_ptr->palette[i].green,
    243                     info_ptr->palette[i].blue,
    244 #if PNG_LIBPNG_VER_MAJOR < 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 4)
    245                     info_ptr->trans[i]
    246 #else
    247                     info_ptr->trans_alpha[i]
    248 #endif
     292                    palette[i].red,
     293                    palette[i].green,
     294                    palette[i].blue,
     295                    trans_alpha[i]
    249296                   )
    250297               );
     
    252299            }
    253300        }
    254         while (i < info_ptr->num_palette) {
     301        while (i < num_palette) {
    255302            image.setColor(i, qRgba(
    256                 info_ptr->palette[i].red,
    257                 info_ptr->palette[i].green,
    258                 info_ptr->palette[i].blue,
     303                palette[i].red,
     304                palette[i].green,
     305                palette[i].blue,
    259306                0xff
    260307               )
     
    312359#endif
    313360
    314 class QPngHandlerPrivate
    315 {
    316 public:
    317     enum State {
    318         Ready,
    319         ReadHeader,
    320         Error
    321     };
    322 
    323     QPngHandlerPrivate(QPngHandler *qq)
    324         : gamma(0.0), quality(2), png_ptr(0), info_ptr(0),
    325           end_info(0), row_pointers(0), state(Ready), q(qq)
    326     { }
    327 
    328     float gamma;
    329     int quality;
    330     QString description;
    331 
    332     png_struct *png_ptr;
    333     png_info *info_ptr;
    334     png_info *end_info;
    335     png_byte **row_pointers;
    336 
    337     bool readPngHeader();
    338     bool readPngImage(QImage *image);
    339 
    340     QImage::Format readImageFormat();
    341 
    342     State state;
    343 
    344     QPngHandler *q;
    345 };
    346361
    347362/*!
     
    377392    }
    378393
    379     png_set_read_fn(png_ptr, q->device(), iod_read_fn);
     394    png_set_read_fn(png_ptr, this, iod_read_fn);
    380395    png_read_info(png_ptr, info_ptr);
    381396
     
    387402    while (num_text--) {
    388403        QString key, value;
    389 #if defined(PNG_iTXt_SUPPORTED)
     404#if defined(PNG_iTXt_SUPPORTED) && !defined(QT_NO_TEXTCODEC)
    390405        if (text_ptr->lang) {
    391406            QTextCodec *codec = QTextCodec::codecForName(text_ptr->lang);
     
    503518#endif
    504519
     520    state = ReadingEnd;
    505521    png_read_end(png_ptr, end_info);
    506522    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
     
    532548        png_uint_32 width, height;
    533549        int bit_depth, color_type;
    534         if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
     550        png_colorp palette;
     551        int num_palette;
     552        png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
     553        if (color_type == PNG_COLOR_TYPE_GRAY) {
    535554            // Black & White or 8-bit grayscale
    536             if (info_ptr->bit_depth == 1 && info_ptr->channels == 1) {
     555            if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) {
    537556                format = QImage::Format_Mono;
    538             } else if (info_ptr->bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
     557            } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
    539558                format = QImage::Format_ARGB32;
    540559            } else {
    541560                format = QImage::Format_Indexed8;
    542561            }
    543         } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE
    544                 && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
    545                    && info_ptr->num_palette <= 256)
     562        } else if (color_type == PNG_COLOR_TYPE_PALETTE
     563                   && png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)
     564                   && num_palette <= 256)
    546565        {
    547566            // 1-bit and 8-bit color
    548             if (info_ptr->bit_depth != 1)
     567            if (bit_depth != 1)
    549568                png_set_packing(png_ptr);
    550569            png_read_update_info(png_ptr, info_ptr);
     
    553572        } else {
    554573            // 32-bit
    555             if (info_ptr->bit_depth == 16)
     574            if (bit_depth == 16)
    556575                png_set_strip_16(png_ptr);
    557576
    558577            format = QImage::Format_ARGB32;
    559578            // Only add filler if no alpha, or we can get 5 channel data.
    560             if (!(info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
     579            if (!(color_type & PNG_COLOR_MASK_ALPHA)
    561580                && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
    562581                // We want 4 bytes, but it isn't an alpha channel
     
    649668        text_ptr[i].text_length = 0;
    650669        text_ptr[i].itxt_length = value.size();
    651         text_ptr[i].lang = "UTF-8";
     670        text_ptr[i].lang = const_cast<char*>("UTF-8");
    652671        text_ptr[i].lang_key = qstrdup(it.key().toUtf8().constData());
    653672#endif
     
    673692}
    674693
    675 bool Q_INTERNAL_WIN_NO_THROW QPNGImageWriter::writeImage(const QImage& image_in, int quality_in, const QString &description,
     694bool Q_INTERNAL_WIN_NO_THROW QPNGImageWriter::writeImage(const QImage& image, int quality_in, const QString &description,
    676695                                 int off_x_in, int off_y_in)
    677696{
     
    679698    Q_UNUSED(description);
    680699#endif
    681 
    682     QImage image;
    683     switch (image_in.format()) {
    684     case QImage::Format_ARGB32_Premultiplied:
    685     case QImage::Format_ARGB4444_Premultiplied:
    686     case QImage::Format_ARGB8555_Premultiplied:
    687     case QImage::Format_ARGB8565_Premultiplied:
    688     case QImage::Format_ARGB6666_Premultiplied:
    689         image = image_in.convertToFormat(QImage::Format_ARGB32);
    690         break;
    691     case QImage::Format_RGB16:
    692     case QImage::Format_RGB444:
    693     case QImage::Format_RGB555:
    694     case QImage::Format_RGB666:
    695     case QImage::Format_RGB888:
    696         image = image_in.convertToFormat(QImage::Format_RGB32);
    697         break;
    698     default:
    699         image = image_in;
    700         break;
    701     }
    702700
    703701    QPoint offset = image.offset();
     
    707705    png_structp png_ptr;
    708706    png_infop info_ptr;
    709     png_bytep* row_pointers;
    710707
    711708    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);
     
    736733    }
    737734
     735    png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn);
     736
     737
     738    int color_type = 0;
     739    if (image.colorCount())
     740        color_type = PNG_COLOR_TYPE_PALETTE;
     741    else if (image.hasAlphaChannel())
     742        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
     743    else
     744        color_type = PNG_COLOR_TYPE_RGB;
     745
     746    png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
     747                 image.depth() == 1 ? 1 : 8, // per channel
     748                 color_type, 0, 0, 0);       // sets #channels
     749
    738750    if (gamma != 0.0) {
    739751        png_set_gAMA(png_ptr, info_ptr, 1.0/gamma);
    740752    }
    741753
    742     png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn);
    743 
    744     info_ptr->channels =
    745         (image.depth() == 32)
    746         ? (image.format() == QImage::Format_RGB32 ? 3 : 4)
    747         : 1;
    748 
    749     png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
    750         image.depth() == 1 ? 1 : 8 /* per channel */,
    751         image.depth() == 32
    752             ? image.format() == QImage::Format_RGB32
    753                 ? PNG_COLOR_TYPE_RGB
    754                 : PNG_COLOR_TYPE_RGB_ALPHA
    755             : PNG_COLOR_TYPE_PALETTE, 0, 0, 0);
    756 
    757 
    758     //png_set_sBIT(png_ptr, info_ptr, 8);
    759     info_ptr->sig_bit.red = 8;
    760     info_ptr->sig_bit.green = 8;
    761     info_ptr->sig_bit.blue = 8;
     754    png_color_8 sig_bit;
     755    sig_bit.red = 8;
     756    sig_bit.green = 8;
     757    sig_bit.blue = 8;
     758    sig_bit.alpha = image.hasAlphaChannel() ? 8 : 0;
     759    png_set_sBIT(png_ptr, info_ptr, &sig_bit);
    762760
    763761    if (image.format() == QImage::Format_MonoLSB)
    764762       png_set_packswap(png_ptr);
    765763
    766     png_colorp palette = 0;
    767     png_bytep copy_trans = 0;
    768764    if (image.colorCount()) {
    769765        // Paletted
    770         int num_palette = image.colorCount();
    771         palette = new png_color[num_palette];
    772         png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
    773         int* trans = new int[num_palette];
     766        int num_palette = qMin(256, image.colorCount());
     767        png_color palette[256];
     768        png_byte trans[256];
    774769        int num_trans = 0;
    775770        for (int i=0; i<num_palette; i++) {
    776             QRgb rgb=image.color(i);
    777             info_ptr->palette[i].red = qRed(rgb);
    778             info_ptr->palette[i].green = qGreen(rgb);
    779             info_ptr->palette[i].blue = qBlue(rgb);
    780             trans[i] = rgb >> 24;
     771            QRgb rgba=image.color(i);
     772            palette[i].red = qRed(rgba);
     773            palette[i].green = qGreen(rgba);
     774            palette[i].blue = qBlue(rgba);
     775            trans[i] = qAlpha(rgba);
    781776            if (trans[i] < 255) {
    782777                num_trans = i+1;
    783778            }
    784779        }
     780        png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
     781
    785782        if (num_trans) {
    786             copy_trans = new png_byte[num_trans];
    787             for (int i=0; i<num_trans; i++)
    788                 copy_trans[i] = trans[i];
    789             png_set_tRNS(png_ptr, info_ptr, copy_trans, num_trans, 0);
    790         }
    791         delete [] trans;
    792     }
    793 
    794     if (image.format() != QImage::Format_RGB32) {
    795         info_ptr->sig_bit.alpha = 8;
     783            png_set_tRNS(png_ptr, info_ptr, trans, num_trans, 0);
     784        }
    796785    }
    797786
     
    802791    }
    803792
    804     // Qt==ARGB==Big(ARGB)==Little(BGRA)
    805     if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
     793    // Qt==ARGB==Big(ARGB)==Little(BGRA). But RGB888 is RGB regardless
     794    if (QSysInfo::ByteOrder == QSysInfo::LittleEndian
     795        && image.format() != QImage::Format_RGB888) {
    806796        png_set_bgr(png_ptr);
    807797    }
     
    828818        png_set_packing(png_ptr);
    829819
    830     if (image.format() == QImage::Format_RGB32)
     820    if (color_type == PNG_COLOR_TYPE_RGB && image.format() != QImage::Format_RGB888)
    831821        png_set_filler(png_ptr, 0,
    832822            QSysInfo::ByteOrder == QSysInfo::BigEndian ?
     
    849839    }
    850840
    851     png_uint_32 width;
    852     png_uint_32 height;
    853     int bit_depth;
    854     int color_type;
    855     png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
    856         0, 0, 0);
    857 
    858     const uchar *data = (static_cast<const QImage *>(&image))->bits();
    859     int bpl = image.bytesPerLine();
    860     row_pointers = new png_bytep[height];
    861     uint y;
    862     for (y=0; y<height; y++) {
    863         row_pointers[y] = (png_bytep)(data + y * bpl);
    864     }
    865     png_write_image(png_ptr, row_pointers);
    866     delete [] row_pointers;
     841    int height = image.height();
     842    int width = image.width();
     843    switch (image.format()) {
     844    case QImage::Format_Mono:
     845    case QImage::Format_MonoLSB:
     846    case QImage::Format_Indexed8:
     847    case QImage::Format_RGB32:
     848    case QImage::Format_ARGB32:
     849    case QImage::Format_RGB888:
     850        {
     851            png_bytep* row_pointers = new png_bytep[height];
     852            for (int y=0; y<height; y++)
     853                row_pointers[y] = (png_bytep)image.constScanLine(y);
     854            png_write_image(png_ptr, row_pointers);
     855            delete [] row_pointers;
     856        }
     857        break;
     858    default:
     859        {
     860            QImage::Format fmt = image.hasAlphaChannel() ? QImage::Format_ARGB32 : QImage::Format_RGB32;
     861            QImage row;
     862            png_bytep row_pointers[1];
     863            for (int y=0; y<height; y++) {
     864                row = image.copy(0, y, width, 1).convertToFormat(fmt);
     865                row_pointers[0] = png_bytep(row.constScanLine(0));
     866                png_write_rows(png_ptr, row_pointers, 1);
     867            }
     868        }
     869        break;
     870    }
    867871
    868872    png_write_end(png_ptr, info_ptr);
    869873    frames_written++;
    870 
    871     if (palette)
    872         delete [] palette;
    873     if (copy_trans)
    874         delete [] copy_trans;
    875874
    876875    png_destroy_write_struct(&png_ptr, &info_ptr);
     
    905904bool QPngHandler::canRead() const
    906905{
    907     if (d->state == QPngHandlerPrivate::Ready) {
    908         if (!canRead(device()))
    909             return false;
     906    if (d->state == QPngHandlerPrivate::Ready && !canRead(device()))
     907        return false;
     908
     909    if (d->state != QPngHandlerPrivate::Error) {
    910910        setFormat("png");
    911911        return true;
    912912    }
    913     return d->state != QPngHandlerPrivate::Error;
     913
     914    return false;
    914915}
    915916
     
    959960        return d->description;
    960961    else if (option == Size)
    961         return QSize(d->info_ptr->width, d->info_ptr->height);
     962        return QSize(png_get_image_width(d->png_ptr, d->info_ptr),
     963                     png_get_image_height(d->png_ptr, d->info_ptr));
    962964    else if (option == ImageFormat)
    963965        return d->readImageFormat();
Note: See TracChangeset for help on using the changeset viewer.