打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用libjpeg处理图像(功能简陋)
使用libjpeg实现图像的输入输出和像素读写,可以帮助大家学习libjpeg。

/*
 * hellojpeg.c
 * 
 * This program is under Omega Project 2012.
 * 
 * Copyright 2012 S O Coleman
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */


#include <stdio.h>
#include <jpeglib.h>
#include <malloc.h>
#include "hellojpeg.h"

/*
char filename[] = "02.jpg";
char filenameout[] = "A02.jpg";

int width, height = 0;
unsigned char *data;

int main(int argc, char **argv)
{
    read_jpeg(filename, &data, &width, &height);
    unsigned char pixel[3] = {0, 0, 0};
    set_pixel_of(0, 0, pixel, &data, &width, &height);
    write_jpeg(filenameout, &data, &width, &height);
return 0;
}
*/

void read_jpeg(char *filename, unsigned char **data, int *width, int *height)
{
    FILE * infile = fopen(filename, "rb");
    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);
    *width = cinfo.output_width;
    *height = cinfo.output_height;
    *data = (unsigned char *) malloc(cinfo.output_height * cinfo.output_width * cinfo.output_components);
    unsigned char *line_pointer;
    int i = 0;
    while (cinfo.output_scanline < cinfo.image_height) {
        line_pointer = *data + i * cinfo.output_width * cinfo.output_components;
        jpeg_read_scanlines(&cinfo, &line_pointer, 1); 
        i ++;
    }
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
}

void write_jpeg(char *filename, unsigned char **data, int *width, int *height)
{
    FILE * outfile = fopen(filename, "wb");
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = *width;
    cinfo.image_height = *height;
    cinfo.in_color_space = JCS_RGB;
    cinfo.input_components = 3;
    jpeg_set_defaults(&cinfo);
    jpeg_start_compress(&cinfo, TRUE);
    unsigned char *line_pointer;
    int i = 0;
    while (cinfo.next_scanline < cinfo.image_height) {
        line_pointer = *data + i * cinfo.image_width * cinfo.input_components;
        jpeg_write_scanlines(&cinfo, &line_pointer, 1); 
        i ++;
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
}

void get_pixel_of(int x, int y, unsigned char *dest, unsigned char **data, int *width, int *height)
{
if ((x >= *width) || (x < 0) || (y >= *height) || (y < 0))
{
        dest[0] = 0;
        dest[1] = 0;
        dest[2] = 0;
return;
}
    unsigned char *pos;
    pos = *data + (y * *width + x) * 3;
    dest[0] = pos[0];
    dest[1] = pos[1];
    dest[2] = pos[2];
}

void set_pixel_of(int x, int y, unsigned char *dest, unsigned char **data, int *width, int *height)
{
if ((x >= *width) || (x < 0) || (y >= *height) || (y < 0))
{
return;
}
    unsigned char *pos;
    pos = *data + (y * *width + x) * 3;
    pos[0] = dest[0];
    pos[1] = dest[1];
    pos[2] = dest[2];
}




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
framebuffer显示JPEG图片
MFC读取jpeg
如何使用jpeglib库压缩yuv422?
libjpeg学习4:libjpeg
快速压缩jpeg
Linux下利用libjpeg实现bmp与jpg相互转换C代码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服