forked from yvt/xtbook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XTBImageComplexArticle.cpp
70 lines (60 loc) · 1.72 KB
/
XTBImageComplexArticle.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
//
// XTBImageComplexArticle.cpp
// XTBook
//
// Created by Kawada Tomoaki on 8/1/11.
// Copyright 2011 Nexhawks. All rights reserved.
//
#include "XTBImageComplexArticle.h"
#include "XTBImageComplexDictionary.h"
#include "XTBException.h"
#include "XTBMemoryStream.h"
XTBImageComplexArticle::XTBImageComplexArticle(const XTBImageComplexDictionary *dic,
const std::string& dbKey):
m_dic(dic),
m_imageLoaded(false),
m_dbKey(dbKey),
m_dimensionLoaded(false),
m_image(NULL),
m_pointerLoaded(false){
}
XTBImageComplexArticle::~XTBImageComplexArticle(){
if(m_image)
delete m_image;
}
twSize XTBImageComplexArticle::dimension() const{
if(!m_dimensionLoaded){
XTBDicDBReader *reader=m_dic->m_images;
XTBDicDBPointer ptr=pointer();
std::string header=reader->contentsAtPointer(ptr, 4);
m_dimension.w=((unsigned char)header[1]);
m_dimension.w|=((int)(unsigned char)header[0])<<8;
m_dimension.h=((unsigned char)header[3]);
m_dimension.h|=((int)(unsigned char)header[2])<<8;
m_dimensionLoaded=true;
}
return m_dimension;
}
XTBImage *XTBImageComplexArticle::image() const{
if(!m_imageLoaded){
XTBDicDBReader *reader=m_dic->m_images;
XTBDicDBPointer ptr=pointer();
std::string bytes=reader->contentsAtPointer(ptr);
XTBMemoryStream memoryStream(bytes.data(), bytes.size());
memoryStream.seekToOffset(4);
m_image=XTBImage::imageForStream(&memoryStream);
m_imageLoaded=true;
}
return m_image;
}
XTBDicDBPointer XTBImageComplexArticle::pointer() const{
if(!m_pointerLoaded){
XTBDicDBReader *reader=m_dic->m_images;
m_pointer=reader->pointerForKey(m_dbKey);
if(m_pointer==XTBDicDBInvalidPointer){
XTBThrowException(L"XTBIOErrorException", NULL);
}
m_pointerLoaded=true;
}
return m_pointer;
}