亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

C++中bitmap的壓縮與解壓

c++
小樊
86
2024-08-23 12:02:40
欄目: 編程語言

在C++中,可以使用第三方庫如libpng、OpenCV等來實現bitmap的壓縮與解壓。下面是一個使用libpng庫進行bitmap壓縮與解壓的示例代碼:

#include <iostream>
#include <png.h>

void compressBitmap(const char* inputFile, const char* outputFile) {
    FILE *fp = fopen(inputFile, "rb");
    if (!fp) {
        std::cerr << "Error opening input file" << std::endl;
        return;
    }

    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png) {
        std::cerr << "Error creating read struct" << std::endl;
        fclose(fp);
        return;
    }

    png_infop info = png_create_info_struct(png);
    if (!info) {
        std::cerr << "Error creating info struct" << std::endl;
        png_destroy_read_struct(&png, NULL, NULL);
        fclose(fp);
        return;
    }

    png_init_io(png, fp);

    png_read_info(png, info);

    png_set_compression_level(png, Z_BEST_COMPRESSION);

    FILE *out = fopen(outputFile, "wb");
    if (!out) {
        std::cerr << "Error opening output file" << std::endl;
        png_destroy_read_struct(&png, &info, NULL);
        fclose(fp);
        return;
    }

    png_set_write_fn(png, out, [](png_structp png, png_bytep data, size_t length) {
        fwrite(data, 1, length, (FILE *)png_get_io_ptr(png));
    }, NULL);

    png_write_info(png, info);

    png_bytep row = new png_byte[png_get_rowbytes(png, info)];
    for (int y = 0; y < png_get_image_height(png, info); y++) {
        png_read_row(png, row, NULL);
        png_write_row(png, row);
    }

    png_write_end(png, NULL);

    delete[] row;
    fclose(out);
    png_destroy_read_struct(&png, &info, NULL);
    fclose(fp);

    std::cout << "Bitmap compressed successfully" << std::endl;
}

void decompressBitmap(const char* inputFile, const char* outputFile) {
    FILE *fp = fopen(inputFile, "rb");
    if (!fp) {
        std::cerr << "Error opening input file" << std::endl;
        return;
    }

    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png) {
        std::cerr << "Error creating read struct" << std::endl;
        fclose(fp);
        return;
    }

    png_infop info = png_create_info_struct(png);
    if (!info) {
        std::cerr << "Error creating info struct" << std::endl;
        png_destroy_read_struct(&png, NULL, NULL);
        fclose(fp);
        return;
    }

    png_init_io(png, fp);

    png_read_info(png, info);

    FILE *out = fopen(outputFile, "wb");
    if (!out) {
        std::cerr << "Error opening output file" << std::endl;
        png_destroy_read_struct(&png, &info, NULL);
        fclose(fp);
        return;
    }

    png_set_read_fn(png, out, [](png_structp png, png_bytep data, size_t length) {
        fread(data, 1, length, (FILE *)png_get_io_ptr(png));
    }, NULL);

    png_read_end(png, NULL);

    png_bytep row = new png_byte[png_get_rowbytes(png, info)];
    for (int y = 0; y < png_get_image_height(png, info); y++) {
        png_read_row(png, row, NULL);
    }

    delete[] row;
    fclose(out);
    png_destroy_read_struct(&png, &info, NULL);
    fclose(fp);

    std::cout << "Bitmap decompressed successfully" << std::endl;
}

int main() {
    compressBitmap("input.bmp", "compressed.png");
    decompressBitmap("compressed.png", "output.bmp");

    return 0;
}

在上面的示例中,我們使用libpng庫來實現了對bitmap文件的壓縮和解壓縮操作。首先,我們使用compressBitmap函數來將輸入的bitmap文件壓縮成png文件,然后使用decompressBitmap函數將壓縮后的png文件解壓縮成bitmap文件。最后,在main函數中我們調用這兩個函數來演示整個過程。

0
邵阳县| 尚义县| 敖汉旗| 日喀则市| 韶关市| 泰兴市| 贡嘎县| 济源市| 淮北市| 亚东县| 柯坪县| 商都县| 武穴市| 绵阳市| 秦皇岛市| 焦作市| 湖南省| 金山区| 遵义县| 会东县| 普格县| 荔浦县| 合江县| 镇雄县| 泾川县| 会理县| 镇平县| 绥棱县| 千阳县| 高密市| 铅山县| 离岛区| 增城市| 社会| 修文县| 吉林省| 荔波县| 梨树县| 防城港市| 桦川县| 南宁市|