void maskRenderWidget::load(const QString &filename) { QImage alpha(filename); // I would have liked to use KeepAspectRatio but if someone loads a // bogus mask with a different ratio, the rest will crash. The output // is now undefined but it won't crash. alpha = alpha.scaled(pimpl_->foreground_.width(), pimpl_->foreground_.height(), Qt::IgnoreAspectRatio); QImage temp(pimpl_->foreground_); const int width(temp.width()), height(temp.height()); for (int i = 0; i < width; ++i) for (int j = 0; j < height; ++j) { QRgb rgb = temp.pixel(i, j); temp.setPixel(i, j, QColor(qRed(rgb), qGreen(rgb), qBlue(rgb), qGray(alpha.pixel(i, j))).rgba()); } setAlphaMask(temp); }
void doMagicIn(QString path, QString q, QString OPath, bool removeMode) { 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; QImage image = QImage(path+q); QImage mask = QImage(path+imgFileM); target = setAlphaMask(image, mask); if(!target.isNull()) { target.save(OPath+tmp[0]+".png"); QTextStream(stdout) << path+q <<"\n"; QTextStream(stdout) << OPath+tmp[0]+".png" <<"\n"; if(removeMode) { QFile::remove( path+q ); QFile::remove( path+imgFileM ); } } else QTextStream(stderr) << path+q+" - WRONG!\n"; }
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"; }