compress.cpp 3.57 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library 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 Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <cassert>
#include "Utils/compress.h"
#include "zlib.h"

29 30 31
#include <iostream>
#include <vector>
#include <string.h>
32
#include <algorithm>
33

Sylvain Thery's avatar
Sylvain Thery committed
34 35 36 37 38 39
namespace CGoGN
{

namespace Utils
{

40
void zlibVTUWriteCompressed( unsigned char* input, unsigned int nbBytes, std::ofstream& fout)
Sylvain Thery's avatar
Sylvain Thery committed
41
{
42 43 44 45

	std::cout << "Compressor Block " << std::endl;

	const int CHUNK=1024*256;
Sylvain Thery's avatar
Sylvain Thery committed
46 47 48 49 50 51 52 53
	int level = 6; // compression level 

	z_stream strm;
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	int ret = deflateInit(&strm, level);
	assert(ret == Z_OK); 
Sylvain Thery's avatar
Sylvain Thery committed
54 55
	if (ret != Z_OK)
		return;
Sylvain Thery's avatar
Sylvain Thery committed
56

57 58 59 60 61 62 63
	unsigned char* bufferOut = new unsigned char[nbBytes];
	unsigned char* ptrBufferOut = bufferOut;

	std::vector<unsigned int> header;
	header.reserve(1024);
	header.resize(3);

Sylvain Thery's avatar
Sylvain Thery committed
64 65 66 67 68 69 70 71 72 73
	unsigned char* ptrData = input;
	int remain = nbBytes;

	while (remain >0)
	{
		strm.avail_in = std::min(remain,CHUNK);	// taille buffer
		strm.next_in = ptrData;					// ptr buffer
		do
		{
			strm.avail_out = CHUNK;
74
			strm.next_out = ptrBufferOut;
Sylvain Thery's avatar
Sylvain Thery committed
75 76 77 78 79 80
			if (remain>= CHUNK)
				ret = deflate(&strm, 0);
			else
				ret = deflate(&strm, 1);
			assert(ret != Z_STREAM_ERROR); 
			unsigned int have = CHUNK - strm.avail_out;
81 82
			ptrBufferOut+=have;
			header.push_back(have);
Sylvain Thery's avatar
Sylvain Thery committed
83 84 85 86 87 88
		} while (strm.avail_out == 0);

		remain -= CHUNK;
		ptrData += CHUNK;	
	}
	deflateEnd(&strm);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

	header[0] = header.size()-3;
	header[1] = CHUNK;
	if (remain != 0)
		header[2] = remain +CHUNK;
	else header[2] = 0;

	std::cout << "HEADER "<< std::endl;
	for (unsigned int i=0; i<header.size(); ++i)
		std::cout << header[i]<< std::endl;

	fout.write((char*)&header[0], header.size()*sizeof(unsigned int));
	fout.write((char*)bufferOut, ptrBufferOut - bufferOut);

	std::cout << "DATA "<<  ptrBufferOut - bufferOut << "bytes writen"<< std::endl;

	delete[] bufferOut;
Sylvain Thery's avatar
Sylvain Thery committed
106 107
}

108

Sylvain Thery's avatar
Sylvain Thery committed
109 110
}
}