EigerDecompress.cpp 3.62 KB
Newer Older
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2015
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This 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 3 of the License, or
// (at your option) any later version.
//
// This software 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 this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
#include "lz4.h"
23
#include "bitshuffle.h"
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include "EigerDecompress.h"
#include "EigerStream.h"

#include "processlib/LinkTask.h"
#include "processlib/ProcessExceptions.h"

using namespace lima;
using namespace lima::Eiger;

class _DecompressTask : public LinkTask
{
  DEB_CLASS_NAMESPC(DebModCamera,"_DecompressTask","Eiger");
public:
  _DecompressTask(Stream& stream) : m_stream(stream) {}
  virtual Data process(Data&);

private:
  Stream& m_stream;
};

45
void _expand(void *src,Data& dst)
46
47
48
49
50
51
52
53
54
55
56
57
{
  int nbItems = dst.size() / dst.depth();
  unsigned short* src_data = (unsigned short*)src;
  unsigned int* dst_data = (unsigned int*)dst.data();
  while(nbItems)
    {
      *dst_data = unsigned(*src_data);
      ++dst_data,++src_data,--nbItems;
    }
  dst.type = Data::UINT32;
}

58
Data _DecompressTask::process(Data& out)
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
59
60
61
{
  void *msg_data;
  size_t msg_size;
62
  int depth;
63
64
  void *lima_buffer = out.data();
  if(!m_stream.get_msg(lima_buffer,msg_data,msg_size,depth))
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
65
    throw ProcessException("_DecompressTask: can't find compressed message");
66
  int size;
67
68
  bool expand_16_to_32bit = ((out.depth() == 4) && (depth == 2));
  size = out.size() / (expand_16_to_32bit ? 2 : 1);
69
70
  Camera::CompressionType type;
  m_stream.getCompressionType(type);
71
72
73
74
75
76
77
78
  bool decompress = (type != Camera::NoCompression);
  void *aux_buffer = NULL;
  if(expand_16_to_32bit && decompress)
    if(posix_memalign(&aux_buffer,16,size))
      throw ProcessException("Can't allocate temporary memory");

  void *decompress_out = expand_16_to_32bit ? aux_buffer : lima_buffer;
  int return_code = 0;
79
  if (type == Camera::LZ4) {
80
81
    return_code = LZ4_decompress_fast((const char*)msg_data,(char*)decompress_out,size);
  } else if (type == Camera::BSLZ4) {
82
    size_t nb_elements = size / depth;
83
    return_code = bshuf_decompress_lz4(msg_data, decompress_out, nb_elements, depth, 0);
84
  }
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
85
86
  if(return_code < 0)
    {
87
      if(aux_buffer) free(aux_buffer);
88

Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
89
90
91
      char ErrorBuff[1024];
      snprintf(ErrorBuff,sizeof(ErrorBuff),
	       "_DecompressTask: decompression failed, (error code: %d) (data size %d)",
92
	       return_code,out.size());
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
93
94
      throw ProcessException(ErrorBuff);
    }
95
96
97
98
99
100
101
102
103
104
105

  if(expand_16_to_32bit) {
    void *expand_src = decompress ? decompress_out : msg_data;
    _expand(expand_src,out);
  } else if(!decompress)
    memcpy(lima_buffer, msg_data, size);
  
  if(aux_buffer)
    free(aux_buffer);

  return out;
Sebastien Petitdemange's avatar
Sebastien Petitdemange committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
}

Decompress::Decompress(Stream& stream) :
  m_decompress_task(new _DecompressTask(stream))
{
}

Decompress::~Decompress()
{
  m_decompress_task->unref();
}

LinkTask* Decompress::getReconstructionTask()
{
  return m_decompress_task;
}

void Decompress::setActive(bool active)
{
  reconstructionChange(active ? m_decompress_task : NULL);
}