kis_tiff_stream.cpp
1/*
2 * Copyright (c) 2005-2006 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "kis_tiff_stream.h"
21
22TIFFStreamContigBase::TIFFStreamContigBase( uint8* src, uint16 depth, uint32 lineSize ) : TIFFStreamBase(depth), m_src(src), m_lineSize(lineSize) { restart(); }
23
24void TIFFStreamContigBase::restart()
25{
26 m_srcit = m_src;
27 m_posinc = 8;
28}
29
30void TIFFStreamContigBase::moveToLine(uint32 lineNumber)
31{
32 m_srcit = m_src + lineNumber * m_lineSize;
33 m_posinc = 8;
34}
35
36uint32 TIFFStreamContigBelow16::nextValue()
37{
38 uint8 remain;
39 uint32 value;
40 remain = m_depth;
41 value = 0;
42 while (remain > 0)
43 {
44 uint8 toread;
45 toread = remain;
46 if (toread > m_posinc) toread = m_posinc;
47 remain -= toread;
48 m_posinc -= toread;
49 value = (value << toread) | (( (*m_srcit) >> (m_posinc) ) & ( ( 1 << toread ) - 1 ) );
50 if (m_posinc == 0)
51 {
52 m_srcit++;
53 m_posinc=8;
54 }
55 }
56 return value;
57}
58
59uint32 TIFFStreamContigBelow32::nextValue()
60{
61 uint8 remain;
62 uint32 value;
63 remain = m_depth;
64 value = 0;
65 while (remain > 0)
66 {
67 uint8 toread;
68 toread = remain;
69 if (toread > m_posinc) toread = m_posinc;
70 remain -= toread;
71 m_posinc -= toread;
72 value = (value) | ( (( (*m_srcit) >> (m_posinc) ) & ( ( 1 << toread ) - 1 ) ) << ( m_depth - 8 - remain ) );
73 if (m_posinc == 0)
74 {
75 m_srcit++;
76 m_posinc=8;
77 }
78 }
79 return value;
80}
81
82uint32 TIFFStreamContigAbove32::nextValue()
83{
84 uint8 remain;
85 uint32 value;
86 remain = m_depth;
87 value = 0;
88 while (remain > 0)
89 {
90 uint8 toread;
91 toread = remain;
92 if (toread > m_posinc) toread = m_posinc;
93 remain -= toread;
94 m_posinc -= toread;
95 if(remain < 32 )
96 {
97 value = (value) | ( (( (*m_srcit) >> (m_posinc) ) & ( ( 1 << toread ) - 1 ) ) << ( 24 - remain ) );
98 }
99 if (m_posinc == 0)
100 {
101 m_srcit++;
102 m_posinc=8;
103 }
104 }
105 return value;
106}
107
108TIFFStreamSeperate::TIFFStreamSeperate( uint8** srcs, uint8 nb_samples ,uint16 depth, uint32* lineSize) : TIFFStreamBase(depth), m_nb_samples(nb_samples)
109{
110 streams = new TIFFStreamContigBase*[nb_samples];
111 if(depth < 16)
112 {
113 for(uint8 i = 0; i < m_nb_samples; i++)
114 {
115 streams[i] = new TIFFStreamContigBelow16(srcs[i], depth, lineSize[i]);
116 }
117 } else if( depth < 32 )
118 {
119 for(uint8 i = 0; i < m_nb_samples; i++)
120 {
121 streams[i] = new TIFFStreamContigBelow32(srcs[i], depth, lineSize[i]);
122 }
123 } else {
124 for(uint8 i = 0; i < m_nb_samples; i++)
125 {
126 streams[i] = new TIFFStreamContigAbove32(srcs[i], depth, lineSize[i]);
127 }
128 }
129 restart();
130}
131
132TIFFStreamSeperate::~TIFFStreamSeperate()
133{
134 for(uint8 i = 0; i < m_nb_samples; i++)
135 {
136 delete streams[i];
137 }
138 delete[] streams;
139}
140
141uint32 TIFFStreamSeperate::nextValue()
142{
143 uint32 value = streams[ m_current_sample ]->nextValue();
144 if( (++m_current_sample) >= m_nb_samples)
145 m_current_sample = 0;
146 return value;
147}
148
149void TIFFStreamSeperate::restart()
150{
151 m_current_sample = 0;
152 for(uint8 i = 0; i < m_nb_samples; i++)
153 {
154 streams[i]->restart();
155 }
156}
157
158void TIFFStreamSeperate::moveToLine(uint32 lineNumber)
159{
160 for(uint8 i = 0; i < m_nb_samples; i++)
161 {
162 streams[i]->moveToLine(lineNumber);
163 }
164}