Skip to content

Commit

Permalink
Cleanup the rest of bytestreamutils.h and migrate TGALoader.cpp.
Browse files Browse the repository at this point in the history
Require the <cstdint> header in configure.ac.
  • Loading branch information
codereader committed Sep 7, 2017
1 parent a54d082 commit d387000
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 91 deletions.
3 changes: 3 additions & 0 deletions configure.ac
Expand Up @@ -48,6 +48,9 @@ AC_PROG_LIBTOOL
# We require a C++11-compliant compiler, without nonstandard extensions
AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory])

# We require the cstdint header to be present
AC_CHECK_HEADER([cstdint], [], [AC_MSG_ERROR([Could not find the <cstdint> header.])])

# Optional features
AC_ARG_ENABLE([debug],
[AS_HELP_STRING([--enable-debug],
Expand Down
80 changes: 2 additions & 78 deletions libs/bytestreamutils.h
@@ -1,56 +1,12 @@
/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once

#if defined(__GNUC__)

#define _ISOC9X_SOURCE 1
#define _ISOC99_SOURCE 1

#define __USE_ISOC9X 1
#define __USE_ISOC99 1

#include <stdint.h>

#endif
// Fixed width integer types are defined in the C++11 header <cstdint>
#include <cstdint>

#include "idatastream.h"
#include <ostream>
#include <algorithm>

// if C99 is unavailable, fall back to the types most likely to be the right sizes
#if !defined(int16_t)
typedef signed short int16_t;
#endif
#if !defined(uint16_t)
typedef unsigned short uint16_t;
#endif
#if !defined(int32_t)
typedef signed int int32_t;
#endif
#if !defined(uint32_t)
typedef unsigned int uint32_t;
#endif


namespace stream
{

Expand Down Expand Up @@ -117,35 +73,3 @@ inline InputStream::byte_type readByte(InputStream& stream)
}

}

template<typename InputStreamType, typename Type>
inline void istream_read_little_endian(InputStreamType& istream, Type& value)
{
istream.read(reinterpret_cast<typename InputStreamType::byte_type*>(&value), sizeof(Type));
#if defined(__BIG_ENDIAN__)
std::reverse(reinterpret_cast<typename InputStreamType::byte_type*>(&value), reinterpret_cast<typename InputStreamType::byte_type*>(&value) + sizeof(Type));
#endif
}

template<typename InputStreamType, typename Type>
inline void istream_read_big_endian(InputStreamType& istream, Type& value)
{
istream.read(reinterpret_cast<typename InputStreamType::byte_type*>(&value), sizeof(Type));
#if !defined(__BIG_ENDIAN__)
std::reverse(reinterpret_cast<typename InputStreamType::byte_type*>(&value), reinterpret_cast<typename InputStreamType::byte_type*>(&value) + sizeof(Type));
#endif
}

template<typename InputStreamType>
inline void istream_read_byte(InputStreamType& istream, typename InputStreamType::byte_type& b)
{
istream.read(&b, 1);
}

template<typename InputStreamType>
inline typename InputStreamType::byte_type istream_read_byte(InputStreamType& istream)
{
typename InputStreamType::byte_type b;
istream.read(&b, sizeof(typename InputStreamType::byte_type));
return b;
}
26 changes: 13 additions & 13 deletions plugins/image/TGALoader.cpp
Expand Up @@ -282,19 +282,19 @@ inline void targa_header_read_istream(TargaHeader& targa_header, stream::Pointer
{
static_assert(sizeof(unsigned short) == sizeof(uint16_t), "TGA Loader Code relies on unsigned short being 16 bits wide!");

targa_header.id_length = istream_read_byte(istream);
targa_header.colormap_type = istream_read_byte(istream);
targa_header.image_type = istream_read_byte(istream);

targa_header.colormap_index = stream::readLittleEndian<uint16_t>(istream);
targa_header.colormap_length = stream::readLittleEndian<uint16_t>(istream);
targa_header.colormap_size = istream_read_byte(istream);
targa_header.x_origin = stream::readLittleEndian<uint16_t>(istream);
targa_header.y_origin = stream::readLittleEndian<uint16_t>(istream);
targa_header.width = stream::readLittleEndian<uint16_t>(istream);
targa_header.height = stream::readLittleEndian<uint16_t>(istream);
targa_header.pixel_size = istream_read_byte(istream);
targa_header.attributes = istream_read_byte(istream);
targa_header.id_length = stream::readByte(istream);
targa_header.colormap_type = stream::readByte(istream);
targa_header.image_type = stream::readByte(istream);

targa_header.colormap_index = stream::readLittleEndian<uint16_t>(istream);
targa_header.colormap_length = stream::readLittleEndian<uint16_t>(istream);
targa_header.colormap_size = stream::readByte(istream);
targa_header.x_origin = stream::readLittleEndian<uint16_t>(istream);
targa_header.y_origin = stream::readLittleEndian<uint16_t>(istream);
targa_header.width = stream::readLittleEndian<uint16_t>(istream);
targa_header.height = stream::readLittleEndian<uint16_t>(istream);
targa_header.pixel_size = stream::readByte(istream);
targa_header.attributes = stream::readByte(istream);

if (targa_header.id_length != 0)
istream.seek(targa_header.id_length); // skip TARGA image comment
Expand Down

0 comments on commit d387000

Please sign in to comment.