EmmaTT 发表于 2024-4-8 10:28

有没有老哥有jpg转bmp的源码啊?

想要给我的上位机增加这个功能

o88ne 发表于 2024-8-27 23:29

C语言JPEG转BMP源码

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

void write_bmp_header(FILE *bmp_file, int width, int height) {
    unsigned char bmpfileheader = {
      0x42, 0x4D,            // BM
      0, 0, 0, 0,            // file size
      0, 0,                  // reserved
      0, 0,                  // reserved
      54, 0, 0, 0            // start of pixel data
    };
    unsigned char bmpinfoheader = {
      40, 0, 0, 0,             // size of this header
      0, 0, 0, 0,            // width of bitmap
      0, 0, 0, 0,            // height of bitmap
      1, 0,                  // number of color planes
      24, 0,                   // number of bits per pixel
      0, 0, 0, 0,            // compression method (no compression)
      0, 0, 0, 0,            // size of pixel data (can be 0 for BI_RGB bitmaps)
      0, 0, 0, 0,            // horizontal resolution
      0, 0, 0, 0,            // vertical resolution
      0, 0, 0, 0,            // number of colors in palette
      0, 0, 0, 0               // important colors (0 = all colors are important)
    };

    int filesize = 54 + 3 * width * height;
    bmpfileheader = (unsigned char)(filesize);
    bmpfileheader = (unsigned char)(filesize >> 8);
    bmpfileheader = (unsigned char)(filesize >> 16);
    bmpfileheader = (unsigned char)(filesize >> 24);

    bmpinfoheader = (unsigned char)(width);
    bmpinfoheader = (unsigned char)(width >> 8);
    bmpinfoheader = (unsigned char)(width >> 16);
    bmpinfoheader = (unsigned char)(width >> 24);

    bmpinfoheader = (unsigned char)(height);
    bmpinfoheader = (unsigned char)(height >> 8);
    bmpinfoheader = (unsigned char)(height >> 16);
    bmpinfoheader = (unsigned char)(height >> 24);

    fwrite(bmpfileheader, 1, 14, bmp_file);
    fwrite(bmpinfoheader, 1, 40, bmp_file);
}

void jpeg_to_bmp(const char *jpeg_file, const char *bmp_file) {
    FILE *infile = fopen(jpeg_file, "rb");
    if (infile == NULL) {
      fprintf(stderr, "Error opening JPEG file %s\n", jpeg_file);
      return;
    }

    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    jpeg_stdio_src(&cinfo, infile);
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);

    int width = cinfo.output_width;
    int height = cinfo.output_height;
    int pixel_size = cinfo.output_components;

    unsigned char *buffer = (unsigned char *)malloc(width * pixel_size);
    if (buffer == NULL) {
      fprintf(stderr, "Memory allocation failed\n");
      return;
    }

    FILE *outfile = fopen(bmp_file, "wb");
    if (outfile == NULL) {
      fprintf(stderr, "Error opening BMP file %s\n", bmp_file);
      return;
    }

    write_bmp_header(outfile, width, height);

    unsigned char *row = (unsigned char *)malloc(3 * width);
    if (row == NULL) {
      fprintf(stderr, "Memory allocation failed\n");
      return;
    }

    while (cinfo.output_scanline < height) {
      jpeg_read_scanlines(&cinfo, &buffer, 1);
      for (int i = 0; i < width; i++) {
            row = buffer;   // B
            row = buffer; // G
            row = buffer;   // R
      }
      fwrite(row, 3, width, outfile);
    }

    free(row);
    fclose(outfile);
    free(buffer);

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
      fprintf(stderr, "Usage: %s <input.jpg> <output.bmp>\n", argv);
      return 1;
    }

    jpeg_to_bmp(argv, argv);

    return 0;
}

o88ne 发表于 2024-8-27 23:29

编译: 使用gcc编译此程序,并链接libjpeg库:

bash

gcc jpeg_to_bmp.c -o jpeg_to_bmp -ljpeg

o88ne 发表于 2024-8-27 23:29

运行: 执行程序时传入JPEG文件和目标BMP文件路径:

bash

./jpeg_to_bmp input.jpg output.bmp

o88ne 发表于 2024-8-27 23:29

该程序假定输入的JPEG文件是24位的无损压缩图像。如果处理其他类型的JPEG文件,可能需要更多的处理步骤。

o88ne 发表于 2024-8-27 23:29

如果需要处理更复杂的图像格式(如灰度图),则需要修改代码来适应不同的颜色深度。

o88ne 发表于 2024-8-27 23:57

通过这个源码,你可以将JPEG文件转换为BMP格式,并且可以根据实际需求对代码进行进一步修改
页: [1]
查看完整版本: 有没有老哥有jpg转bmp的源码啊?