/*
* 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];
}