PCXTranslator now uses the StreamBuffer class
fixed StreamBuffer::Read() to handle bigger reads added a write mode revised code style and license header git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24068 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "StreamBuffer.h"
|
||||||
#include "PCX.h"
|
#include "PCX.h"
|
||||||
#include "PCXTranslator.h"
|
#include "PCXTranslator.h"
|
||||||
|
|
||||||
@@ -69,7 +70,7 @@ pcx_header::SwapFromHost()
|
|||||||
|
|
||||||
|
|
||||||
static status_t
|
static status_t
|
||||||
convert_data_to_bits(pcx_header &header, BPositionIO &source,
|
convert_data_to_bits(pcx_header &header, StreamBuffer &source,
|
||||||
BPositionIO &target)
|
BPositionIO &target)
|
||||||
{
|
{
|
||||||
uint16 bitsPerPixel = header.bitsPerPixel;
|
uint16 bitsPerPixel = header.bitsPerPixel;
|
||||||
@@ -224,8 +225,12 @@ PCX::identify(BMessage *settings, BPositionIO &stream, uint8 &type, int32 &bitsP
|
|||||||
status_t
|
status_t
|
||||||
PCX::convert_pcx_to_bits(BMessage *settings, BPositionIO &source, BPositionIO &target)
|
PCX::convert_pcx_to_bits(BMessage *settings, BPositionIO &source, BPositionIO &target)
|
||||||
{
|
{
|
||||||
|
StreamBuffer sourceBuf(&source, 2048);
|
||||||
|
if (sourceBuf.InitCheck() != B_OK)
|
||||||
|
return B_IO_ERROR;
|
||||||
|
|
||||||
pcx_header header;
|
pcx_header header;
|
||||||
if (source.Read(&header, sizeof(pcx_header)) != (ssize_t)sizeof(pcx_header))
|
if (sourceBuf.Read(&header, sizeof(pcx_header)) != (ssize_t)sizeof(pcx_header))
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
header.SwapToHost();
|
header.SwapToHost();
|
||||||
@@ -253,5 +258,5 @@ PCX::convert_pcx_to_bits(BMessage *settings, BPositionIO &source, BPositionIO &t
|
|||||||
swap_data(B_UINT32_TYPE, &bitsHeader, sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN);
|
swap_data(B_UINT32_TYPE, &bitsHeader, sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN);
|
||||||
target.Write(&bitsHeader, sizeof(TranslatorBitmap));
|
target.Write(&bitsHeader, sizeof(TranslatorBitmap));
|
||||||
|
|
||||||
return convert_data_to_bits(header, source, target);
|
return convert_data_to_bits(header, sourceBuf, target);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +1,13 @@
|
|||||||
/*****************************************************************************/
|
/*
|
||||||
// StreamBuffer
|
* Copyright 2003-2008, Haiku, Inc. All rights reserved.
|
||||||
// Written by Michael Wilber, OBOS Translation Kit Team
|
* Distributed under the terms of the MIT License.
|
||||||
//
|
*
|
||||||
// StreamBuffer.cpp
|
* Authors :
|
||||||
//
|
* Michael Wilber
|
||||||
// This class is for buffering data from a BPositionIO object in order to
|
* Jérôme Duval
|
||||||
// improve performance for cases when small amounts of data are frequently
|
*/
|
||||||
// read from a BPositionIO object.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// Copyright (c) 2003 OpenBeOS Project
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
|
||||||
// to deal in the Software without restriction, including without limitation
|
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included
|
|
||||||
// in all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
/*****************************************************************************/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
#include "StreamBuffer.h"
|
#include "StreamBuffer.h"
|
||||||
|
|
||||||
#ifndef min
|
#ifndef min
|
||||||
@@ -61,25 +37,20 @@
|
|||||||
//
|
//
|
||||||
// Returns:
|
// Returns:
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
StreamBuffer::StreamBuffer(BPositionIO *pstream, size_t nbuffersize,
|
StreamBuffer::StreamBuffer(BPositionIO *pstream, size_t nbuffersize, bool toRead)
|
||||||
bool binitialread)
|
|
||||||
{
|
{
|
||||||
fpStream = pstream;
|
fStream = pstream;
|
||||||
fpBuffer = NULL;
|
fBuffer = NULL;
|
||||||
fnBufferSize = 0;
|
fBufferSize = 0;
|
||||||
fnLen = 0;
|
fLen = 0;
|
||||||
fnPos = 0;
|
fPos = 0;
|
||||||
|
fToRead = toRead;
|
||||||
|
|
||||||
if (!pstream)
|
if (!pstream)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fnBufferSize = max(nbuffersize, MIN_BUFFER_SIZE);
|
fBufferSize = max(nbuffersize, MIN_BUFFER_SIZE);
|
||||||
fpBuffer = new uint8[fnBufferSize];
|
fBuffer = new uint8[fBufferSize];
|
||||||
if (fpBuffer && binitialread)
|
|
||||||
ReadStream();
|
|
||||||
// Fill the buffer with data so that
|
|
||||||
// object is prepared for first call to
|
|
||||||
// Read()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
@@ -97,13 +68,10 @@ StreamBuffer::StreamBuffer(BPositionIO *pstream, size_t nbuffersize,
|
|||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
StreamBuffer::~StreamBuffer()
|
StreamBuffer::~StreamBuffer()
|
||||||
{
|
{
|
||||||
fnBufferSize = 0;
|
if (!fToRead && fLen > 0)
|
||||||
fnLen = 0;
|
fStream->Write(fBuffer, fLen);
|
||||||
fnPos = 0;
|
delete[] fBuffer;
|
||||||
fpStream = NULL;
|
fBuffer = NULL;
|
||||||
|
|
||||||
delete[] fpBuffer;
|
|
||||||
fpBuffer = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
@@ -123,7 +91,7 @@ StreamBuffer::~StreamBuffer()
|
|||||||
status_t
|
status_t
|
||||||
StreamBuffer::InitCheck()
|
StreamBuffer::InitCheck()
|
||||||
{
|
{
|
||||||
if (fpStream && fpBuffer)
|
if (fStream && fBuffer)
|
||||||
return B_OK;
|
return B_OK;
|
||||||
else
|
else
|
||||||
return B_ERROR;
|
return B_ERROR;
|
||||||
@@ -147,38 +115,71 @@ StreamBuffer::InitCheck()
|
|||||||
// error code returned by BPositionIO::Read()
|
// error code returned by BPositionIO::Read()
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
ssize_t
|
ssize_t
|
||||||
StreamBuffer::Read(uint8 *pinto, size_t nbytes)
|
StreamBuffer::Read(void *pinto, size_t nbytes)
|
||||||
{
|
{
|
||||||
|
if (nbytes == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
ssize_t result = B_ERROR;
|
ssize_t result = B_ERROR;
|
||||||
size_t rd1 = 0, rd2 = 0;
|
|
||||||
|
size_t totalRead = min(nbytes, fLen - fPos);
|
||||||
|
memcpy(pinto, fBuffer + fPos, totalRead);
|
||||||
|
fPos += totalRead;
|
||||||
|
(uint8 *)pinto += totalRead;
|
||||||
|
nbytes -= totalRead;
|
||||||
|
|
||||||
rd1 = min(nbytes, fnLen - fnPos);
|
while (nbytes > 0) {
|
||||||
memcpy(pinto, fpBuffer + fnPos, rd1);
|
result = _ReadStream();
|
||||||
fnPos += rd1;
|
if (result <= 0)
|
||||||
|
|
||||||
if (rd1 < nbytes) {
|
|
||||||
pinto += rd1;
|
|
||||||
result = ReadStream();
|
|
||||||
if (result > 0) {
|
|
||||||
rd2 = min(nbytes - rd1, fnLen);
|
|
||||||
memcpy(pinto, fpBuffer, rd2);
|
|
||||||
fnPos += rd2;
|
|
||||||
} else
|
|
||||||
// return error code or zero
|
|
||||||
return result;
|
return result;
|
||||||
|
if (result > 0) {
|
||||||
|
size_t left = min(nbytes, fLen - fPos);
|
||||||
|
memcpy(pinto, fBuffer + fPos, left);
|
||||||
|
fPos += left;
|
||||||
|
(uint8 *)pinto += left;
|
||||||
|
nbytes -= left;
|
||||||
|
totalRead += left;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rd1 + rd2;
|
return totalRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
// Write
|
||||||
|
//
|
||||||
|
// Copies up to nbytes of data from pinto into the stream
|
||||||
|
//
|
||||||
|
// Parameters: pinto, the buffer to be copied from
|
||||||
|
// nbytes, the maximum number of bytes to copy
|
||||||
|
//
|
||||||
|
// Returns: the number of bytes successfully read or an
|
||||||
|
// error code returned by BPositionIO::Read()
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
void
|
||||||
|
StreamBuffer::Write(void *pinto, size_t nbytes)
|
||||||
|
{
|
||||||
|
if (nbytes < fBufferSize - fLen) {
|
||||||
|
memcpy(fBuffer + fLen, pinto, nbytes);
|
||||||
|
fLen += nbytes;
|
||||||
|
} else {
|
||||||
|
if (fLen > 0) {
|
||||||
|
fStream->Write(fBuffer, fLen);
|
||||||
|
fLen = 0;
|
||||||
|
}
|
||||||
|
fStream->Write(pinto, nbytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
// Seek
|
// Seek
|
||||||
//
|
//
|
||||||
// Seeks the stream to the given position and refreshes the
|
// Seeks the stream to the given position. If the seek operation fails,
|
||||||
// read buffer. If the seek operation fails, the read buffer
|
// the read buffer will be reset.
|
||||||
// will be reset.
|
|
||||||
//
|
//
|
||||||
// Preconditions: fpBuffer must be allocated and fnBufferSize
|
// Preconditions: fBuffer must be allocated and fBufferSize
|
||||||
// must be valid
|
// must be valid
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
@@ -191,11 +192,10 @@ StreamBuffer::Read(uint8 *pinto, size_t nbytes)
|
|||||||
bool
|
bool
|
||||||
StreamBuffer::Seek(off_t position)
|
StreamBuffer::Seek(off_t position)
|
||||||
{
|
{
|
||||||
fnLen = 0;
|
fLen = 0;
|
||||||
fnPos = 0;
|
fPos = 0;
|
||||||
|
|
||||||
if (fpStream->Seek(position, SEEK_SET) == position) {
|
if (fStream->Seek(position, SEEK_SET) == position) {
|
||||||
ReadStream();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,11 +203,11 @@ StreamBuffer::Seek(off_t position)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
// ReadStream
|
// _ReadStream
|
||||||
//
|
//
|
||||||
// Fills the stream buffer with data read in from the stream
|
// Fills the stream buffer with data read in from the stream
|
||||||
//
|
//
|
||||||
// Preconditions: fpBuffer must be allocated and fnBufferSize
|
// Preconditions: fBuffer must be allocated and fBufferSize
|
||||||
// must be valid
|
// must be valid
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
@@ -218,13 +218,12 @@ StreamBuffer::Seek(off_t position)
|
|||||||
// error code returned by BPositionIO::Read()
|
// error code returned by BPositionIO::Read()
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
ssize_t
|
ssize_t
|
||||||
StreamBuffer::ReadStream()
|
StreamBuffer::_ReadStream()
|
||||||
{
|
{
|
||||||
ssize_t rd;
|
ssize_t len = fStream->Read(fBuffer, fBufferSize);
|
||||||
rd = fpStream->Read(fpBuffer, fnBufferSize);
|
if (len < 0)
|
||||||
if (rd >= 0) {
|
return len;
|
||||||
fnLen = rd;
|
fLen = len;
|
||||||
fnPos = 0;
|
fPos = 0;
|
||||||
}
|
return fLen;
|
||||||
return rd;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,11 @@
|
|||||||
/*****************************************************************************/
|
/*
|
||||||
// StreamBuffer
|
* Copyright 2003-2008, Haiku, Inc. All rights reserved.
|
||||||
// Written by Michael Wilber, Haiku Translation Kit Team
|
* Distributed under the terms of the MIT License.
|
||||||
//
|
*
|
||||||
// StreamBuffer.h
|
* Authors :
|
||||||
//
|
* Michael Wilber
|
||||||
// This class is for buffering data from a BPositionIO object in order to
|
* Jérôme Duval
|
||||||
// improve performance for cases when small amounts of data are frequently
|
*/
|
||||||
// read from a BPositionIO object.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// Copyright (c) 2003 Haiku, Inc.
|
|
||||||
//
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
|
||||||
// to deal in the Software without restriction, including without limitation
|
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
||||||
// and/or sell copies of the Software, and to permit persons to whom the
|
|
||||||
// Software is furnished to do so, subject to the following conditions:
|
|
||||||
//
|
|
||||||
// The above copyright notice and this permission notice shall be included
|
|
||||||
// in all copies or substantial portions of the Software.
|
|
||||||
//
|
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
||||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
||||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
||||||
// DEALINGS IN THE SOFTWARE.
|
|
||||||
/*****************************************************************************/
|
|
||||||
|
|
||||||
#ifndef STREAM_BUFFER_H
|
#ifndef STREAM_BUFFER_H
|
||||||
#define STREAM_BUFFER_H
|
#define STREAM_BUFFER_H
|
||||||
@@ -39,32 +16,37 @@
|
|||||||
|
|
||||||
class StreamBuffer {
|
class StreamBuffer {
|
||||||
public:
|
public:
|
||||||
StreamBuffer(BPositionIO *pstream, size_t nbuffersize, bool binitialread);
|
StreamBuffer(BPositionIO *stream, size_t bufferSize, bool toRead = true);
|
||||||
~StreamBuffer();
|
~StreamBuffer();
|
||||||
|
|
||||||
status_t InitCheck();
|
status_t InitCheck();
|
||||||
// Determines whether the constructor failed or not
|
// Determines whether the constructor failed or not
|
||||||
|
|
||||||
ssize_t Read(uint8 *pinto, size_t nbytes);
|
ssize_t Read(void *buffer, size_t size);
|
||||||
// copy nbytes from the stream into pinto
|
// copy nbytes from the stream into pinto
|
||||||
|
|
||||||
|
void Write(void *buffer, size_t size);
|
||||||
|
// copy nbytes from the stream into pinto
|
||||||
|
|
||||||
bool Seek(off_t position);
|
bool Seek(off_t position);
|
||||||
// seek the stream to the given position
|
// seek the stream to the given position
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ssize_t ReadStream();
|
ssize_t _ReadStream();
|
||||||
// Load the stream buffer from the stream
|
// Load the stream buffer from the stream
|
||||||
|
|
||||||
BPositionIO *fpStream;
|
BPositionIO *fStream;
|
||||||
// stream object this object is buffering
|
// stream object this object is buffering
|
||||||
uint8 *fpBuffer;
|
uint8 *fBuffer;
|
||||||
// buffered data from fpStream
|
// buffered data from fpStream
|
||||||
size_t fnBufferSize;
|
size_t fBufferSize;
|
||||||
// number of bytes of memory allocated for fpBuffer
|
// number of bytes of memory allocated for fpBuffer
|
||||||
size_t fnLen;
|
size_t fLen;
|
||||||
// number of bytes of actual data in fpBuffer
|
// number of bytes of actual data in fpBuffer
|
||||||
size_t fnPos;
|
size_t fPos;
|
||||||
// current position in the buffer
|
// current position in the buffer
|
||||||
|
bool fToRead;
|
||||||
|
// whether the stream is to be read.
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1640,7 +1640,7 @@ TGATranslator::translate_from_tganmrle_to_bits(BPositionIO *inSource,
|
|||||||
memset(bitsRowData, 0xff, bitsRowBytes);
|
memset(bitsRowData, 0xff, bitsRowBytes);
|
||||||
uint8 *pbitspixel = bitsRowData;
|
uint8 *pbitspixel = bitsRowData;
|
||||||
uint8 packethead;
|
uint8 packethead;
|
||||||
StreamBuffer sbuf(inSource, TGA_STREAM_BUFFER_SIZE, true);
|
StreamBuffer sbuf(inSource, TGA_STREAM_BUFFER_SIZE);
|
||||||
ssize_t rd = 0;
|
ssize_t rd = 0;
|
||||||
if (sbuf.InitCheck() == B_OK)
|
if (sbuf.InitCheck() == B_OK)
|
||||||
rd = sbuf.Read(&packethead, 1);
|
rd = sbuf.Read(&packethead, 1);
|
||||||
@@ -1945,7 +1945,7 @@ TGATranslator::translate_from_tgamrle_to_bits(BPositionIO *inSource,
|
|||||||
memset(bitsRowData, 0xff, bitsRowBytes);
|
memset(bitsRowData, 0xff, bitsRowBytes);
|
||||||
uint8 *pbitspixel = bitsRowData;
|
uint8 *pbitspixel = bitsRowData;
|
||||||
uint8 packethead;
|
uint8 packethead;
|
||||||
StreamBuffer sbuf(inSource, TGA_STREAM_BUFFER_SIZE, true);
|
StreamBuffer sbuf(inSource, TGA_STREAM_BUFFER_SIZE);
|
||||||
ssize_t rd = 0;
|
ssize_t rd = 0;
|
||||||
if (sbuf.InitCheck() == B_OK)
|
if (sbuf.InitCheck() == B_OK)
|
||||||
rd = sbuf.Read(&packethead, 1);
|
rd = sbuf.Read(&packethead, 1);
|
||||||
|
|||||||
Reference in New Issue
Block a user