imgtool_stream *stream_open_write_stream(int size) { imgtool_stream::ptr imgfile(new imgtool_stream(false, size)); if (!imgfile->buffer) return nullptr; return imgfile.release(); }
imgtool_stream *stream_open(const char *fname, int read_or_write) { static const UINT32 write_modes[] = { OPEN_FLAG_READ, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, OPEN_FLAG_READ | OPEN_FLAG_WRITE, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE }; imgtool_stream *s = nullptr; char c; /* maybe we are just a ZIP? */ const char *ext = strrchr(fname, '.'); if (ext && !core_stricmp(ext, ".zip")) return stream_open_zip(fname, nullptr, read_or_write); util::core_file::ptr f = nullptr; auto const filerr = util::core_file::open(fname, write_modes[read_or_write], f); if (filerr != osd_file::error::NONE) { if (!read_or_write) { int const len = strlen(fname); /* can't open the file; try opening ZIP files with other names */ std::vector<char> buf(len + 1); strcpy(&buf[0], fname); for (int i = len-1; !s && (i >= 0); i--) { if ((buf[i] == '\\') || (buf[i] == '/')) { c = buf[i]; buf[i] = '\0'; s = stream_open_zip(&buf[0], &buf[i + 1], read_or_write); buf[i] = c; } } if (s) return s; } /* ah well, it was worth a shot */ return nullptr; } imgtool_stream::ptr imgfile(new imgtool_stream(read_or_write ? false : true, std::move(f))); /* Normal file */ imgfile->name = fname; return imgfile.release(); }
static imgtool_stream *stream_open_zip(const char *zipname, const char *subname, int read_or_write) { if (read_or_write) return nullptr; /* check to see if the file exists */ FILE *f = fopen(zipname, "r"); if (!f) return nullptr; fclose(f); imgtool_stream::ptr imgfile(new imgtool_stream(true)); imgfile->imgtype = IMG_MEM; zip_file *z = nullptr; const zip_file_header *zipent = nullptr; zip_file_open(zipname, &z); if (!z) goto error; zipent = zip_file_first_file(z); while (zipent && subname && strcmp(subname, zipent->filename)) zipent = zip_file_next_file(z); if (!zipent) goto error; imgfile->filesize = zipent->uncompressed_length; imgfile->buffer = reinterpret_cast<std::uint8_t *>(malloc(zipent->uncompressed_length)); if (!imgfile->buffer) goto error; if (zip_file_decompress(z, imgfile->buffer, zipent->uncompressed_length)) goto error; zip_file_close(z); return imgfile.release(); error: if (z) zip_file_close(z); return nullptr; }
void MainWindow::Open() { QString filepath = QFileDialog::getOpenFileName(NULL, "Dump screen file"); if (!filepath.isEmpty()) { QFile imgfile(filepath); if (!imgfile.open(QIODevice::ReadOnly)) { return; } QByteArray imgData = imgfile.readAll(); if (settings.exec() != QDialog::Accepted) { return; } const int expectedSize = settings.GetWidth() * settings.GetHeight() * 4; if (imgData.size() != expectedSize) { if (QMessageBox::information(this, QApplication::applicationName(), "WARNING: Size of the file and image format settings do not match, ignore?", QMessageBox::Ignore | QMessageBox::Cancel) != QMessageBox::Ignore) { return; } } QImage image(settings.GetWidth(), settings.GetHeight(), settings.GetImageFormat()); memcpy(image.bits(), imgData.data(), expectedSize < imgData.size() ? expectedSize : imgData.size()); m_image = image; ui->label->setPixmap(QPixmap::fromImage(m_image)); ui->label->setScaledContents(true); } }
static imgtool_stream *stream_open_zip(const char *zipname, const char *subname, int read_or_write) { if (read_or_write) return nullptr; /* check to see if the file exists */ FILE *f = fopen(zipname, "r"); if (!f) return nullptr; fclose(f); imgtool_stream::ptr imgfile(new imgtool_stream(true)); imgfile->imgtype = IMG_MEM; util::archive_file::ptr z; util::archive_file::open_zip(zipname, z); if (!z) return nullptr; int zipent = z->first_file(); while ((zipent >= 0) && subname && strcmp(subname, z->current_name().c_str())) zipent = z->next_file(); if (zipent < 0) return nullptr; imgfile->filesize = z->current_uncompressed_length(); imgfile->buffer = reinterpret_cast<std::uint8_t *>(malloc(z->current_uncompressed_length())); if (!imgfile->buffer) return nullptr; if (z->decompress(imgfile->buffer, z->current_uncompressed_length()) != util::archive_file::error::NONE) return nullptr; return imgfile.release(); }
imgtool_stream *stream_open_mem(void *buf, size_t sz) { imgtool_stream::ptr imgfile(new imgtool_stream(false, sz, buf)); return imgfile.release(); }