summaryrefslogtreecommitdiffstats
path: root/filters/chalk/tiff/kis_tiff_stream.cpp
blob: 4c80fefcdd143514344b81bc0eaa2a7bbdc2572c (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
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
154
155
156
157
158
159
160
161
162
163
164
/*
 *  Copyright (c) 2005-2006 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
 
#include "kis_tiff_stream.h"

TIFFStreamContigBase::TIFFStreamContigBase( uint8* src, uint16 depth, uint32 lineSize ) : TIFFStreamBase(depth), m_src(src), m_lineSize(lineSize) { restart(); }

void TIFFStreamContigBase::restart()
{
    m_srcit = m_src;
    m_posinc = 8;
}

void TIFFStreamContigBase::moveToLine(uint32 lineNumber)
{
    m_srcit = m_src + lineNumber * m_lineSize;
    m_posinc = 8;
}

uint32 TIFFStreamContigBelow16::nextValue()
{
    uint8 remain;
    uint32 value;
    remain = m_depth;
    value = 0;
    while (remain > 0)
    {
        uint8 toread;
        toread = remain;
        if (toread > m_posinc) toread = m_posinc;
        remain -= toread;
        m_posinc -= toread;
        value = (value << toread) | (( (*m_srcit) >> (m_posinc) ) & ( ( 1 << toread ) - 1 ) );
        if (m_posinc == 0)
        {
            m_srcit++;
            m_posinc=8;
        }
    }
    return value;
}

uint32 TIFFStreamContigBelow32::nextValue()
{
    uint8 remain;
    uint32 value;
    remain = m_depth;
    value = 0;
    while (remain > 0)
    {
        uint8 toread;
        toread = remain;
        if (toread > m_posinc) toread = m_posinc;
        remain -= toread;
        m_posinc -= toread;
        value = (value) | ( (( (*m_srcit) >> (m_posinc) ) & ( ( 1 << toread ) - 1 ) ) << ( m_depth - 8 - remain ) );
        if (m_posinc == 0)
        {
            m_srcit++;
            m_posinc=8;
        }
    }
    return value;
}

uint32 TIFFStreamContigAbove32::nextValue()
{
    uint8 remain;
    uint32 value;
    remain = m_depth;
    value = 0;
    while (remain > 0)
    {
        uint8 toread;
        toread = remain;
        if (toread > m_posinc) toread = m_posinc;
        remain -= toread;
        m_posinc -= toread;
        if(remain < 32 )
        {
            value = (value) | ( (( (*m_srcit) >> (m_posinc) ) & ( ( 1 << toread ) - 1 ) ) << ( 24 - remain ) );
        }
        if (m_posinc == 0)
        {
            m_srcit++;
            m_posinc=8;
        }
    }
    return value;
}

TIFFStreamSeperate::TIFFStreamSeperate( uint8** srcs, uint8 nb_samples ,uint16 depth, uint32* lineSize) : TIFFStreamBase(depth), m_nb_samples(nb_samples)
{
    streams = new TIFFStreamContigBase*[nb_samples];
    if(depth < 16)
    {
        for(uint8 i = 0; i < m_nb_samples; i++)
        {
            streams[i] = new TIFFStreamContigBelow16(srcs[i], depth, lineSize[i]);
        }
    } else if( depth < 32 )
    {
        for(uint8 i = 0; i < m_nb_samples; i++)
        {
            streams[i] = new TIFFStreamContigBelow32(srcs[i], depth, lineSize[i]);
        }
    } else {
        for(uint8 i = 0; i < m_nb_samples; i++)
        {
            streams[i] = new TIFFStreamContigAbove32(srcs[i], depth, lineSize[i]);
        }
    }
    restart();
}

TIFFStreamSeperate::~TIFFStreamSeperate()
{
    for(uint8 i = 0; i < m_nb_samples; i++)
    {
        delete streams[i];
    }
    delete[] streams;
}

uint32 TIFFStreamSeperate::nextValue()
{
    uint32 value = streams[ m_current_sample ]->nextValue();
    if( (++m_current_sample) >= m_nb_samples)
        m_current_sample = 0;
    return value;
}

void TIFFStreamSeperate::restart()
{
    m_current_sample = 0;
    for(uint8 i = 0; i < m_nb_samples; i++)
    {
        streams[i]->restart();
    }
}

void TIFFStreamSeperate::moveToLine(uint32 lineNumber)
{
    for(uint8 i = 0; i < m_nb_samples; i++)
    {
        streams[i]->moveToLine(lineNumber);
    }
}