Index: /trunk/FACT++/src/fitsCompressor.cc
===================================================================
--- /trunk/FACT++/src/fitsCompressor.cc	(revision 16804)
+++ /trunk/FACT++/src/fitsCompressor.cc	(revision 16805)
@@ -405,5 +405,5 @@
     private:
         ///@brief compresses one buffer of data, the one given by threadIndex
-        uint32_t compressBuffer(uint32_t threadIndex);
+        uint64_t compressBuffer(uint32_t threadIndex);
 
         ///@brief writes an already compressed buffer to disk
@@ -455,4 +455,18 @@
 #define COMPRESSED_FLAG 0x1
 #define UNCOMPRESSED_FLAG 0x0
+
+typedef struct TileHeader
+{
+  char     id[4];
+  uint64_t size;
+  TileHeader()
+  {
+      id[0]='T';
+      id[1]='I';
+      id[2]='L';
+      id[3]='E';
+      size=0;
+  };
+} __attribute__((__packed__)) TileHeader;
 
 template<>
@@ -633,5 +647,5 @@
     {
         _transposedBuffer[i] = new char[_rowWidth*_numRowsPerTile];
-        _compressedBuffer[i] = new char[_rowWidth*_numRowsPerTile + _columns.size()]; //use a bit more memory for compression flags
+        _compressedBuffer[i] = new char[_rowWidth*_numRowsPerTile + _columns.size() + sizeof(TileHeader)]; //use a bit more memory for compression flags
         if (_transposedBuffer[i] == NULL || _compressedBuffer[i] == NULL)
             return false;
@@ -639,4 +653,7 @@
         memset(_compressedBuffer[i], 0, 4);
         _compressedBuffer[i] = _compressedBuffer[i]+4;
+        //initialize the tile header
+        TileHeader tileHeader;
+        memcpy(_compressedBuffer[i], &tileHeader, sizeof(TileHeader));
     }
     return true;
@@ -1073,9 +1090,11 @@
         int64_t compressedOffset = 0;
         for (uint32_t i=0;i<_catalog.size();i++)
+        {
+            compressedOffset += sizeof(TileHeader);
+            heapSize += compressedOffset;
             for (uint32_t j=0;j<_catalog[i].size();j++)
             {
                 heapSize += _catalog[i][j].first;
                 //set the catalog offsets to their actual values
-                if (compressedOffset < 0) return false;
                 _catalog[i][j].second = compressedOffset;
                 compressedOffset += _catalog[i][j].first;
@@ -1083,4 +1102,5 @@
                 if (_catalog[i][j].first == 0) _catalog[i][j].second = 0;
             }
+        }
         setHeaderKey(HeaderEntry("PCOUNT", heapSize, "size of special data area"));
     }
@@ -1157,4 +1177,5 @@
         while (_threadStatus[_threadLooper] != _THREAD_WAIT_)
             usleep(100000);
+
         _threadNumRows[_threadLooper] = _totalNumRows;
         _threadStatus[_threadLooper] = _THREAD_COMPRESS_;
@@ -1223,10 +1244,10 @@
  *                  COMPRESS BUFFER
  ****************************************************************/
-uint32_t CompressedFitsWriter::compressBuffer(uint32_t threadIndex)
+uint64_t CompressedFitsWriter::compressBuffer(uint32_t threadIndex)
 {
     uint32_t thisRoundNumRows = (_threadNumRows[threadIndex]%_numRowsPerTile) ? _threadNumRows[threadIndex]%_numRowsPerTile : _numRowsPerTile;
     uint32_t offset=0;
     uint32_t currentCatalogRow = (_threadNumRows[threadIndex]-1)/_numRowsPerTile;
-    int64_t compressedOffset = 0;
+    uint64_t compressedOffset = sizeof(TileHeader); //skip the 'TILE' marker and tile size entry
 
     //now compress each column one by one by calling compression on arrays
@@ -1239,5 +1260,5 @@
         if (_columns[i].numElems() == 0) continue;
         //set the default byte telling if uncompressed the compressed Flag
-        int64_t previousOffset = compressedOffset;
+        uint64_t previousOffset = compressedOffset;
         _compressedBuffer[threadIndex][compressedOffset++] = COMPRESSED_FLAG;
         switch (compression)
@@ -1264,4 +1285,5 @@
         _catalog[currentCatalogRow][i].first = compressedOffset - _catalog[currentCatalogRow][i].second;
     }
+    memcpy(&(_compressedBuffer[threadIndex][4]), &compressedOffset, sizeof(uint64_t));
     return compressedOffset;
 }
