C/C++编程:C生成BMP、PNG和JPEG图片:

1059 篇文章 285 订阅

1、生成bmp图片

在学习图形图像的过程中,最简单和常见的格式是BMP和PPM。

下面为BMP生成的最简单代码:

#include <stdio.h>
#include <stdlib.h>
#define w 200
#define h 500
void WriteBMP(char*img,const char* filename)
{
    int l=(w*3+3)/4*4;
    int bmi[]= {
            l*h+54,0,54,40,w,h,1|3*8<<16,0,l*h,0,0,100,0};
    FILE *fp = fopen(filename,"wb");
    fprintf(fp,"BM");
    fwrite(&bmi,52,1,fp);
    fwrite(img,1,l*h,fp);
    fclose(fp);
}
int main()
{
    char img[w*h*3];
    for(int i=0; i<w*h*3; i++)img[i]=rand()%256;
    WriteBMP(img,"test.bmp");
    system("test.bmp");
    return 0;
}

如果想指定颜色

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

/*
https://blog.csdn.net/nibiewuxuanze/article/details/78805763
按小端字节序来存储,且数据块一般组织成4字节对齐。
图像数据区也不例外,按每行图像的数据字节,按4字节对齐。

图像数据按行倒序存放,先存储最后一行图像数据,然后依次存放,直到第一行数据。
这样设计,可能是为了从文件尾部往前读的时候,能够直接顺序读出图像数据吧。
*/
typedef union {
    uint8_t bytes[4];
    uint32_t value;
}LITTLE;

/*
 * @fileName: bmp file name: test.bmp
 * @width   : bmp pixel width: 32bit
 * @height  : bmp pixel width: 32bit
 * @color   : R[8]/G[8]/B[8]
 * @note    : BMP is l endian mode
 */
int bmp_gen_test(char *fileName, uint32_t width, uint32_t height, uint32_t color)
{
    FILE *fp;
    uint32_t i, j;
    LITTLE l_width, l_height, l_bfSize, l_biSizeImage;

    uint8_t r = color >> 16;
    uint8_t g = color >> 8;
    uint8_t b = color;
    uint32_t width_r  =  (width * 24 / 8 + 3) / 4 * 4;
    uint32_t bfSize = width_r * height + 54 + 2;
    uint32_t biSizeImage = width_r * height;

    l_width.value = width;
    l_height.value = height;
    l_bfSize.value = bfSize;
    l_biSizeImage.value = biSizeImage;

    /* BMP file format: www.cnblogs.com/wainiwann/p/7086844.html */
    uint8_t bmp_head_map[54] = {
            /* bmp file header: 14 byte */
            0x42, 0x4d,
            // bmp pixel size: width * height * 3 + 54
            l_bfSize.bytes[0], l_bfSize.bytes[1], l_bfSize.bytes[2], l_bfSize.bytes[3],
            0, 0 , 0, 0,
            54, 0 , 0, 0,    /* 14+40=54 */

            /* bmp map info: 40 byte */
            40, 0, 0, 0,
            //width
            l_width.bytes[0], l_width.bytes[1], l_width.bytes[2], l_width.bytes[3],
            //height
            l_height.bytes[0], l_height.bytes[1], l_height.bytes[2], l_height.bytes[3],
            1, 0,
            24, 00,             /* 24 bit: R[8]/G[8]/B[8] */

            0, 0, 0, 0,     //biCompression:0
//        0, 0, 0, 0,     //biSizeImage锛欰2 00 00 00=162
            l_biSizeImage.bytes[0], l_biSizeImage.bytes[1], l_biSizeImage.bytes[2], l_biSizeImage.bytes[3],
            0, 0, 0, 0,     //biXPelsPerMeter: 60 0F 00 00
            0, 0, 0, 0,     //biYPelsPerMeter
            0, 0, 0, 0,     //biClrUsed
            0, 0, 0, 0      //biClrImportant
    };

    /* write in binary format */
    fp = fopen(fileName, "wb+");
    if(fp == NULL)
    {
        printf("%s: file create failed!\n", fileName);
        return -1;
    }

    printf("%s: file create success!\n", fileName);

    fwrite(bmp_head_map, sizeof(bmp_head_map), 1, fp);

    for(i = 0; i < height; i++) {
        for(j = 0; j < width; j++)
            fprintf(fp, "%c%c%c", b, g, r); /* BGR */
        //4 byte align
        for(j = 0; j < width_r-width*3; j++)
            fprintf(fp, "%c", 0);
    }

    fprintf(fp, "%c%c", 0, 0); //PhotoShop two byte "0"

    if(fclose(fp))
    {
        printf("file close failed!\n");
        return -1;
    }
    fp = NULL;

    printf("width: %d\n", width);
    printf("height: %d\n", height);
    printf("R:%d, G:%d, B:%d or #%06x\n", r, g, b, color);

    return 0;
}

int main(int argc, char *argv[])
{
    int ret;
    char bmpName[200];

    char *name = "test";
    uint32_t width = 200;
    uint32_t height = 200;
    uint32_t color = 0x563412;

    /* generate bmp file name */
    sprintf(bmpName, "%s_%d_%d_0x%06x.bmp", name, width, height, color);
    printf("bmpName: %s\n", bmpName);

    ret = bmp_gen_test(bmpName, width, height, color);
    if(!ret)
        system(bmpName);

    

    return 0;
}

上述代码生成一幅宽和高均为200的BMP随机位图。如图所示:

在这里插入图片描述

2、生成png图片

生成无压缩PNG图片:https://github.com/miloyip/svpng

//------------------ svpng.h---------------------

/*! \file
    \brief      svpng() is a minimalistic C function for saving RGB/RGBA image into uncompressed PNG.
    \author     Milo Yip
    \version    0.1.1
    \copyright  MIT license
    \sa         http://github.com/miloyip/svpng
*/

#ifndef SVPNG_INC_
#define SVPNG_INC_

/*! \def SVPNG_LINKAGE
    \brief User customizable linkage for svpng() function.
    By default this macro is empty.
    User may define this macro as static for static linkage,
    and/or inline in C99/C++, etc.
*/
#ifndef SVPNG_LINKAGE
#define SVPNG_LINKAGE
#endif

/*! \def SVPNG_OUTPUT
    \brief User customizable output stream.
    By default, it uses C file descriptor and fputc() to output bytes.
    In C++, for example, user may use std::ostream or std::vector instead.
*/
#ifndef SVPNG_OUTPUT
#include <stdio.h>
#define SVPNG_OUTPUT FILE* fp
#endif

/*! \def SVPNG_PUT
    \brief Write a byte
*/
#ifndef SVPNG_PUT
#define SVPNG_PUT(u) fputc(u, fp)
#endif


/*!
    \brief Save a RGB/RGBA image in PNG format.
    \param SVPNG_OUTPUT Output stream (by default using file descriptor).
    \param w Width of the image. (<16383)
    \param h Height of the image.
    \param img Image pixel data in 24-bit RGB or 32-bit RGBA format.
    \param alpha Whether the image contains alpha channel.
*/
SVPNG_LINKAGE void svpng(SVPNG_OUTPUT, unsigned w, unsigned h, const unsigned char* img, int alpha) {
    static const unsigned t[] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
            /* CRC32 Table */    0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
    unsigned a = 1, b = 0, c, p = w * (alpha ? 4 : 3) + 1, x, y, i;   /* ADLER-a, ADLER-b, CRC, pitch */
#define SVPNG_U8A(ua, l) for (i = 0; i < l; i++) SVPNG_PUT((ua)[i]);
#define SVPNG_U32(u) do { SVPNG_PUT((u) >> 24); SVPNG_PUT(((u) >> 16) & 255); SVPNG_PUT(((u) >> 8) & 255); SVPNG_PUT((u) & 255); } while(0)
#define SVPNG_U8C(u) do { SVPNG_PUT(u); c ^= (u); c = (c >> 4) ^ t[c & 15]; c = (c >> 4) ^ t[c & 15]; } while(0)
#define SVPNG_U8AC(ua, l) for (i = 0; i < l; i++) SVPNG_U8C((ua)[i])
#define SVPNG_U16LC(u) do { SVPNG_U8C((u) & 255); SVPNG_U8C(((u) >> 8) & 255); } while(0)
#define SVPNG_U32C(u) do { SVPNG_U8C((u) >> 24); SVPNG_U8C(((u) >> 16) & 255); SVPNG_U8C(((u) >> 8) & 255); SVPNG_U8C((u) & 255); } while(0)
#define SVPNG_U8ADLER(u) do { SVPNG_U8C(u); a = (a + (u)) % 65521; b = (b + a) % 65521; } while(0)
#define SVPNG_BEGIN(s, l) do { SVPNG_U32(l); c = ~0U; SVPNG_U8AC(s, 4); } while(0)
#define SVPNG_END() SVPNG_U32(~c)
    SVPNG_U8A("\x89PNG\r\n\32\n", 8);           /* Magic */
    SVPNG_BEGIN("IHDR", 13);                    /* IHDR chunk { */
    SVPNG_U32C(w); SVPNG_U32C(h);               /*   Width & Height (8 bytes) */
    SVPNG_U8C(8); SVPNG_U8C(alpha ? 6 : 2);     /*   Depth=8, Color=True color with/without alpha (2 bytes) */
    SVPNG_U8AC("\0\0\0", 3);                    /*   Compression=Deflate, Filter=No, Interlace=No (3 bytes) */
    SVPNG_END();                                /* } */
    SVPNG_BEGIN("IDAT", 2 + h * (5 + p) + 4);   /* IDAT chunk { */
    SVPNG_U8AC("\x78\1", 2);                    /*   Deflate block begin (2 bytes) */
    for (y = 0; y < h; y++) {                   /*   Each horizontal line makes a block for simplicity */
        SVPNG_U8C(y == h - 1);                  /*   1 for the last block, 0 for others (1 byte) */
        SVPNG_U16LC(p); SVPNG_U16LC(~p);        /*   Size of block in little endian and its 1's complement (4 bytes) */
        SVPNG_U8ADLER(0);                       /*   No filter prefix (1 byte) */
        for (x = 0; x < p - 1; x++, img++)
            SVPNG_U8ADLER(*img);                /*   Image pixel data */
    }
    SVPNG_U32C((b << 16) | a);                  /*   Deflate block end with adler (4 bytes) */
    SVPNG_END();                                /* } */
    SVPNG_BEGIN("IEND", 0); SVPNG_END();        /* IEND chunk {} */
}

#endif /* SVPNG_INC_ */
-----------------main.c---------------------------------
#include "svpng.h"

void test_rgb(void) {
    unsigned char rgb[256 * 256 * 3], *p = rgb;
    unsigned x, y;
    FILE *fp = fopen("C:\\Users\\Administrator\\Pictures\\rgb.png", "wb");
    for (y = 0; y < 256; y++)
        for (x = 0; x < 256; x++) {
            *p++ = (unsigned char)x;    /* R */
            *p++ = (unsigned char)y;    /* G */
            *p++ = 128;                 /* B */
        }
    svpng(fp, 256, 256, rgb, 0);
    fclose(fp);
}

void test_rgba(void) {
    unsigned char rgba[256 * 256 * 4], *p = rgba;
    unsigned x, y;
    FILE* fp = fopen("C:\\Users\\Administrator\\Pictures\\rgba.png", "wb");
    for (y = 0; y < 256; y++)
        for (x = 0; x < 256; x++) {
            *p++ = (unsigned char)x;                /* R */
            *p++ = (unsigned char)y;                /* G */
            *p++ = 128;                             /* B */
            *p++ = (unsigned char)((x + y) / 2);    /* A */
        }
    svpng(fp, 256, 256, rgba, 1);
    fclose(fp);
}

int main(void) {
    test_rgb();
    test_rgba();
    return 0;
}

补充:LodePNG是一个集合了PNG图像解码器和编码器的代码文件,不依赖于诸如zlib和libpng的外部链接/库,提供方便友好的PNG编解码器调用方法。LodePNG主要是采用C(ISO C90)编写的,并提供了C++的接口。LodePNG的使用非常简单,只要在项目文件中包含lodepng.cpp和lodepng.h或者lodepng.c和lodepng.h就可以。

  • 9
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
非常感谢您的提问!下面是一个基于QT/C++、OpenCV的可视化窗体开发,对jpegPNG图像进行自适应双边滤波的示例代码。 首先,我们需要在QT中新建一个窗体应用程序,并将OpenCV集成到其中。具体的操作可以参考这篇文章:https://www.cnblogs.com/FlyingFish/p/11052436.html 接下来,我们需要编写代码实现对jpegPNG图像进行自适应双边滤波。代码如下所示: ```c++ #include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)")); if(!fileName.isEmpty()) { cv::Mat image = cv::imread(fileName.toStdString()); cv::Mat dst; cv::bilateralFilter(image, dst, 15, 80, 80); cv::imshow("Original Image", image); cv::imshow("Filtered Image", dst); cv::waitKey(); } } ``` 在这段代码中,我们使用QT提供的QFileDialog类来实现文件选择功能,然后使用OpenCV的imread函数读取图像文件。接着,我们使用OpenCV的bilateralFilter函数来对图像进行自适应双边滤波,其中第一个参数是原始图像,第二个参数是输出图像,第三个参数是滤波窗口大小,第四个参数是空间高斯函数标准差,第五个参数是灰度值相似性高斯函数标准差。 最后,我们使用OpenCV的imshow函数来显示原始图像和滤波后的图像,并使用waitKey函数等待用户按下任意键关闭窗口。 希望这个示例能够对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值