QPixmap SxButton::recolorPixmap(const QPixmap& pixmap, int _r, int _g, int _b) { QImage image = pixmap.toImage(); QImage alpha = image.alphaChannel(); for (int y = 0; y < image.height(); y++) { for (int x = 0; x < image.width(); x++) { QRgb color = image.pixel(x,y); int r = qRed(color); int g = qGreen(color); int b = qBlue(color); r+= _r; g+= _g; b+= _b; if (r<0) r=0; else if (r>=256) r=255; if (g<0) g=0; else if (g>=256) g=255; if (b<0) b=0; else if (b>=256) b=255; color = QColor(r,g,b).rgb(); image.setPixel(x,y,color); } } image.setAlphaChannel(alpha); return QPixmap::fromImage(image); }
QImage IconUtils::tinted(const QImage &image, const QColor &color, QPainter::CompositionMode mode) { QImage img(image.size(), QImage::Format_ARGB32_Premultiplied); QPainter painter(&img); painter.drawImage(0, 0, grayscaled(image)); painter.setCompositionMode(mode); painter.fillRect(img.rect(), color); painter.end(); img.setAlphaChannel(image.alphaChannel()); return img; }
void ImageCalibrator::on_WriteGIF_clicked() { QImage target = generateTarget().toImage(); QImage mask = target.alphaChannel(); mask.invertPixels(); Graphics::toGif(target, gifPath); Graphics::toGif(mask, gifPathM); saveCalibrates(); QMessageBox::information(this, "Image was overwritten", "Calibrated sprite was saved in:\n"+gifPath, QMessageBox::Ok); }
Car::Car(const QColor &color) : startPoint(0,0), targetPoint(0,0), targetCellCol(0), targetCellRow(0), targetDirection(0), progress(0), progressDelta(10), _direction(-1) { QPixmap carPix; QImage carImg = QImage(":/images/red-car.png"); QImage alphaChannel = carImg.alphaChannel(); carImg.fill(color); carImg.setAlphaChannel(alphaChannel); carPix.convertFromImage(carImg); setPixmap(carPix); setTransformOriginPoint( carPix.rect().center() ); setFlag(ItemSendsGeometryChanges); }
//Implementation of VB similar transparency function QImage setAlphaMask_VB(QImage image, QImage mask) { if(mask.isNull()) return image; if(image.isNull()) return image; bool isWhiteMask = true; QImage target; target = QImage(image.width(), image.height(), QImage::Format_ARGB32); target.fill(qRgb(128,128,128)); QImage newmask = mask; target.convertToFormat(QImage::Format_ARGB32); if(target.size()!= newmask.size()) { newmask = newmask.copy(0, 0, target.width(), target.height()); } QImage alphaChannel = image.alphaChannel(); //vbSrcAnd for(int y=0; y< image.height(); y++ ) for(int x=0; x < image.width(); x++ ) { QColor Dpix = QColor(target.pixel(x,y)); QColor Spix = QColor(newmask.pixel(x,y)); QColor Npix; Npix.setAlpha(255); Npix.setRed( Dpix.red() & Spix.red()); Npix.setGreen( Dpix.green() & Spix.green()); Npix.setBlue( Dpix.blue() & Spix.blue()); target.setPixel(x, y, Npix.rgba()); isWhiteMask &= ( (Spix.red()>240) //is almost White &&(Spix.green()>240) &&(Spix.blue()>240)); int newAlpha = 255-((Spix.red() + Spix.green() + Spix.blue())/3); if( (Spix.red()>240) //is almost White &&(Spix.green()>240) &&(Spix.blue()>240)) { newAlpha = 0; } alphaChannel.setPixel(x,y, newAlpha); } //vbSrcPaint for(int y=0; y< image.height(); y++ ) for(int x=0; x < image.width(); x++ ) { QColor Dpix = QColor(image.pixel(x,y)); QColor Spix = QColor(target.pixel(x,y)); QColor Npix; Npix.setAlpha(255); Npix.setRed( Dpix.red() | Spix.red()); Npix.setGreen( Dpix.green() | Spix.green()); Npix.setBlue( Dpix.blue() | Spix.blue()); target.setPixel(x, y, Npix.rgba()); //QColor curAlpha; int curAlpha = QColor(alphaChannel.pixel(x,y)).red(); int newAlpha = curAlpha+((Dpix.red() + Dpix.green() + Dpix.blue())/3); if(newAlpha>255) newAlpha=255; alphaChannel.setPixel(x,y, newAlpha); } target.setAlphaChannel(alphaChannel); return target; }
void doMagicIn(QString path, QString q, QString OPath) { QRegExp isMask = QRegExp("*m.gif"); isMask.setPatternSyntax(QRegExp::Wildcard); QRegExp isBackupDir = QRegExp("*/_backup/"); isBackupDir.setPatternSyntax(QRegExp::Wildcard); if(isBackupDir.exactMatch(path)) return; //Skip backup directories if(isMask.exactMatch(q)) return; QImage target; QString imgFileM; QStringList tmp = q.split(".", QString::SkipEmptyParts); if(tmp.size()==2) imgFileM = tmp[0] + "m." + tmp[1]; else return; //skip unexists pairs if(!QFile(path+q).exists()) return; if(!QFile(path+imgFileM).exists()) { QString saveTo; QImage image = loadQImage(path+q); if(image.isNull()) return; QTextStream(stdout) << QString(path+q+"\n").toUtf8().data(); saveTo = QString(OPath+(tmp[0].toLower())+".gif"); //overwrite source image (convert BMP to GIF) if(toGif( image, saveTo ) ) //Write gif { QTextStream(stdout) <<"GIF-1 only\n"; } else { QTextStream(stdout) <<"BMP-1 only\n"; image.save(saveTo, "BMP"); //If failed, write BMP } return; } if(!noBackUp) { //create backup dir QDir backup(path+"_backup/"); if(!backup.exists()) { QTextStream(stdout) << QString("Create backup with path %1\n").arg(path+"_backup"); if(!backup.mkdir(".")) QTextStream(stderr) << QString("WARNING! Can't create backup directory %1\n").arg(path+"_backup"); } //create Back UP of source images if(!QFile(path+"_backup/"+q).exists()) QFile::copy(path+q, path+"_backup/"+q); if(!QFile(path+"_backup/"+imgFileM).exists()) QFile::copy(path+imgFileM, path+"_backup/"+imgFileM); } QImage image = loadQImage(path+q); QImage mask = loadQImage(path+imgFileM); if(mask.isNull()) //Skip null masks return; target = setAlphaMask(image, mask); if(!target.isNull()) { //Save before fix //target.save(OPath+tmp[0]+"_before.png"); //mask.save(OPath+tmp[0]+"_mask_before.png"); QTextStream(stdout) << QString(path+q+"\n").toUtf8().data(); //fix if(image.size()!= mask.size()) mask = mask.copy(0,0, image.width(), image.height()); mask = target.alphaChannel(); mask.invertPixels(); //Save after fix //target.save(OPath+tmp[0]+"_after.bmp", "BMP"); QString saveTo; saveTo = QString(OPath+(tmp[0].toLower())+".gif"); //overwrite source image (convert BMP to GIF) if(toGif(image, saveTo ) ) //Write gif { QTextStream(stdout) <<"GIF-1 "; } else { QTextStream(stdout) <<"BMP-1 "; image.save(saveTo, "BMP"); //If failed, write BMP } saveTo = QString(OPath+(tmp[0].toLower())+"m.gif"); //overwrite mask image if( toGif(mask, saveTo ) ) //Write gif { QTextStream(stdout) <<"GIF-2\n"; } else { mask.save(saveTo, "BMP"); //If failed, write BMP QTextStream(stdout) <<"BMP-2\n"; } } else QTextStream(stderr) << path+q+" - WRONG!\n"; }
void QImageTextureGlyphCache::fillTexture(const Coord &c, glyph_t g, QFixed subPixelPosition) { QImage mask = textureMapForGlyph(g, subPixelPosition); #ifdef CACHE_DEBUG printf("fillTexture of %dx%d at %d,%d in the cache of %dx%d\n", c.w, c.h, c.x, c.y, m_image.width(), m_image.height()); if (mask.width() > c.w || mask.height() > c.h) { printf(" ERROR; mask is bigger than reserved space! %dx%d instead of %dx%d\n", mask.width(), mask.height(), c.w,c.h); return; } #endif if (m_type == QFontEngineGlyphCache::Raster_RGBMask) { QImage ref(m_image.bits() + (c.x * 4 + c.y * m_image.bytesPerLine()), qMax(mask.width(), c.w), qMax(mask.height(), c.h), m_image.bytesPerLine(), m_image.format()); QPainter p(&ref); p.setCompositionMode(QPainter::CompositionMode_Source); p.fillRect(0, 0, c.w, c.h, QColor(0,0,0,0)); // TODO optimize this p.drawImage(0, 0, mask); p.end(); } else if (m_type == QFontEngineGlyphCache::Raster_Mono) { if (mask.depth() > 1) { // TODO optimize this mask = mask.alphaChannel(); mask.invertPixels(); mask = mask.convertToFormat(QImage::Format_Mono); } int mw = qMin(mask.width(), c.w); int mh = qMin(mask.height(), c.h); uchar *d = m_image.bits(); int dbpl = m_image.bytesPerLine(); for (int y = 0; y < c.h; ++y) { uchar *dest = d + (c.y + y) *dbpl + c.x/8; if (y < mh) { uchar *src = mask.scanLine(y); for (int x = 0; x < c.w/8; ++x) { if (x < (mw+7)/8) dest[x] = src[x]; else dest[x] = 0; } } else { for (int x = 0; x < c.w/8; ++x) dest[x] = 0; } } } else { // A8 int mw = qMin(mask.width(), c.w); int mh = qMin(mask.height(), c.h); uchar *d = m_image.bits(); int dbpl = m_image.bytesPerLine(); if (mask.depth() == 1) { for (int y = 0; y < c.h; ++y) { uchar *dest = d + (c.y + y) *dbpl + c.x; if (y < mh) { uchar *src = (uchar *) mask.scanLine(y); for (int x = 0; x < c.w; ++x) { if (x < mw) dest[x] = (src[x >> 3] & (1 << (7 - (x & 7)))) > 0 ? 255 : 0; } } } } else if (mask.depth() == 8) {
bool ImageHandler::getSessionItemImage(SessionItem* pItem, QBuffer& buffer, const QString& format, int band, int* pBbox) { if (format.isEmpty()) { return false; } bool success = true; QImage image; Layer* pLayer = dynamic_cast<Layer*>(pItem); View* pView = dynamic_cast<View*>(pItem); if (pLayer != NULL) { SpatialDataView* pSDView = dynamic_cast<SpatialDataView*>(pLayer->getView()); if (pSDView != NULL) { UndoLock ulock(pSDView); DimensionDescriptor cur; DisplayMode mode; RasterLayer* pRasterLayer = dynamic_cast<RasterLayer*>(pLayer); if (band >= 0 && pRasterLayer != NULL) { RasterElement* pRaster = pRasterLayer->getDisplayedRasterElement(GRAY); DimensionDescriptor bandDesc = static_cast<RasterDataDescriptor*>(pRaster->getDataDescriptor())->getActiveBand(band); cur = pRasterLayer->getDisplayedBand(GRAY); mode = pRasterLayer->getDisplayMode(); pRasterLayer->setDisplayedBand(GRAY, bandDesc); pRasterLayer->setDisplayMode(GRAYSCALE_MODE); } int bbox[4] = {0, 0, 0, 0}; ColorType transparent(255, 255, 254); success = pSDView->getLayerImage(pLayer, image, transparent, bbox); if (pBbox != NULL) { memcpy(pBbox, bbox, sizeof(bbox)); } QImage alphaChannel(image.size(), QImage::Format_Indexed8); if (image.hasAlphaChannel()) { alphaChannel = image.alphaChannel(); } else { alphaChannel.fill(0xff); } QRgb transColor = COLORTYPE_TO_QCOLOR(transparent).rgb(); for (int y = 0; y < image.height(); y++) { for (int x = 0; x < image.width(); x++) { if (image.pixel(x, y) == transColor) { alphaChannel.setPixel(x, y, 0x00); } } } image.setAlphaChannel(alphaChannel); if (mode.isValid()) { pRasterLayer->setDisplayedBand(GRAY, cur); pRasterLayer->setDisplayMode(mode); } } } else if (pView != NULL) { success = pView->getCurrentImage(image); } else { success = false; } if (success) { buffer.open(QIODevice::WriteOnly); QImageWriter writer(&buffer, format.toAscii()); success = writer.write(image); } return success; }
int doMagicIn(QString path, QString q, QString OPath) { //skip unexists pairs if(!QFile(path+q).exists()) return CNV_SKIPPED; QImage ImgSrc; QImage image; QImage mask; ImgSrc = loadQImage(path+q); if(ImgSrc.isNull()) //Skip null images return CNV_FAILED; QString bname = QFileInfo(path+q).baseName(); QString saveToImg = QString(OPath+(bname.toLower())+".gif"); QString saveToMask = QString(OPath+(bname.toLower())+"m.gif"); QTextStream(stdout) << path+q+"\n"; mask = ImgSrc.alphaChannel(); mask.invertPixels(); //Write mask image if( toGif(mask, saveToMask ) ) //Write gif { QTextStream(stdout) <<"GIF-1\n"; } else { mask.save(saveToMask, "BMP"); //If failed, write BMP QTextStream(stdout) <<"BMP-1\n"; } image = ImgSrc.convertToFormat(QImage::Format_RGB32); mask = ImgSrc.alphaChannel(); mask.invertPixels(); for(int w=0; w < mask.width(); w++) for(int h=0; h < mask.height(); h++) { if(mask.pixel(w,h)==qRgb(0xFF,0xFF,0xFF)) //Fill white pixel into black on image { image.setPixel(w,h, qRgb(0,0,0)); } else if(mask.pixel(w,h)!=qRgb(0,0,0)) //Fill non-black pixel color into black on mask { mask.setPixel(w,h, qGray(0,0,0)); } } mask.invertPixels(); image.setAlphaChannel(mask); //Write mask image if( toGif(image, saveToImg ) ) //Write gif { QTextStream(stdout) <<"GIF-2\n"; } else { image.save(saveToImg, "BMP"); //If failed, write BMP QTextStream(stdout) <<"BMP-2\n"; } if(removeSource) QFile(path+q).remove(); return CNV_SUCCESS; }
static MythImage *LoadImage(MythPainter *painter, // Must be a copy for thread safety ImageProperties imProps, ImageCacheMode cacheMode, // Included only to check address, could be // replaced by generating a unique value for // each MythUIImage object? const MythUIImage *parent, bool &aborted, MythImageReader *imageReader = NULL) { QString cacheKey = GenImageLabel(imProps); if (!PreLoad(cacheKey, parent)) { aborted = true; return NULL; } QString filename = imProps.filename; MythImage *image = NULL; bool bResize = false; bool bFoundInCache = false; int w = -1; int h = -1; if (!imProps.forceSize.isNull()) { if (imProps.forceSize.width() != -1) w = imProps.forceSize.width(); if (imProps.forceSize.height() != -1) h = imProps.forceSize.height(); bResize = true; } if (!imageReader) { image = GetMythUI()->LoadCacheImage(filename, cacheKey, painter, cacheMode); } if (image) { if (VERBOSE_LEVEL_CHECK(VB_GUI | VB_FILE, LOG_INFO)) { image->IncrRef(); int cnt = image->DecrRef(); LOG(VB_GUI | VB_FILE, LOG_INFO, QString("ImageLoader::LoadImage(%1) Found in cache, " "RefCount = %2") .arg(cacheKey).arg(cnt)); } if (imProps.isReflected) image->setIsReflected(true); if (imProps.isOriented) image->setIsOriented(true); bFoundInCache = true; } else { LOG(VB_GUI | VB_FILE, LOG_INFO, QString("ImageLoader::LoadImage(%1) NOT Found in cache. " "Loading Directly").arg(cacheKey)); image = painter->GetFormatImage(); bool ok = false; if (imageReader) ok = image->Load(imageReader); else ok = image->Load(filename); if (!ok) { image->DecrRef(); image = NULL; } } if (image && image->isNull()) { LOG(VB_GUI | VB_FILE, LOG_INFO, QString("ImageLoader::LoadImage(%1) Image is NULL") .arg(filename)); image->DecrRef(); image = NULL; } if (image && !bFoundInCache) { if (imProps.isReflected) image->Reflect(imProps.reflectAxis, imProps.reflectShear, imProps.reflectScale, imProps.reflectLength, imProps.reflectSpacing); if (imProps.isGreyscale) image->ToGreyscale(); if (imProps.isOriented) image->Orientation(imProps.orientation); // Even if an explicit size wasn't defined this image may still need // to be scaled because of a difference between the theme resolution // and the screen resolution. We want to avoid scaling twice. if (!bResize && imProps.isThemeImage) { float wmult; // Width multipler float hmult; // Height multipler GetMythUI()->GetScreenSettings(wmult, hmult); if (wmult != 1.0f || hmult != 1.0f) { w = image->size().width() * wmult; h = image->size().height() * hmult; bResize = true; } } if (bResize) image->Resize(QSize(w, h), imProps.preserveAspect); if (imProps.isMasked) { QRect imageArea = image->rect(); QRect maskArea = imProps.GetMaskImageRect(); // Crop the mask to the image int x = 0; int y = 0; if (maskArea.width() > imageArea.width()) x = (maskArea.width() - imageArea.width()) / 2; if (maskArea.height() > imageArea.height()) y = (maskArea.height() - imageArea.height()) / 2; if (x > 0 || y > 0) imageArea.translate(x, y); QImage mask = imProps.GetMaskImageSubset(imageArea); image->setAlphaChannel(mask.alphaChannel()); } if (!imageReader) GetMythUI()->CacheImage(cacheKey, image); } if (image) image->SetChanged(); PostLoad(cacheKey); return image; }
/** * \brief Load an image */ MythImage *MythUIImage::LoadImage( MythImageReader &imageReader, const QString &imFile, QSize bForceSize, int cacheMode) { QString filename = imFile; m_loadingImagesLock.lock(); // Check to see if the image is being loaded by us in another thread if ((m_loadingImages.contains(filename)) && (m_loadingImages[filename] == this)) { VERBOSE(VB_GUI|VB_FILE|VB_EXTRA, LOC + QString( "MythUIImage::LoadImage(%1), this " "file is already being loaded by this same MythUIImage in " "another thread.").arg(filename)); m_loadingImagesLock.unlock(); return NULL; } // Check to see if the exact same image is being loaded anywhere else while (m_loadingImages.contains(filename)) m_loadingImagesCond.wait(&m_loadingImagesLock); m_loadingImages[filename] = this; m_loadingImagesLock.unlock(); VERBOSE(VB_GUI|VB_FILE, LOC + QString("LoadImage(%2) Object %3") .arg(filename).arg(objectName())); MythImage *image = NULL; bool bForceResize = false; bool bFoundInCache = false; QString imagelabel; int w = -1; int h = -1; if (!bForceSize.isNull()) { if (bForceSize.width() != -1) w = bForceSize.width(); if (bForceSize.height() != -1) h = bForceSize.height(); bForceResize = true; } imagelabel = GenImageLabel(filename, w, h); if (!imageReader.supportsAnimation()) { image = GetMythUI()->LoadCacheImage( filename, imagelabel, GetPainter(), (ImageCacheMode) cacheMode); } if (image) { image->UpRef(); VERBOSE(VB_GUI|VB_FILE, LOC + QString("LoadImage found in cache :%1: RefCount = %2") .arg(imagelabel).arg(image->RefCount())); if (m_isReflected) image->setIsReflected(true); bFoundInCache = true; } else { VERBOSE(VB_GUI|VB_FILE, LOC + QString("LoadImage Not Found in cache. " "Loading Directly :%1:").arg(filename)); image = GetPainter()->GetFormatImage(); image->UpRef(); bool ok = false; if (imageReader.supportsAnimation()) ok = image->Load(imageReader); else ok = image->Load(filename); if (!ok) { image->DownRef(); m_loadingImagesLock.lock(); m_loadingImages.remove(filename); m_loadingImagesCond.wakeAll(); m_loadingImagesLock.unlock(); return NULL; } } if (!bFoundInCache) { if (bForceResize) image->Resize(QSize(w, h), m_preserveAspect); if (m_isMasked) { QRect imageArea = image->rect(); QRect maskArea = m_maskImage->rect(); // Crop the mask to the image int x = 0; int y = 0; if (maskArea.width() > imageArea.width()) x = (maskArea.width() - imageArea.width()) / 2; if (maskArea.height() > imageArea.height()) y = (maskArea.height() - imageArea.height()) / 2; if (x > 0 || y > 0) imageArea.translate(x,y); d->m_UpdateLock.lockForWrite(); QImage mask = m_maskImage->copy(imageArea); d->m_UpdateLock.unlock(); image->setAlphaChannel(mask.alphaChannel()); } if (m_isReflected) image->Reflect(m_reflectAxis, m_reflectShear, m_reflectScale, m_reflectLength, m_reflectSpacing); if (m_isGreyscale) image->ToGreyscale(); if (!imageReader.supportsAnimation()) GetMythUI()->CacheImage(imagelabel, image); } if (image->isNull()) { VERBOSE(VB_GUI|VB_FILE, LOC + QString("LoadImage Image is NULL :%1:") .arg(filename)); image->DownRef(); Reset(); m_loadingImagesLock.lock(); m_loadingImages.remove(filename); m_loadingImagesCond.wakeAll(); m_loadingImagesLock.unlock(); return NULL; } image->SetChanged(); m_loadingImagesLock.lock(); m_loadingImages.remove(filename); m_loadingImagesCond.wakeAll(); m_loadingImagesLock.unlock(); return image; }