]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/jpeg/jpegloader.cpp
Add a helper class for registering image loaders
[libs/gui.git] / source / graphics / jpeg / jpegloader.cpp
1 #include <stdio.h>
2 #include <setjmp.h>
3 #include <jpeglib.h>
4 #include "jpegloader.h"
5
6 using namespace std;
7
8 namespace {
9
10 struct ErrorManager: jpeg_error_mgr
11 {
12         jmp_buf jmp;
13         string message;
14 };
15
16 void error_exit(j_common_ptr jpeg)
17 {
18         ErrorManager *err = reinterpret_cast<ErrorManager *>(jpeg->err);
19         char buf[JMSG_LENGTH_MAX];
20         err->format_message(jpeg, buf);
21         err->message = buf;
22         longjmp(err->jmp, 1);
23 }
24
25 void emit_message(j_common_ptr, int)
26 {
27 }
28
29
30 struct SourceManager: jpeg_source_mgr
31 {
32         Msp::IO::Seekable *io;
33         JOCTET buffer[4096];
34 };
35
36 void init_source(j_decompress_ptr jpeg)
37 {
38         SourceManager *src = reinterpret_cast<SourceManager *>(jpeg->src);
39         src->next_input_byte = src->buffer;
40         src->bytes_in_buffer = 0;
41 }
42
43 int fill_input_buffer(j_decompress_ptr jpeg)
44 {
45         SourceManager *src = reinterpret_cast<SourceManager *>(jpeg->src);
46         src->next_input_byte = src->buffer;
47         src->bytes_in_buffer = src->io->read(reinterpret_cast<char *>(src->buffer), sizeof(src->buffer));
48         return true;
49 }
50
51 void skip_input_data(j_decompress_ptr jpeg, long count)
52 {
53         if(count<=0)
54                 return;
55
56         SourceManager *src = reinterpret_cast<SourceManager *>(jpeg->src);
57         if(static_cast<unsigned long>(count)<src->bytes_in_buffer)
58         {
59                 src->next_input_byte += count;
60                 src->bytes_in_buffer -= count;
61         }
62         else
63         {
64                 src->io->seek(count-src->bytes_in_buffer, Msp::IO::S_CUR);
65                 src->bytes_in_buffer = 0;
66         }
67 }
68
69 void term_source(j_decompress_ptr)
70 {
71 }
72
73 } // namespace
74
75
76 namespace Msp {
77 namespace Graphics {
78
79 struct JpegLoader::Private
80 {
81         jpeg_decompress_struct jpeg;
82         ErrorManager err_mgr;
83         SourceManager src_mgr;
84 };
85
86
87 JpegLoader::JpegLoader(IO::Seekable &io):
88         priv(new Private)
89 {
90         priv->jpeg.err = jpeg_std_error(&priv->err_mgr);
91         priv->err_mgr.error_exit = &error_exit;
92         priv->err_mgr.emit_message = &emit_message;
93
94         jpeg_create_decompress(&priv->jpeg);
95
96         priv->jpeg.src = &priv->src_mgr;
97         priv->src_mgr.init_source = &init_source;
98         priv->src_mgr.fill_input_buffer = &fill_input_buffer;
99         priv->src_mgr.skip_input_data = &skip_input_data;
100         priv->src_mgr.resync_to_restart = &jpeg_resync_to_restart;
101         priv->src_mgr.term_source = &term_source;
102         priv->src_mgr.io = &io;
103 }
104
105 JpegLoader::~JpegLoader()
106 {
107         jpeg_destroy_decompress(&priv->jpeg);
108         delete priv;
109 }
110
111 bool JpegLoader::detect(const string &sig)
112 {
113         static const char jpeg_sig[] = "\xFF\xD8\xFF";
114         if(sig.size()<sizeof(jpeg_sig))
115                 return false;
116         return !sig.compare(0, 3, jpeg_sig);
117 }
118
119 void JpegLoader::load(Image::Data &data)
120 {
121         if(setjmp(priv->err_mgr.jmp))
122                 throw bad_image_data(priv->err_mgr.message);
123
124         jpeg_read_header(&priv->jpeg, true);
125         priv->jpeg.out_color_space = JCS_RGB;
126         jpeg_start_decompress(&priv->jpeg);
127
128         data.width = priv->jpeg.output_width;
129         data.height = priv->jpeg.output_height;
130         data.stride = priv->jpeg.output_width*priv->jpeg.output_components;
131         data.fmt = RGB;
132
133         data.data = new char[data.stride*data.height];
134         JSAMPROW rows[8];
135         while(priv->jpeg.output_scanline<data.height)
136         {
137                 unsigned y = data.height-priv->jpeg.output_scanline;
138                 unsigned count = min(y, 8U);
139                 for(unsigned i=0; i<count; ++i)
140                         rows[i] = reinterpret_cast<JSAMPROW>(data.data+(y-i-1)*data.stride);
141                 jpeg_read_scanlines(&priv->jpeg, rows, count);
142         }
143
144         jpeg_finish_decompress(&priv->jpeg);
145 }
146
147 } // namespace Graphics
148 } // namespace Msp