-
Notifications
You must be signed in to change notification settings - Fork 0
/
XTBJpegLoader.cpp
154 lines (121 loc) · 2.93 KB
/
XTBJpegLoader.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// XTBJpegLoader.cpp
// XTBook
//
// Created by Kawada Tomoaki on 7/29/11.
// Copyright 2011 Nexhawks. All rights reserved.
//
#include "XTBJpegLoader.h"
#include "XTBRichgelJpegStream.h"
#include "XTBException.h"
#include <tcw/twSDLDC.h>
#include <assert.h>
bool XTBJpegLoader::probe(XTBStream *steam){
unsigned char buf[2];
if(steam->readToBuffer(buf, 2)<2)
return false;
if(buf[0]==0xff && buf[1]==0xd8)
return true;
return false;
}
#pragma mark - Decoder
XTBJpegLoader::XTBJpegLoader(XTBStream *stream):
m_stream(stream){
m_stream2=new XTBRichgelJpegStream(m_stream);
m_decoder=new jpeg_decoder(m_stream2, false);
if(m_decoder->get_error_code()){
int errorCode=m_decoder->get_error_code();
delete m_decoder;
delete m_stream2;
XTBThrowException(L"XTBRichgelJpegException",
XTBFormatStd(L"0x%08x", errorCode).c_str(), NULL);
}
}
XTBJpegLoader::~XTBJpegLoader(){
delete m_decoder;
delete m_stream2;
}
twDC *XTBJpegLoader::image(){
if(m_decoder->begin()){
int errorCode=m_decoder->get_error_code();
delete m_decoder;
delete m_stream2;
XTBThrowException(L"XTBRichgelJpegException",
XTBFormatStd(L"0x%08x", errorCode).c_str(), NULL);
}
uint8_t *lineBuffer; // don't need to malloc/free
twSDLDC *image;
SDL_Surface *surf;
uint lineLen;
int width=m_decoder->get_width();
int height=m_decoder->get_height();
image=new twSDLDC(twSize(width, height));
surf=image->getSurface();
uint16_t *dest=(uint16_t *)surf->pixels;
assert(surf->format->BitsPerPixel==16);
int y=0;
while(!m_decoder->decode((void **)&lineBuffer, &lineLen)){
if(m_decoder->get_num_components()==1){
convert8To565(dest, lineBuffer, m_decoder->get_width(), y);
}else if(m_decoder->get_num_components()==3){
convert888To565(dest, lineBuffer, m_decoder->get_width(), y);
}
dest+=m_decoder->get_width();
y++;
}
return image;
}
#pragma mark - Converter
static const uint8_t g_ditherMap[]={
0, 4, 1, 2,
6, 2, 7, 3,
2, 6, 1, 5,
8, 4, 7, 3
};
void XTBJpegLoader::convert888To565(uint16_t *dest,
const uint8_t *src,
size_t pixels,
int y){
register const uint8_t *ditherMap=g_ditherMap;
register int ditherPos=(y&3)*4;
register int ditherLimit=ditherPos+4;
while(pixels--){
register unsigned int out;
register uint8_t dither=ditherMap[ditherPos];
register unsigned int tmp;
tmp=*(src++);
if(tmp<(255-7))
tmp+=dither;
out=(tmp>>3);
out<<=6;
tmp=*(src++);
if(tmp<(255-3))
tmp+=(dither>>1);
out|=(tmp>>2);
out<<=5;
tmp=*(src++);
if(tmp<(255-7))
tmp+=dither;
out|=(tmp>>3);
*(dest++)=out;
src++;
ditherPos++;
if(ditherPos==ditherLimit)
ditherPos-=4;
}
}
void XTBJpegLoader::convert8To565(uint16_t *dest,
const uint8_t *src,
size_t pixels,
int y){
while(pixels--){
register unsigned int out, v;
v=*(src++)>>3;
out=v;
out<<=5;
out|=v;
out<<=6;
out|=v;
*(dest++)=out;
}
}