src/gui/image/qpixmap_mac.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   158 
   158 
   159 QSet<QMacPixmapData*> QMacPixmapData::validDataPointers;
   159 QSet<QMacPixmapData*> QMacPixmapData::validDataPointers;
   160 
   160 
   161 QMacPixmapData::QMacPixmapData(PixelType type)
   161 QMacPixmapData::QMacPixmapData(PixelType type)
   162     : QPixmapData(type, MacClass), has_alpha(0), has_mask(0),
   162     : QPixmapData(type, MacClass), has_alpha(0), has_mask(0),
   163       uninit(true), pixels(0), pixelsToFree(0), bytesPerRow(0),
   163       uninit(true), pixels(0), pixelsSize(0), pixelsToFree(0),
   164       cg_data(0), cg_dataBeingReleased(0), cg_mask(0),
   164       bytesPerRow(0), cg_data(0), cg_dataBeingReleased(0), cg_mask(0),
   165       pengine(0)
   165       pengine(0)
   166 {
   166 {
   167 }
   167 }
   168 
   168 
   169 QPixmapData *QMacPixmapData::createCompatiblePixmapData() const
   169 QPixmapData *QMacPixmapData::createCompatiblePixmapData() const
   246                                    | Qt::PreferDither;
   246                                    | Qt::PreferDither;
   247             conv8 = true;
   247             conv8 = true;
   248         } else if ((flags & Qt::ColorMode_Mask) == Qt::ColorOnly) {
   248         } else if ((flags & Qt::ColorMode_Mask) == Qt::ColorOnly) {
   249             conv8 = d == 1;                     // native depth wanted
   249             conv8 = d == 1;                     // native depth wanted
   250         } else if (d == 1) {
   250         } else if (d == 1) {
   251             if (image.numColors() == 2) {
   251             if (image.colorCount() == 2) {
   252                 QRgb c0 = image.color(0);       // Auto: convert to best
   252                 QRgb c0 = image.color(0);       // Auto: convert to best
   253                 QRgb c1 = image.color(1);
   253                 QRgb c1 = image.color(1);
   254                 conv8 = qMin(c0,c1) != qRgb(0,0,0) || qMax(c0,c1) != qRgb(255,255,255);
   254                 conv8 = qMin(c0,c1) != qRgb(0,0,0) || qMax(c0,c1) != qRgb(255,255,255);
   255             } else {
   255             } else {
   256                 // eg. 1-color monochrome images (they do exist).
   256                 // eg. 1-color monochrome images (they do exist).
   303                 else
   303                 else
   304                     *(drow+x) = 0xFFFFFFFF;
   304                     *(drow+x) = 0xFFFFFFFF;
   305             }
   305             }
   306             break;
   306             break;
   307         }
   307         }
   308         case QImage::Format_Indexed8:
   308         case QImage::Format_Indexed8: {
   309             for (int x = 0; x < w; ++x) {
   309             int numColors = image.numColors();
   310                 *(drow+x) = PREMUL(image.color(*(srow + x)));
   310             if (numColors > 0) {
       
   311                 for (int x = 0; x < w; ++x) {
       
   312                     int index = *(srow + x);
       
   313                     *(drow+x) = PREMUL(image.color(qMin(index, numColors)));
       
   314                 }
   311             }
   315             }
   312             break;
   316         } break;
   313         case QImage::Format_RGB32:
   317         case QImage::Format_RGB32:
   314             for (int x = 0; x < w; ++x)
   318             for (int x = 0; x < w; ++x)
   315                 *(drow+x) = *(((quint32*)srow) + x) | 0xFF000000;
   319                 *(drow+x) = *(((quint32*)srow) + x) | 0xFF000000;
   316             break;
   320             break;
   317         case QImage::Format_ARGB32:
   321         case QImage::Format_ARGB32:
   333     }
   337     }
   334     if (sfmt != QImage::Format_RGB32) { //setup the alpha
   338     if (sfmt != QImage::Format_RGB32) { //setup the alpha
   335         bool alphamap = image.depth() == 32;
   339         bool alphamap = image.depth() == 32;
   336         if (sfmt == QImage::Format_Indexed8) {
   340         if (sfmt == QImage::Format_Indexed8) {
   337             const QVector<QRgb> rgb = image.colorTable();
   341             const QVector<QRgb> rgb = image.colorTable();
   338             for (int i = 0, count = image.numColors(); i < count; ++i) {
   342             for (int i = 0, count = image.colorCount(); i < count; ++i) {
   339                 const int alpha = qAlpha(rgb[i]);
   343                 const int alpha = qAlpha(rgb[i]);
   340                 if (alpha != 0xff) {
   344                 if (alpha != 0xff) {
   341                     alphamap = true;
   345                     alphamap = true;
   342                     break;
   346                     break;
   343                 }
   347                 }
   349 }
   353 }
   350 
   354 
   351 int get_index(QImage * qi,QRgb mycol)
   355 int get_index(QImage * qi,QRgb mycol)
   352 {
   356 {
   353     int loopc;
   357     int loopc;
   354     for(loopc=0;loopc<qi->numColors();loopc++) {
   358     for(loopc=0;loopc<qi->colorCount();loopc++) {
   355         if(qi->color(loopc)==mycol)
   359         if(qi->color(loopc)==mycol)
   356             return loopc;
   360             return loopc;
   357     }
   361     }
   358     qi->setNumColors(qi->numColors()+1);
   362     qi->setColorCount(qi->colorCount()+1);
   359     qi->setColor(qi->numColors(),mycol);
   363     qi->setColor(qi->colorCount(),mycol);
   360     return qi->numColors();
   364     return qi->colorCount();
   361 }
   365 }
   362 
   366 
   363 QImage QMacPixmapData::toImage() const
   367 QImage QMacPixmapData::toImage() const
   364 {
   368 {
   365     QImage::Format format = QImage::Format_MonoLSB;
   369     QImage::Format format = QImage::Format_MonoLSB;
   370     QImage image(w, h, format);
   374     QImage image(w, h, format);
   371     quint32 *sptr = pixels, *srow;
   375     quint32 *sptr = pixels, *srow;
   372     const uint sbpr = bytesPerRow;
   376     const uint sbpr = bytesPerRow;
   373     if (format == QImage::Format_MonoLSB) {
   377     if (format == QImage::Format_MonoLSB) {
   374         image.fill(0);
   378         image.fill(0);
   375         image.setNumColors(2);
   379         image.setColorCount(2);
   376         image.setColor(0, QColor(Qt::color0).rgba());
   380         image.setColor(0, QColor(Qt::color0).rgba());
   377         image.setColor(1, QColor(Qt::color1).rgba());
   381         image.setColor(1, QColor(Qt::color1).rgba());
   378         for (int y = 0; y < h; ++y) {
   382         for (int y = 0; y < h; ++y) {
   379             uchar *scanLine = image.scanLine(y);
   383             uchar *scanLine = image.scanLine(y);
   380             srow = sptr + (y * (sbpr/4));
   384             srow = sptr + (y * (sbpr/4));
   631         // We need a block of memory to do stuff with.
   635         // We need a block of memory to do stuff with.
   632         base_pixels = static_cast<quint32 *>(malloc(numBytes));
   636         base_pixels = static_cast<quint32 *>(malloc(numBytes));
   633     }
   637     }
   634 
   638 
   635     if (pixels)
   639     if (pixels)
   636         memcpy(base_pixels, pixels, numBytes);
   640         memcpy(base_pixels, pixels, pixelsSize);
   637     pixels = base_pixels;
   641     pixels = base_pixels;
       
   642     pixelsSize = numBytes;
   638 }
   643 }
   639 
   644 
   640 #if 0
   645 #if 0
   641 QPixmap QMacPixmapData::transformed(const QTransform &transform,
   646 QPixmap QMacPixmapData::transformed(const QTransform &transform,
   642                                     Qt::TransformationMode mode) const
   647                                     Qt::TransformationMode mode) const
   963     \warning This function is only available on Mac OS X.
   968     \warning This function is only available on Mac OS X.
   964 */
   969 */
   965 
   970 
   966 Qt::HANDLE QPixmap::macCGHandle() const
   971 Qt::HANDLE QPixmap::macCGHandle() const
   967 {
   972 {
       
   973     if (isNull())
       
   974         return 0;
       
   975 
   968     if (data->classId() == QPixmapData::MacClass) {
   976     if (data->classId() == QPixmapData::MacClass) {
   969         QMacPixmapData *d = static_cast<QMacPixmapData *>(data.data());
   977         QMacPixmapData *d = static_cast<QMacPixmapData *>(data.data());
   970         if (!d->cg_data)
   978         if (!d->cg_data)
   971             d->macCreateCGImageRef();
   979             d->macCreateCGImageRef();
   972         CGImageRef ret = d->cg_data;
   980         CGImageRef ret = d->cg_data;