[英]How to convert a TIF file into a BMP file in C++98 using QT 4?
I am new in C++.我是 C++ 的新手。 How can I convert a TIF file into a BMP file in C++98?
如何在 C++98 中将 TIF 文件转换为 BMP 文件?
Here is my "newForm.h".Here is private slots for loading tiff and coverting the image.这是我的“newForm.h”。这是用于加载 tiff 和覆盖图像的私人插槽。
#ifndef _NEWFORM_H
#define _NEWFORM_H
#include <QMainWindow>
#include <QFileDialog>
#include <QDebug>
#include "ui_newForm.h"
QT_BEGIN_NAMESPACE
namespace Ui { class newForm; }
QT_END_NAMESPACE
class newForm : public QDialog {
Q_OBJECT
public:
newForm();
virtual ~newForm();
private slots:
void on_load_tiff_PushButton_clicked();
void on_bmp_PushButton_convert();
private:
QString mResourceDir;
Ui::newForm widget;
};
#endif /* _NEWFORM_H */
Here is my "main.cpp" in which i show the "newForm".这是我的“main.cpp”,其中显示了“newForm”。
#include <QApplication>
#include "newForm.h"
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
newForm a;
a.show();
// create and show your widgets here
return app.exec();
}
Here is "newForm.cpp".Here is on_load_tiff_PushButton_clicked which load tiff image.I want to convert tiff image into bitmap image using on_bitmap_PushButton_convert .how i can do that这是“ newForm.cpp ”。这是加载 tiff 图像的 on_load_tiff_PushButton_clicked。我想使用on_bitmap_PushButton_convert将 tiff 图像转换为 bitmap 图像。我该怎么做
#include "newForm.h"
#include <QMainWindow>
#include <QFileDialog>
#include <QDebug>
newForm::newForm() {
widget.setupUi(this);
mResourceDir = "/root/NetBeansProjects/TIF_TO_BMP/file.tiff";
connect(widget.pushButton, SIGNAL(clicked()), this, SLOT(on_load_tiff_PushButton_clicked()));
connect(widget.pushButton_2, SIGNAL(clicked()), this, SLOT(on_bitmap_PushButton_convert()));
}
QString filename ;
void newForm::on_load_tiff_PushButton_clicked()
{
filename = QFileDialog::getOpenFileName(this,tr("Load Image"),mResourceDir,tr("Images (*.jpg *.tiff)"));
if (filename.isEmpty()){
return;
}
QPixmap p(filename);
if (! widget.graphicsView->scene()){
qDebug() << "No Scene!";
QGraphicsScene *scene = new QGraphicsScene(this);
widget.graphicsView->setScene(scene);
}
widget.graphicsView->scene()->addPixmap(p);
}
void newForm::on_bitmap_PushButton_convert()
{
if (widget.graphicsView->scene()) {
QPixmap p(filename);
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
p.save(&buffer, "BMP"); // writes pixmap into bytes in PNG format
// widget.graphicsView->scene()->clear();
}
}
newForm::~newForm() {
}
Here is image of the "newForm.ui"这是“newForm.ui”的图像
I want this make "bmp" image on this directory /root/NetBeansProjects/TIF_TO_BMP/file.tiff in centos6.8 which is not made on clicking the Convert_To_BMP button that calls on_bitmap_PushButton_convert() .我希望在 centos6.8 中的此目录/root/NetBeansProjects/TIF_TO_BMP/file.tiff上制作“bmp”图像,这不是在单击调用on_bitmap_PushButton_convert()的Convert_To_BMP按钮时制作的。
You could use stb which is a header-only library.您可以使用stb这是一个仅限标头的库。
https://github.com/nothings/stb/blob/master/tests/image_write_test.c https://github.com/nothings/stb/blob/master/tests/image_write_test.c
// make a RGB version of the template image
// use red on blue to detect R<->B swaps
unsigned char img6x5_rgb[6*5*3];
float img6x5_rgbf[6*5*3];
int i;
for (i = 0; i < 6*5; i++) {
int on = img6x5_template[i] == '*';
img6x5_rgb[i*3 + 0] = on ? 255 : 0;
img6x5_rgb[i*3 + 1] = 0;
img6x5_rgb[i*3 + 2] = on ? 0 : 255;
img6x5_rgbf[i*3 + 0] = on ? 1.0f : 0.0f;
img6x5_rgbf[i*3 + 1] = 0.0f;
img6x5_rgbf[i*3 + 2] = on ? 0.0f : 1.0f;
}
stbi_write_png("output/wr6x5_regular.png", 6, 5, 3, img6x5_rgb, 6*3);
stbi_write_bmp("output/wr6x5_regular.bmp", 6, 5, 3, img6x5_rgb);
stbi_write_tga("output/wr6x5_regular.tga", 6, 5, 3, img6x5_rgb);
stbi_write_jpg("output/wr6x5_regular.jpg", 6, 5, 3, img6x5_rgb, 95);
stbi_write_hdr("output/wr6x5_regular.hdr", 6, 5, 3, img6x5_rgbf);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.