/*! Returns a copy of the given \a image converted to a bitmap using the specified image conversion \a flags. \sa fromData() */ QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) { if (image.isNull()) return QBitmap(); QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags); // make sure image.color(0) == Qt::color0 (white) // and image.color(1) == Qt::color1 (black) const QRgb c0 = QColor(Qt::black).rgb(); const QRgb c1 = QColor(Qt::white).rgb(); if (img.color(0) == c0 && img.color(1) == c1) { img.invertPixels(); img.setColor(0, c1); img.setColor(1, c0); } QPixmapData *d; // QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem(); // if (gs) // d = gs->createPixmapData(QPixmapData::BitmapType); // else // d = QGraphicsSystem::createDefaultPixmapData(QPixmapData::BitmapType); // // d->fromImage(img, flags | Qt::MonoOnly); return QPixmap(d); }
static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, const QImage &image, Qt::ImageConversionFlags flags) { if (d->pixelType() == QPlatformPixmap::BitmapType) { QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags); // make sure image.color(0) == Qt::color0 (white) // and image.color(1) == Qt::color1 (black) const QRgb c0 = QColor(Qt::black).rgb(); const QRgb c1 = QColor(Qt::white).rgb(); if (img.color(0) == c0 && img.color(1) == c1) { img.invertPixels(); img.setColor(0, c1); img.setColor(1, c0); } return img; } return image; }
int player_preview_area::get_colorn_from_rgb(QImage &image, int r, int g, int b) { QColor temp_key_color1(r, g, b); for (int i=0; i<image.colorCount(); i++) { if (temp_key_color1.rgb() == image.color(i)) { return i; } } return 0; }
static QImage toRgba(const QImage& image, int alpha) { if ( alpha < 0 || alpha >= 255 ) return image; #if QT_VERSION < 0x040000 QImage alphaImage(image.size(), 32); alphaImage.setAlphaBuffer(true); #else QImage alphaImage(image.size(), QImage::Format_ARGB32); #endif const QRgb mask1 = qRgba(0, 0, 0, alpha); const QRgb mask2 = qRgba(255, 255, 255, 0); const QRgb mask3 = qRgba(0, 0, 0, 255); const int w = image.size().width(); const int h = image.size().height(); if ( image.depth() == 8 ) { for ( int y = 0; y < h; y++ ) { QRgb* alphaLine = (QRgb*)alphaImage.scanLine(y); const unsigned char *line = image.scanLine(y); for ( int x = 0; x < w; x++ ) *alphaLine++ = (image.color(*line++) & mask2) | mask1; } } else if ( image.depth() == 32 ) { for ( int y = 0; y < h; y++ ) { QRgb* alphaLine = (QRgb*)alphaImage.scanLine(y); const QRgb* line = (const QRgb*) image.scanLine(y); for ( int x = 0; x < w; x++ ) { const QRgb rgb = *line++; if ( rgb & mask3 ) // alpha != 0 *alphaLine++ = (rgb & mask2) | mask1; else *alphaLine++ = rgb; } } } return alphaImage; }
QPixmap *Board::lighten(QPixmap *src) { const float FACTOR = 1.3; QImage img = src->convertToImage(); if(img.depth() > 8) { // at least high-color for(int y = 0; y < src->height(); y++) { uchar *p = (uchar *)img.scanLine(y); for(int x = 0; x < src->width() * 4; x++) { *p = (unsigned char)MIN(255, (int)(FACTOR * (*p))); p++; } } } else { // image has a palette // get background color index int idx = img.pixelIndex(8, 1); // should work for all tiles img.setColor(idx, QColor(img.color(idx)).light().rgb()); } QPixmap *pm = new QPixmap(); pm->convertFromImage(img); return pm; }
QPixmap *MyIconFactory::createPixmap(const QIconSet &set, QIconSet::Size size, QIconSet::Mode mode, QIconSet::State state) { if (mode != QIconSet::Disabled) { return 0; } QImage image; image = set.pixmap(size, QIconSet::Normal, state); const int numColors = image.numColors(); if (numColors > 0) { for (int i = 0; i < numColors; ++i) { image.setColor(i, grayRgb(image.color(i))); } } else { const int width = image.width(); const int height = image.height(); for (int i = 0; i < width; ++i) { for (int j = 0; j < height; ++j) { image.setPixel(i, j, grayRgb(image.pixel(i, j))); } } } return new QPixmap(image); }
void QMacPixmapData::fromImage(const QImage &img, Qt::ImageConversionFlags flags) { setSerialNumber(++qt_pixmap_serial); // the conversion code only handles format >= // Format_ARGB32_Premultiplied at the moment.. if (img.format() > QImage::Format_ARGB32_Premultiplied) { QImage image; if (img.hasAlphaChannel()) image = img.convertToFormat(QImage::Format_ARGB32_Premultiplied); else image = img.convertToFormat(QImage::Format_RGB32); fromImage(image, flags); return; } w = img.width(); h = img.height(); is_null = (w <= 0 || h <= 0); d = (pixelType() == BitmapType ? 1 : img.depth()); QImage image = img; int dd = QPixmap::defaultDepth(); bool force_mono = (dd == 1 || (flags & Qt::ColorMode_Mask)==Qt::MonoOnly); if (force_mono) { // must be monochrome if (d != 1) { image = image.convertToFormat(QImage::Format_MonoLSB, flags); // dither d = 1; } } else { // can be both bool conv8 = false; if(d > 8 && dd <= 8) { // convert to 8 bit if ((flags & Qt::DitherMode_Mask) == Qt::AutoDither) flags = (flags & ~Qt::DitherMode_Mask) | Qt::PreferDither; conv8 = true; } else if ((flags & Qt::ColorMode_Mask) == Qt::ColorOnly) { conv8 = d == 1; // native depth wanted } else if (d == 1) { if (image.colorCount() == 2) { QRgb c0 = image.color(0); // Auto: convert to best QRgb c1 = image.color(1); conv8 = qMin(c0,c1) != qRgb(0,0,0) || qMax(c0,c1) != qRgb(255,255,255); } else { // eg. 1-color monochrome images (they do exist). conv8 = true; } } if (conv8) { image = image.convertToFormat(QImage::Format_Indexed8, flags); d = 8; } } if (image.depth()==1) { image.setColor(0, QColor(Qt::color0).rgba()); image.setColor(1, QColor(Qt::color1).rgba()); } if (d == 16 || d == 24) { image = image.convertToFormat(QImage::Format_RGB32, flags); fromImage(image, flags); return; } // different size or depth, make a new pixmap resize(w, h); quint32 *dptr = pixels, *drow; const uint dbpr = bytesPerRow; const QImage::Format sfmt = image.format(); const unsigned short sbpr = image.bytesPerLine(); // use const_cast to prevent a detach const uchar *sptr = const_cast<const QImage &>(image).bits(), *srow; for (int y = 0; y < h; ++y) { drow = dptr + (y * (dbpr / 4)); srow = sptr + (y * sbpr); switch(sfmt) { case QImage::Format_MonoLSB: case QImage::Format_Mono:{ for (int x = 0; x < w; ++x) { char one_bit = *(srow + (x / 8)); if (sfmt == QImage::Format_Mono) one_bit = one_bit >> (7 - (x % 8)); else one_bit = one_bit >> (x % 8); if ((one_bit & 0x01)) *(drow+x) = 0xFF000000; else *(drow+x) = 0xFFFFFFFF; } break; } case QImage::Format_Indexed8: { int numColors = image.numColors(); if (numColors > 0) { for (int x = 0; x < w; ++x) { int index = *(srow + x); *(drow+x) = PREMUL(image.color(qMin(index, numColors))); } } } break; case QImage::Format_RGB32: for (int x = 0; x < w; ++x) *(drow+x) = *(((quint32*)srow) + x) | 0xFF000000; break; case QImage::Format_ARGB32: case QImage::Format_ARGB32_Premultiplied: for (int x = 0; x < w; ++x) { if(sfmt == QImage::Format_RGB32) *(drow+x) = 0xFF000000 | (*(((quint32*)srow) + x) & 0x00FFFFFF); else if(sfmt == QImage::Format_ARGB32_Premultiplied) *(drow+x) = *(((quint32*)srow) + x); else *(drow+x) = PREMUL(*(((quint32*)srow) + x)); } break; default: qWarning("Qt: internal: Oops: Forgot a format [%d] %s:%d", sfmt, __FILE__, __LINE__); break; } }
static void setup_qt( QImage& image, png_structp png_ptr, png_infop info_ptr, float screen_gamma=0.0 ) { if ( screen_gamma != 0.0 && png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) ) { double file_gamma; png_get_gAMA(png_ptr, info_ptr, &file_gamma); png_set_gamma( png_ptr, screen_gamma, file_gamma ); } png_uint_32 width; png_uint_32 height; int bit_depth; int color_type; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0); if ( color_type == PNG_COLOR_TYPE_GRAY ) { // Black & White or 8-bit grayscale if ( bit_depth == 1 && info_ptr->channels == 1 ) { png_set_invert_mono( png_ptr ); png_read_update_info( png_ptr, info_ptr ); if (!image.create( width, height, 1, 2, QImage::BigEndian )) return; image.setColor( 1, qRgb(0,0,0) ); image.setColor( 0, qRgb(255,255,255) ); } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { png_set_expand(png_ptr); png_set_strip_16(png_ptr); png_set_gray_to_rgb(png_ptr); if (!image.create(width, height, 32)) return; image.setAlphaBuffer(TRUE); if (QImage::systemByteOrder() == QImage::BigEndian) png_set_swap_alpha(png_ptr); png_read_update_info(png_ptr, info_ptr); } else { if ( bit_depth == 16 ) png_set_strip_16(png_ptr); else if ( bit_depth < 8 ) png_set_packing(png_ptr); int ncols = bit_depth < 8 ? 1 << bit_depth : 256; png_read_update_info(png_ptr, info_ptr); if (!image.create(width, height, 8, ncols)) return; for (int i=0; i<ncols; i++) { int c = i*255/(ncols-1); image.setColor( i, qRgba(c,c,c,0xff) ); } if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) { const int g = info_ptr->trans_values.gray; if (g < ncols) { image.setAlphaBuffer(TRUE); image.setColor(g, image.color(g) & RGB_MASK); } } } } else if ( color_type == PNG_COLOR_TYPE_PALETTE && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE) && info_ptr->num_palette <= 256 ) { // 1-bit and 8-bit color if ( bit_depth != 1 ) png_set_packing( png_ptr ); png_read_update_info( png_ptr, info_ptr ); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0); if (!image.create(width, height, bit_depth, info_ptr->num_palette, QImage::BigEndian)) return; int i = 0; if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) { image.setAlphaBuffer( TRUE ); while ( i < info_ptr->num_trans ) { image.setColor(i, qRgba( info_ptr->palette[i].red, info_ptr->palette[i].green, info_ptr->palette[i].blue, info_ptr->trans[i] ) ); i++; } } while ( i < info_ptr->num_palette ) { image.setColor(i, qRgba( info_ptr->palette[i].red, info_ptr->palette[i].green, info_ptr->palette[i].blue, 0xff ) ); i++; } } else { // 32-bit if ( bit_depth == 16 ) png_set_strip_16(png_ptr); png_set_expand(png_ptr); if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) png_set_gray_to_rgb(png_ptr); if (!image.create(width, height, 32)) return; // Only add filler if no alpha, or we can get 5 channel data. if (!(color_type & PNG_COLOR_MASK_ALPHA) && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { png_set_filler(png_ptr, 0xff, QImage::systemByteOrder() == QImage::BigEndian ? PNG_FILLER_BEFORE : PNG_FILLER_AFTER); // We want 4 bytes, but it isn't an alpha channel } else { image.setAlphaBuffer(TRUE); } if ( QImage::systemByteOrder() == QImage::BigEndian ) { png_set_swap_alpha(png_ptr); } png_read_update_info(png_ptr, info_ptr); } // Qt==ARGB==Big(ARGB)==Little(BGRA) if ( QImage::systemByteOrder() == QImage::LittleEndian ) { png_set_bgr(png_ptr); } }
static bool write_xbm_image(const QImage &sourceImage, QIODevice *device, const QString &fileName) { QImage image = sourceImage; int w = image.width(); int h = image.height(); int i; QString s = fileName; // get file base name int msize = s.length() + 100; char *buf = new char[msize]; qsnprintf(buf, msize, "#define %s_width %d\n", s.toAscii().data(), w); device->write(buf, qstrlen(buf)); qsnprintf(buf, msize, "#define %s_height %d\n", s.toAscii().data(), h); device->write(buf, qstrlen(buf)); qsnprintf(buf, msize, "static char %s_bits[] = {\n ", s.toAscii().data()); device->write(buf, qstrlen(buf)); if (image.format() != QImage::Format_MonoLSB) image = image.convertToFormat(QImage::Format_MonoLSB); bool invert = qGray(image.color(0)) < qGray(image.color(1)); char hexrep[16]; for (i=0; i<10; i++) hexrep[i] = '0' + i; for (i=10; i<16; i++) hexrep[i] = 'a' -10 + i; if (invert) { char t; for (i=0; i<8; i++) { t = hexrep[15-i]; hexrep[15-i] = hexrep[i]; hexrep[i] = t; } } int bcnt = 0; register char *p = buf; int bpl = (w+7)/8; for (int y = 0; y < h; ++y) { uchar *b = image.scanLine(y); for (i = 0; i < bpl; ++i) { *p++ = '0'; *p++ = 'x'; *p++ = hexrep[*b >> 4]; *p++ = hexrep[*b++ & 0xf]; if (i < bpl - 1 || y < h - 1) { *p++ = ','; if (++bcnt > 14) { *p++ = '\n'; *p++ = ' '; *p = '\0'; if ((int)qstrlen(buf) != device->write(buf, qstrlen(buf))) { delete [] buf; return false; } p = buf; bcnt = 0; } } } } #if defined(_MSC_VER) && _MSC_VER >= 1400 strcpy_s(p, sizeof(" };\n"), " };\n"); #else strcpy(p, " };\n"); #endif if ((int)qstrlen(buf) != device->write(buf, qstrlen(buf))) { delete [] buf; return false; } delete [] buf; return true; }
static bool write_pbm_image(QIODevice *out, const QImage &sourceImage, const QByteArray &sourceFormat) { QByteArray str; QImage image = sourceImage; QByteArray format = sourceFormat; format = format.left(3); // ignore RAW part bool gray = format == "pgm"; if (format == "pbm") { image = image.convertToFormat(QImage::Format_Mono); } else if (gray) { image = image.convertToFormat(QImage::Format_Grayscale8); } else { switch (image.format()) { case QImage::Format_Mono: case QImage::Format_MonoLSB: image = image.convertToFormat(QImage::Format_Indexed8); break; case QImage::Format_Indexed8: case QImage::Format_RGB32: case QImage::Format_ARGB32: break; default: if (image.hasAlphaChannel()) image = image.convertToFormat(QImage::Format_ARGB32); else image = image.convertToFormat(QImage::Format_RGB32); break; } } if (image.depth() == 1 && image.colorCount() == 2) { if (qGray(image.color(0)) < qGray(image.color(1))) { // 0=dark/black, 1=light/white - invert image.detach(); for (int y=0; y<image.height(); y++) { uchar *p = image.scanLine(y); uchar *end = p + image.bytesPerLine(); while (p < end) *p++ ^= 0xff; } } } uint w = image.width(); uint h = image.height(); str = "P\n"; str += QByteArray::number(w); str += ' '; str += QByteArray::number(h); str += '\n'; switch (image.depth()) { case 1: { str.insert(1, '4'); if (out->write(str, str.length()) != str.length()) return false; w = (w+7)/8; for (uint y=0; y<h; y++) { uchar* line = image.scanLine(y); if (w != (uint)out->write((char*)line, w)) return false; } } break; case 8: { str.insert(1, gray ? '5' : '6'); str.append("255\n"); if (out->write(str, str.length()) != str.length()) return false; uint bpl = w * (gray ? 1 : 3); uchar *buf = new uchar[bpl]; if (image.format() == QImage::Format_Indexed8) { QVector<QRgb> color = image.colorTable(); for (uint y=0; y<h; y++) { const uchar *b = image.constScanLine(y); uchar *p = buf; uchar *end = buf+bpl; if (gray) { while (p < end) { uchar g = (uchar)qGray(color[*b++]); *p++ = g; } } else { while (p < end) { QRgb rgb = color[*b++]; *p++ = qRed(rgb); *p++ = qGreen(rgb); *p++ = qBlue(rgb); } } if (bpl != (uint)out->write((char*)buf, bpl)) return false; } } else { for (uint y=0; y<h; y++) { const uchar *b = image.constScanLine(y); uchar *p = buf; uchar *end = buf + bpl; if (gray) { while (p < end) *p++ = *b++; } else { while (p < end) { uchar color = *b++; *p++ = color; *p++ = color; *p++ = color; } } if (bpl != (uint)out->write((char*)buf, bpl)) return false; } } delete [] buf; break; } case 32: { str.insert(1, '6'); str.append("255\n"); if (out->write(str, str.length()) != str.length()) return false; uint bpl = w * 3; uchar *buf = new uchar[bpl]; for (uint y=0; y<h; y++) { const QRgb *b = reinterpret_cast<const QRgb *>(image.constScanLine(y)); uchar *p = buf; uchar *end = buf+bpl; while (p < end) { QRgb rgb = *b++; *p++ = qRed(rgb); *p++ = qGreen(rgb); *p++ = qBlue(rgb); } if (bpl != (uint)out->write((char*)buf, bpl)) return false; } delete [] buf; break; } default: return false; } return true; }
bool Q_INTERNAL_WIN_NO_THROW QPNGImageWriter::writeImage(const QImage& image, volatile int quality_in, const QString &description, int off_x_in, int off_y_in) { QPoint offset = image.offset(); int off_x = off_x_in + offset.x(); int off_y = off_y_in + offset.y(); png_structp png_ptr; png_infop info_ptr; png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0); if (!png_ptr) { return false; } png_set_error_fn(png_ptr, 0, 0, qt_png_warning); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_write_struct(&png_ptr, 0); return false; } if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_write_struct(&png_ptr, &info_ptr); return false; } int quality = quality_in; if (quality >= 0) { if (quality > 9) { qWarning("PNG: Quality %d out of range", quality); quality = 9; } png_set_compression_level(png_ptr, quality); } png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn); int color_type = 0; if (image.colorCount()) { if (image.isGrayscale()) color_type = PNG_COLOR_TYPE_GRAY; else color_type = PNG_COLOR_TYPE_PALETTE; } else if (image.format() == QImage::Format_Grayscale8) color_type = PNG_COLOR_TYPE_GRAY; else if (image.hasAlphaChannel()) color_type = PNG_COLOR_TYPE_RGB_ALPHA; else color_type = PNG_COLOR_TYPE_RGB; png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(), image.depth() == 1 ? 1 : 8, // per channel color_type, 0, 0, 0); // sets #channels if (gamma != 0.0) { png_set_gAMA(png_ptr, info_ptr, 1.0/gamma); } if (image.format() == QImage::Format_MonoLSB) png_set_packswap(png_ptr); if (color_type == PNG_COLOR_TYPE_PALETTE) { // Paletted int num_palette = qMin(256, image.colorCount()); png_color palette[256]; png_byte trans[256]; int num_trans = 0; for (int i=0; i<num_palette; i++) { QRgb rgba=image.color(i); palette[i].red = qRed(rgba); palette[i].green = qGreen(rgba); palette[i].blue = qBlue(rgba); trans[i] = qAlpha(rgba); if (trans[i] < 255) { num_trans = i+1; } } png_set_PLTE(png_ptr, info_ptr, palette, num_palette); if (num_trans) { png_set_tRNS(png_ptr, info_ptr, trans, num_trans, 0); } } // Swap ARGB to RGBA (normal PNG format) before saving on // BigEndian machines if (QSysInfo::ByteOrder == QSysInfo::BigEndian) { png_set_swap_alpha(png_ptr); } // Qt==ARGB==Big(ARGB)==Little(BGRA). But RGB888 is RGB regardless if (QSysInfo::ByteOrder == QSysInfo::LittleEndian && image.format() != QImage::Format_RGB888) { png_set_bgr(png_ptr); } if (off_x || off_y) { png_set_oFFs(png_ptr, info_ptr, off_x, off_y, PNG_OFFSET_PIXEL); } if (frames_written > 0) png_set_sig_bytes(png_ptr, 8); if (image.dotsPerMeterX() > 0 || image.dotsPerMeterY() > 0) { png_set_pHYs(png_ptr, info_ptr, image.dotsPerMeterX(), image.dotsPerMeterY(), PNG_RESOLUTION_METER); } set_text(image, png_ptr, info_ptr, description); png_write_info(png_ptr, info_ptr); if (image.depth() != 1) png_set_packing(png_ptr); if (color_type == PNG_COLOR_TYPE_RGB && image.format() != QImage::Format_RGB888) png_set_filler(png_ptr, 0, QSysInfo::ByteOrder == QSysInfo::BigEndian ? PNG_FILLER_BEFORE : PNG_FILLER_AFTER); if (looping >= 0 && frames_written == 0) { uchar data[13] = "NETSCAPE2.0"; // 0123456789aBC data[0xB] = looping%0x100; data[0xC] = looping/0x100; png_write_chunk(png_ptr, const_cast<png_bytep>((const png_byte *)"gIFx"), data, 13); } if (ms_delay >= 0 || disposal!=Unspecified) { uchar data[4]; data[0] = disposal; data[1] = 0; data[2] = (ms_delay/10)/0x100; // hundredths data[3] = (ms_delay/10)%0x100; png_write_chunk(png_ptr, const_cast<png_bytep>((const png_byte *)"gIFg"), data, 4); } int height = image.height(); int width = image.width(); switch (image.format()) { case QImage::Format_Mono: case QImage::Format_MonoLSB: case QImage::Format_Indexed8: case QImage::Format_Grayscale8: case QImage::Format_RGB32: case QImage::Format_ARGB32: case QImage::Format_RGB888: { png_bytep* row_pointers = new png_bytep[height]; for (int y=0; y<height; y++) row_pointers[y] = const_cast<png_bytep>(image.constScanLine(y)); png_write_image(png_ptr, row_pointers); delete [] row_pointers; } break; default: { QImage::Format fmt = image.hasAlphaChannel() ? QImage::Format_ARGB32 : QImage::Format_RGB32; QImage row; png_bytep row_pointers[1]; for (int y=0; y<height; y++) { row = image.copy(0, y, width, 1).convertToFormat(fmt); row_pointers[0] = const_cast<png_bytep>(row.constScanLine(0)); png_write_rows(png_ptr, row_pointers, 1); } } break; } png_write_end(png_ptr, info_ptr); frames_written++; png_destroy_write_struct(&png_ptr, &info_ptr); return true; }
void ColorPaletteSwatchArea::paintEvent(QPaintEvent*) { Image *currentImage = ImageArea::getCurrentImage(); if (!currentImage) { QPainter painter(this); painter.setPen(QPen(Qt::lightGray)); painter.setBrush(QBrush(Qt::lightGray)); painter.drawRect(0, 0, width(), height()); painter.setPen(QPen(Qt::black)); painter.setBrush(QBrush(Qt::black)); painter.drawLine(0, 0, width(), height()); painter.drawLine(width(), 0, 0, height()); return; } QImage *image = currentImage->getImage(); assert(image); const int numColorSwatchesXY = sqrt(image->colorCount()); const float w = width() / numColorSwatchesXY; const float h = height() / numColorSwatchesXY; QPainter painter(this); painter.setPen(QPen(Qt::black)); painter.setBrush(QBrush(Qt::black)); painter.drawRect(0, 0, width(), height()); for (int y = 0; y < numColorSwatchesXY; ++y) { for (int x = 0; x < numColorSwatchesXY; ++x) { const unsigned int index = x + numColorSwatchesXY * y; QColor color = image->color(index); painter.setPen(QPen(color)); painter.setBrush(QBrush(color)); painter.drawRect(x * w, y * h, w, h); if (index == currentImage->getSelectedColorIndex()) { painter.setBrush(QBrush(Qt::transparent)); painter.setPen(QPen(Qt::white)); painter.drawRect(x * w, y * h, w - 1, h - 1); painter.setPen(QPen(Qt::black)); painter.drawRect(x * w + 1, y * h + 1, w - 3, h - 3); } short hotkeyGroupForColorIndex = currentImage->getHotkeyGroupForColorIndex(index); if (hotkeyGroupForColorIndex >= 0) { const unsigned int numberX = x * w + 2; const unsigned int numberY = (y + 1) * h - 2; if (color.redF() + color.greenF() + color.blueF() > 1.5f) // bright color // so write text in black painter.setPen(QPen(Qt::black)); else // dark color // write text in white painter.setPen(QPen(Qt::white)); painter.drawText(QPoint(numberX, numberY), QString::number(hotkeyGroupForColorIndex)); } } } }
QPixmap QPixmap::fromImage(const QImage &img, Qt::ImageConversionFlags flags) { QPixmap pixmap; if(img.isNull()) { qWarning("QPixmap::convertFromImage: Cannot convert a null image"); return pixmap; } QImage image = img; int d = image.depth(); int dd = defaultDepth(); bool force_mono = (dd == 1 || (flags & Qt::ColorMode_Mask)==Qt::MonoOnly); if(force_mono) { // must be monochrome if(d != 1) { image = image.convertToFormat(QImage::Format_MonoLSB, flags); // dither d = 1; } } else { // can be both bool conv8 = false; if(d > 8 && dd <= 8) { // convert to 8 bit if((flags & Qt::DitherMode_Mask) == Qt::AutoDither) flags = (flags & ~Qt::DitherMode_Mask) | Qt::PreferDither; conv8 = true; } else if((flags & Qt::ColorMode_Mask) == Qt::ColorOnly) { conv8 = d == 1; // native depth wanted } else if(d == 1) { if(image.numColors() == 2) { QRgb c0 = image.color(0); // Auto: convert to best QRgb c1 = image.color(1); conv8 = qMin(c0,c1) != qRgb(0,0,0) || qMax(c0,c1) != qRgb(255,255,255); } else { // eg. 1-color monochrome images (they do exist). conv8 = true; } } if(conv8) { image = image.convertToFormat(QImage::Format_Indexed8, flags); d = 8; } } if(image.depth()==1) { image.setColor(0, QColor(Qt::color0).rgba()); image.setColor(1, QColor(Qt::color1).rgba()); } if (d == 16) { QImage im = image.convertToFormat(QImage::Format_RGB32, flags); return fromImage(im); } int w = image.width(); int h = image.height(); // different size or depth, make a new pixmap if (d == 1) pixmap = QBitmap(w, h); else pixmap = QPixmap(w, h); quint32 *dptr = pixmap.data->pixels, *drow; const uint dbpr = pixmap.data->nbytes / h; const QImage::Format sfmt = image.format(); const unsigned short sbpr = image.bytesPerLine(); uchar *sptr = image.bits(), *srow; for(int y=0;y<h;y++) { drow = dptr + (y * (dbpr / 4)); srow = sptr + (y * sbpr); switch(sfmt) { case QImage::Format_MonoLSB: case QImage::Format_Mono:{ for(int x=0;x<w;++x) { char one_bit = *(srow + (x / 8)); if(sfmt == QImage::Format_Mono) one_bit = one_bit >> (7 - (x % 8)); else one_bit = one_bit >> (x % 8); if((one_bit & 0x01)) *(drow+x) = 0x00000000; else *(drow+x) = 0xFFFFFFFF; } break; } case QImage::Format_Indexed8: for(int x=0;x<w;++x) { *(drow+x) = PREMUL(image.color(*(srow + x))); } break; case QImage::Format_RGB32: for(int x=0;x<w;++x) *(drow+x) = *(((quint32*)srow) + x) | 0xFF000000; break; case QImage::Format_ARGB32: case QImage::Format_ARGB32_Premultiplied: for(int x=0;x<w;++x) { if(sfmt == QImage::Format_RGB32) *(drow+x) = 0xFF000000 | (*(((quint32*)srow) + x) & 0x00FFFFFF); else if(sfmt == QImage::Format_ARGB32_Premultiplied) *(drow+x) = *(((quint32*)srow) + x); else *(drow+x) = PREMUL(*(((quint32*)srow) + x)); } break; default: qWarning("Qt: internal: Oops: Forgot a format [%d] %s:%d", sfmt, __FILE__, __LINE__); break; } }