简体   繁体   中英

How to convert a TIF file into a BMP file in C++98 using QT 4?

I am new in C++. How can I convert a TIF file into a BMP file in C++98?

Here is my "newForm.h".Here is private slots for loading tiff and coverting the image.

#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".

#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

#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"

在此处输入图像描述

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() .

You could use stb which is a header-only library.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM