Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 872233fbd25a5eb4d9cadce04f0ed3d25d3f988d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
   render/jpgfile.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

/*
Notes:

http://www.zarb.org/~gc/html/libpng.html

*/

#include <cstring>
#include <iostream>

#include "filesystem/filesystem.h"
#include "render/jpgfile.h"
#include "sys/sys.h"

extern "C" {
	#include "jpeglib.h"
}

namespace render {

Image *JPG::load(const char *filename)
{
 	struct jpeg_decompress_struct jpeg_decompression_info;
	struct jpeg_error_mgr jerr;

	int row_stride = 0;
	Image *image = 0;

	if (!filename)
		return 0;

	filesystem::File *jpg_file = filesystem::open(filename);
	if (!jpg_file) {
		//con_warn << "Could not open " << filename << std::endl;
		return 0;
	}
 
	// initialize decompression structures
	jpeg_decompression_info.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&jpeg_decompression_info);
 	jpeg_stdio_src(&jpeg_decompression_info, jpg_file->handle());

	// read JPEG header
	jpeg_read_header(&jpeg_decompression_info, TRUE);
	jpeg_start_decompress(&jpeg_decompression_info);

	row_stride = jpeg_decompression_info.output_width * jpeg_decompression_info.output_components;
	image = new Image(jpeg_decompression_info.output_width, 
			jpeg_decompression_info.output_height,
			jpeg_decompression_info.output_components);

	// read pixel data
	JSAMPLE *row_pointer;
	while (jpeg_decompression_info.output_scanline < jpeg_decompression_info.output_height) {
		row_pointer = (*image)[jpeg_decompression_info.output_scanline*row_stride];
		jpeg_read_scanlines(&jpeg_decompression_info, &row_pointer, 1);
	}
	 jpeg_finish_decompress(&jpeg_decompression_info);

	// destroy decompression structures
	jpeg_destroy_decompress(&jpeg_decompression_info);
        filesystem::close(jpg_file);

	con_debug << "  " << filename << " " << image->width() << "x" << image->height() << "x" << image->bpp() << "bpp" << std::endl;
	return image;
}

void JPG::save(const char *filename, Image & image, int jpeg_quality)
{
	struct jpeg_compress_struct jpeg_compression_info;
	struct jpeg_error_mgr jerr;

	JSAMPROW row_pointer[1];
	int row_stride;

        FILE *jpg_file = fopen(filename, "wb");
        if (!jpg_file) {
     		con_warn << "Could not write " << filename << std::endl;
                return;
	}

	jpeg_compression_info.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&jpeg_compression_info);
	jpeg_stdio_dest(&jpeg_compression_info, jpg_file);

	jpeg_compression_info.image_width = image.width();
	jpeg_compression_info.image_height = image.height();
	jpeg_compression_info.input_components = image.channels();
	jpeg_compression_info.in_color_space = JCS_RGB;

	jpeg_set_defaults(&jpeg_compression_info);
	jpeg_set_quality(&jpeg_compression_info, jpeg_quality, TRUE);
	jpeg_start_compress(&jpeg_compression_info, TRUE);
	row_stride = image.width() * image.channels();

	while (jpeg_compression_info.next_scanline < jpeg_compression_info.image_height) {
		row_pointer[0] = image[jpeg_compression_info.next_scanline * row_stride];
		jpeg_write_scanlines(&jpeg_compression_info, row_pointer, 1);
	}

	jpeg_finish_compress(&jpeg_compression_info);
	fclose(jpg_file);

	jpeg_destroy_compress(&jpeg_compression_info);

	con_print << "Wrote " << filename << std::endl;
}

}