Permalink
Browse files

some initial code

  • Loading branch information...
1 parent b02ba3d commit 86532ead205e999b2c562dbe77641f22394decce @albertz committed Mar 2, 2011
Showing with 198 additions and 0 deletions.
  1. +53 −0 Endianess.h
  2. +50 −0 FileUtils.h
  3. +26 −0 Png.cpp
  4. +22 −0 Png.h
  5. +22 −0 Return.h
  6. +20 −0 StaticAssert.h
  7. +5 −0 test-png.cpp
View
@@ -0,0 +1,53 @@
+/*
+ (from OpenLieroX)
+
+ makros for endianess / swapping / conversions
+
+ Code under LGPL
+ by Albert Zeyer, Dark Charlie, 23-06-2007
+ */
+
+#ifndef __ENDIANSWAP_H__
+#define __ENDIANSWAP_H__
+
+#include <machine/endian.h>
+
+#if !defined(BYTE_ORDER)
+# error BYTE_ORDER not defined
+#endif
+#if BYTE_ORDER == LITTLE_ENDIAN
+# define EndianSwap(x) ;
+# define BEndianSwap(x) ByteSwap5(x);
+# define GetEndianSwapped(x) (x)
+#elif BYTEORDER == BIG_ENDIAN
+# define EndianSwap(x) ByteSwap5(x);
+# define BEndianSwap(x) ;
+# define GetEndianSwapped(x) (GetByteSwapped(x))
+#else
+# error unknown ENDIAN type
+#endif
+
+#include <algorithm>
+#include "StaticAssert.h"
+
+template <int n>
+void ByteSwap(unsigned char * b) {
+ static_assert(n == 1 || n % 2 == 0, n_must_be_equal);
+ for(int i = 0; i < n/2; ++i) {
+ std::swap(b[i], b[n - i - 1]);
+ }
+}
+
+template <typename T>
+T GetByteSwapped(T b) {
+ ByteSwap<sizeof(T)>(&b);
+ return b;
+}
+
+template <typename T>
+void ByteSwap5(T& x) {
+ ByteSwap<sizeof(T)>((unsigned char*) &x);
+}
+
+#endif
+
View
@@ -0,0 +1,50 @@
+/* File utils
+ * by Albert Zeyer, 2011
+ * code under LGPL
+ */
+
+#ifndef __AZ__FILEUTILS_H__
+#define __AZ__FILEUTILS_H__
+
+#include "Return.h"
+#include "Endianess.h"
+
+#include <cstdio>
+#include <cstring>
+
+static inline Return fread_bytes(FILE* f, char* d, size_t s) {
+ while(fread(d, s, 1, f) == 0) {
+ if(feof(f))
+ return "end-of-file";
+ if(ferror(f))
+ return "file-read-error";
+ }
+ return true;
+}
+
+template<typename T>
+static inline Return fread_bytes(FILE* f, T d[]) {
+ ASSERT( fread_bytes(f, d, sizeof(T)/sizeof(d[0])) );
+ return true;
+}
+
+template <typename T, typename _D>
+static Return fread_litendian(FILE* stream, _D& d) {
+ T data;
+ ASSERT( fread_bytes(stream, (char*) &data, sizeof(T)) );
+ EndianSwap(data);
+ d = (_D)data;
+ return true;
+}
+
+template <typename T, typename _D>
+static Return fread_bigendian(FILE* stream, _D& d) {
+ T data;
+ ASSERT( fread_bytes(stream, (char*) &data, sizeof(T)) );
+ BEndianSwap(data);
+ d = (_D)data;
+ return true;
+}
+
+
+#endif
View
@@ -0,0 +1,26 @@
+/* PNG parser
+ * by Albert Zeyer, 2011
+ * code under LGPL
+ */
+
+#include "Png.h"
+#include "FileUtils.h"
+
+#include <cstring>
+#include <stdint.h>
+
+Return png_read_sig(FILE* f) {
+ static const char PNGSIG[8] = {137,80,78,71,13,10,26,10};
+ char sig[sizeof(PNGSIG)];
+ ASSERT( fread_bytes(f, sig) );
+ if(memcmp(PNGSIG, sig, sizeof(PNGSIG)) != 0)
+ return "png-signature-wrong";
+ return true;
+}
+
+Return png_read_chunk(FILE* f, PngChunk& chunk) {
+ uint32_t len;
+ ASSERT( fread_litendian<uint32_t>(f, len) );
+ char type[4];
+
+}
View
@@ -0,0 +1,22 @@
+/* PNG parser
+ * by Albert Zeyer, 2011
+ * code under LGPL
+ */
+
+#ifndef __AZ__PNG_H__
+#define __AZ__PNG_H__
+
+#include "Return.h"
+
+#include <string>
+#include <cstdio>
+
+struct PngChunk {
+ std::string type;
+ std::string data;
+};
+
+Return png_read_sig(FILE* f);
+Return png_read_chunk(FILE* f, PngChunk& chunk);
+
+#endif
View
@@ -0,0 +1,22 @@
+/* Return value class
+ * by Albert Zeyer, 2011
+ * code under LGPL
+ */
+
+#ifndef __AZ__RETURN_H__
+#define __AZ__RETURN_H__
+
+#include <string>
+
+struct Return {
+ bool success;
+ std::string errmsg;
+
+ Return(bool s = true) : success(s) {}
+ Return(const std::string& errm) : success(false), errmsg(errm) {}
+ operator bool() { return success; }
+};
+
+#define ASSERT(x) { Return ___r = (x); if(!___r) return ___r; }
+
+#endif
View
@@ -0,0 +1,20 @@
+/*
+ * StaticAssert.h
+ * OpenLieroX
+ *
+ * Created by Albert Zeyer on 30.03.09.
+ * code under LGPL
+ *
+ */
+
+#ifndef __OLX__STATICASSERT_H__
+#define __OLX__STATICASSERT_H__
+
+template <bool b> class static_assert_failure;
+template <> class static_assert_failure<true> { char foo; };
+template <int s> class static_assert_test{};
+
+#define static_assert(X, desc) \
+ typedef static_assert_test< (int)sizeof(static_assert_failure< (bool)(X) >) > static_assert_typedef_##desc;
+
+#endif
View
@@ -0,0 +1,5 @@
+#include "Png.h"
+
+int main() {
+
+}

0 comments on commit 86532ea

Please sign in to comment.