bool ParupaintPanvasInputOutput::savePPA(ParupaintPanvas * panvas, const QString & filename, QString & errorStr) { Q_ASSERT(panvas); if(filename.isEmpty()) return (errorStr = "Enter a filename to save to.").isEmpty(); KZip archive(filename); if(!archive.open(QIODevice::WriteOnly)) return (errorStr = "Failed open PPA for writing").isEmpty(); archive.writeFile("mimetype", "image/parupaint"); QImage mergedImage = panvas->mergedImage(true); archive.writeFile("Thumbnails/thumbnail.png", imageToByteArray(mergedImage.scaled(256, 256, Qt::KeepAspectRatio, Qt::SmoothTransformation))); archive.writeFile("mergedimage.png", imageToByteArray(mergedImage)); // write image files for(int l = 0; l < panvas->layerCount(); l++){ ParupaintLayer * layer = panvas->layerAt(l); for(int f = 0; f < layer->frameCount(); f++){ if(!layer->isFrameReal(f)) continue; ParupaintFrame * frame = layer->frameAt(f); QByteArray byte_array = imageToByteArray(frame->image()); QString frame_name = QString("%1/%2.png").arg(l).arg(layer->frameLabel(f)); archive.writeFile(frame_name, byte_array); } } // json info QJsonObject ppa_info = panvas->json(); QByteArray json = QJsonDocument(ppa_info).toJson(QJsonDocument::Indented); qDebug("%s", qPrintable(json)); archive.writeFile("pp3.json", json); archive.close(); return true; }
bool ParupaintPanvasInputOutput::exportZIPSequence(ParupaintPanvas * panvas, const QString & filename, QString & errorStr) { Q_ASSERT(panvas); if(filename.isEmpty()) return (errorStr = "Enter a filename to save to.").isEmpty(); KZip archive(filename); if(!archive.open(QIODevice::WriteOnly)) return (errorStr = "Failed open ZIP sequence for writing").isEmpty(); int frame = 0; foreach(const QImage & image, panvas->mergedImageFrames(true)){ QByteArray bytes = imageToByteArray(image); archive.writeFile(QString("%1/%2_%3.png").arg(QFileInfo(filename).baseName()).arg(QFileInfo(filename).baseName()).arg(frame, 3, 10, QChar('0')), bytes); frame++; } archive.close(); return true; }
void AsyncImageCreator::run() { // first we make sure we have an image QString cacheFileName; bool hasPreview = false; if (d->mImage.isNull() && d->mInputFileName.isEmpty() && (!d->mDataArray.isEmpty() || !d->mDataArray.isNull())) { d->mImage.loadFromData(d->mDataArray); cacheFileName = QCryptographicHash::hash(d->mDataArray, QCryptographicHash::Md5).toHex(); } else if (d->mImage.isNull() && !d->mInputFileName.isEmpty()) { cacheFileName = QCryptographicHash::hash(QByteArray().append(d->mInputFileName), QCryptographicHash::Md5).toHex(); d->mFileName = d->mInputFileName; // cache preview QString cachePreviewFilePath = QDir::toNativeSeparators( d->mStorePrefix + "/" + cacheFileName + "_preview.png"); QFile cachePreviewFile(cachePreviewFilePath); if (cachePreviewFile.exists()) { d->mThumbNail = QImage(cachePreviewFilePath); } else { d->mImage = QImage(d->mInputFileName); d->mThumbNail = d->mImage.scaled(d->mThumbNailSize, Qt::KeepAspectRatioByExpanding); d->saveFile(cachePreviewFilePath, d->mThumbNail); } hasPreview = true; } else { d->mDataArray = imageToByteArray(d->mImage); cacheFileName = QCryptographicHash::hash(d->mDataArray, QCryptographicHash::Md5).toHex(); } QString cacheFilePath = QDir::toNativeSeparators(d->mStorePrefix + "/" + cacheFileName + ".png"); if (d->mOffline && !d->mStorePrefix.isEmpty()) { d->saveFile(cacheFilePath, d->mImage); d->mFileName = cacheFilePath; } // create the thumbnail if (!hasPreview) d->mThumbNail = d->mImage.scaled(d->mThumbNailSize, Qt::KeepAspectRatioByExpanding); // scale operations if (d->mScaleHeight > 0) { d->mHeightScaledImage = d->mImage.scaledToHeight(d->mScaleHeight); } if (d->mScaleWidth > 0) { d->mWidthScaledImage = d->mImage.scaledToWidth(d->mScaleWidth); } if (d->mCropImage) { QImage imageToCrop = d->mImage.scaledToHeight(d->mCropRect.height()); d->mCropedImage = imageToCrop.copy(d->mCropRect); } Q_EMIT ready(); }
static QByteArray randomImageByteArray(int w=512, int h=512 ) { QImage image=randomImage(w,h); QByteArray ba=imageToByteArray(image); return ba; }