1 /*
2 Source File : InputPredictorPNGNoneStream.cpp
3
4
5 Copyright 2011 Gal Kahana PDFWriter
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19
20 */
21 #include "InputPredictorPNGNoneStream.h"
22
23 #include "Trace.h"
24
25
26 using namespace IOBasicTypes;
27
InputPredictorPNGNoneStream(void)28 InputPredictorPNGNoneStream::InputPredictorPNGNoneStream(void)
29 {
30 mSourceStream = NULL;
31 mBuffer = NULL;
32 mIndex = NULL;
33 mBufferSize = 0;
34 }
35
~InputPredictorPNGNoneStream(void)36 InputPredictorPNGNoneStream::~InputPredictorPNGNoneStream(void)
37 {
38 delete[] mBuffer;
39 delete mSourceStream;
40 }
41
InputPredictorPNGNoneStream(IByteReader * inSourceStream,IOBasicTypes::LongBufferSizeType inColumns)42 InputPredictorPNGNoneStream::InputPredictorPNGNoneStream(IByteReader* inSourceStream,IOBasicTypes::LongBufferSizeType inColumns)
43 {
44 mSourceStream = NULL;
45 mBuffer = NULL;
46 mIndex = NULL;
47 mBufferSize = 0;
48
49 Assign(inSourceStream,inColumns);
50 }
51
52
Read(Byte * inBuffer,LongBufferSizeType inBufferSize)53 LongBufferSizeType InputPredictorPNGNoneStream::Read(Byte* inBuffer,LongBufferSizeType inBufferSize)
54 {
55 LongBufferSizeType readBytes = 0;
56
57
58 // exhaust what's in the buffer currently
59 while(mBufferSize > (LongBufferSizeType)(mIndex - mBuffer) && readBytes < inBufferSize)
60 {
61 DecodeNextByte(inBuffer[readBytes]);
62 ++readBytes;
63 }
64
65 // now repeatedly read bytes from the input stream, and decode
66 while(readBytes < inBufferSize && mSourceStream->NotEnded())
67 {
68 if(mSourceStream->Read(mBuffer,mBufferSize) != mBufferSize)
69 {
70 TRACE_LOG("InputPredictorPNGNoneStream::Read, problem, expected columns number read. didn't make it");
71 readBytes = 0;
72 break;
73 }
74 mIndex = mBuffer+1; // skip the first tag
75
76 while(mBufferSize > (LongBufferSizeType)(mIndex - mBuffer) && readBytes < inBufferSize)
77 {
78 DecodeNextByte(inBuffer[readBytes]);
79 ++readBytes;
80 }
81 }
82 return readBytes;
83 }
84
NotEnded()85 bool InputPredictorPNGNoneStream::NotEnded()
86 {
87 return mSourceStream->NotEnded() || (LongBufferSizeType)(mIndex - mBuffer) < mBufferSize;
88 }
89
DecodeNextByte(Byte & outDecodedByte)90 void InputPredictorPNGNoneStream::DecodeNextByte(Byte& outDecodedByte)
91 {
92 outDecodedByte = *mIndex;
93
94 ++mIndex;
95 }
96
Assign(IByteReader * inSourceStream,IOBasicTypes::LongBufferSizeType inColumns)97 void InputPredictorPNGNoneStream::Assign(IByteReader* inSourceStream,IOBasicTypes::LongBufferSizeType inColumns)
98 {
99 mSourceStream = inSourceStream;
100
101 delete[] mBuffer;
102 mBufferSize = inColumns + 1;
103 mBuffer = new Byte[mBufferSize];
104 mIndex = mBuffer + mBufferSize;
105
106 }
107