第2篇 基于C/C++实现RAW格式图像读取和写出

代码:
#include <iostream>
#include "ipp.h"
#include <stdio.h>
using namespace std;

#define  width  512/* Image width  */
#define  height  512 /* Image height */

int main(void)
{
//fopen读取image
FILE *fpsrc = NULL;  //读入文件指针
Ipp16u *src = new Ipp16u[width*height];  //读入
if ((fpsrc = fopen("src.raw", "rb")) == NULL)
{
	printf("can not open the raw image");
	return 0;
}
else
{
	printf("IMAGE read OK\n");
}
fread(src, sizeof(Ipp16u), width*height, fpsrc);
fclose(fpsrc);

//fwrite写出数据
FILE *fpdst = NULL;  //写出文件指针
Ipp16u *dst = new Ipp16u[width*height];  //写出
fpdst = fopen("dst.raw", "wb");
if (!fpdst)
{
	return 1;
}
fwrite(dst, sizeof(Ipp16u), width * height, fpdst);
fclose(fpdst);


return 0;
}
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,你可以参考以下步骤: 1. 下载libjpeg库文件,并将其加入到你的Android项目中。 2. 在你的C/C++代码中,包含libjpeg的头文件:#include "jpeglib.h" 3. 定义一个结构体,用于存储压缩后的图像数据: ```c++ struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); ``` 4. 设置压缩参数: ```c++ cinfo.image_width = width; // 设置图像宽度 cinfo.image_height = height; // 设置图像高度 cinfo.input_components = 1; // 设置图像颜色分量数 cinfo.in_color_space = JCS_GRAYSCALE; // 设置图像颜色空间为灰度 jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE); // 设置压缩质量 ``` 5. 指定输出文件或者输出缓冲区: ```c++ FILE *outfile; // 输出到文件 outfile = fopen(outfilename, "wb"); jpeg_stdio_dest(&cinfo, outfile); // 或者输出到缓冲区 unsigned char *outbuffer; outbuffer = (unsigned char *)malloc(buffer_size); jpeg_mem_dest(&cinfo, &outbuffer, &buffer_size); ``` 6. 开始压缩: ```c++ jpeg_start_compress(&cinfo, TRUE); JSAMPROW row_pointer[1]; while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = &image_data[cinfo.next_scanline * cinfo.image_width]; jpeg_write_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_compress(&cinfo); ``` 7. 释放资源: ```c++ jpeg_destroy_compress(&cinfo); fclose(outfile); // 如果输出到文件 free(outbuffer); // 如果输出到缓冲区 ``` 这样就可以使用libjpeg库对RAW8图像数据进行压缩了。需要注意的是,压缩后的图像数据需要进行解压缩才能还原成原始图像数据。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值