91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
大荔县| 隆子县| 友谊县| 织金县| 灌阳县| 万年县| 武山县| 凤冈县| 泰安市| 武功县| 仪征市| 洛川县| 康平县| 即墨市| 潼南县| 高陵县| 承德县| 乐平市| 龙岩市| 大余县| 铜鼓县| 靖江市| 东山县| 梅河口市| 扎赉特旗| 溧水县| 论坛| 虎林市| 额敏县| 班玛县| 巫山县| 舞钢市| 忻州市| 察隅县| 松江区| 盐源县| 同江市| 永定县| 长顺县| 汽车| 定陶县|