ofxDepthStream
libs/DepthStream/src/functional.h
Go to the documentation of this file.
1 //
2 // This file is part of the ofxDepthStream [https://github.com/fusefactory/ofxDepthStream]
3 // Copyright (C) 2018 Fuse srl
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
22 #pragma once
23 
24 #include "Frame.h"
25 #include "Buffer.h"
26 #include "Inflater.h"
27 #include "Compressor.h"
28 
35 namespace depth {
36 
37  // compress methods
38 
39  FrameRef compress(const void* data, size_t size) {
40  Compressor compressor;
41  if(!compressor.compress(data, size)) return nullptr;
42  return Frame::refToExternalData(compressor.getData(), compressor.getSize());
43  }
44 
45  FrameRef compress(Frame& instance) { return compress(instance.data(), instance.size()); }
46  FrameRef compress(FrameRef ref) { return compress(ref->data(), ref->size()); }
47 
48 
49  FrameRef inflate(const void* data, size_t size) {
50  Inflater inf;
51  if(!inf.inflate(data, size)) return nullptr;
52  return Frame::refToExternalData(inf.getData(), inf.getSize());
53  }
54 
55  // inflate methods
56 
57  FrameRef inflate(Frame& instance) { return inflate(instance.data(), instance.size()); }
58  FrameRef inflate(FrameRef ref) { return inflate(ref->data(), ref->size()); }
59 
60  // bit-size conversion
61  FrameRef convert_32bit_to_8bit(size_t targetSize, const void* data) {
62  void* converted = malloc(targetSize);
63  double multiplier = 1.0 * (2^8) / (2^32);
64 
65  for(int i=0; i<targetSize; i++) {
66  // unsigned char a = ((const unsigned char*)data)[i*2+1];
67  // unsigned char b = ((const unsigned char*)data)[i*4];
68  int byte0 = ((char*)data)[i*4 + 3];
69  int byte1 = ((char*)data)[i*4 + 2];
70  int byte2 = ((char*)data)[i*4 + 1];
71  int byte3 = ((char*)data)[i*4 + 0];
72  unsigned int val = byte0 << 24 | (byte1 & 0xFF) << 16 | (byte2 & 0xFF) << 8 | (byte3 & 0xFF);
73  ((unsigned char*)converted)[i] = (unsigned char)((double)val / multiplier);
74  }
75 
76  // when the returned shared_ptr<Frame> deallocates, it will free the memory we gave it
77  return Frame::refWithData(converted, targetSize);
78  }
79 
80  FrameRef convert_16bit_to_8bit(size_t targetSize, const void* data) {
81  void* converted = malloc(targetSize);
82  double multiplier = 1.0 * (2^8) / (2^16);
83 
84  for(int i=0; i<targetSize; i++) {
85  // unsigned char a = ((const unsigned char*)data)[i*2+1];
86  // unsigned char b = ((const unsigned char*)data)[i*2]+1;
87  // ((unsigned char*)converted)[i] = b; //(unsigned char)(((a << 8) | b) >> 8);//(unsigned char)val;
88 
89  int byte0 = ((char*)data)[i*2 + 0];
90  int byte1 = ((char*)data)[i*2 + 1];
91  unsigned int val = byte0 << 8 | (byte1 & 0xFF);
92  ((unsigned char*)converted)[i] = (unsigned char)((double)val / multiplier);
93  }
94 
95  // when the returned shared_ptr<Frame> deallocates, it will free the memory we gave it
96  return Frame::refWithData(converted, targetSize);
97  }
98 
99  // grayscale methods
100 
101  FrameRef convertTo8bitGrayscaleData(size_t texSize, const void* data) {
102  void* converted = malloc(texSize);
103 
104  for(int i=0; i<texSize; i++) {
105  // unsigned char a = ((const unsigned char*)data)[i*2+1];
106  unsigned char b = ((const unsigned char*)data)[i*2];
107  ((unsigned char*)converted)[i] = b; //(unsigned char)(((a << 8) | b) >> 8);//(unsigned char)val;
108  }
109 
110  // ofLogNotice() << "last value:" << (int)converted[texSize-1];
111 
112  // when ther returned shared_ptr<Frame> deallocates, it will free the memory we gave it
113  return Frame::refWithData(converted, texSize);
114  }
115 
116  FrameRef convertTo8bitGrayscaleData(int texWidth, int texHeight, const void* data) {
117  return convertTo8bitGrayscaleData(texWidth * texHeight * 1, data);
118  }
119 
120  FrameRef convertTo8bitGrayscaleData(int texWidth, int texHeight, FrameRef ref) {
121  return convertTo8bitGrayscaleData(texWidth, texHeight, ref->data());
122  }
123 
125  std::function<FrameRef(const void*, size_t)> grayscale8bitConverter(int texWidth, int texHeight) {
126  return [texWidth, texHeight](const void* data, size_t size){
127  return convertTo8bitGrayscaleData(texWidth, texHeight, data);
128  };
129  }
130 
131  // buffer methods
132 
134  if(auto r = buf.getRef()) {
135  r->convert(func);
136  buf.clear();
137  }
138  }
139 
141  emptyBuffer(buf, [func](const void* data, size_t size){
142  auto inflatedFrameRef = inflate(data, size);
143 
144  if(!inflatedFrameRef) {
145  // std::cerr << "Could not inflate " << size << "-byte buffer data" << std::endl;
146  return;
147  }
148 
149  func(inflatedFrameRef->data(), inflatedFrameRef->size());
150  });
151  }
152 
153 }
bool inflate(const void *data, size_t size)
Performs decompression on the provided data package.
Definition: Inflater.cpp:48
void clear()
Clear the buffer (doesn&#39;t trigger the output callback)
Definition: Buffer.h:52
A read-only wrapper around a data block of a specified size.
Definition: Frame.h:36
const void * getData() const
Returns a pointer to the inflated package data (will be NULL when no inflation is performed or after ...
Definition: Inflater.h:47
FrameRef inflate(const void *data, size_t size)
Definition: libs/DepthStream/src/functional.h:49
int getSize()
Definition: Compressor.h:33
size_t size()
Definition: Frame.h:78
Inflates ("decompresses") a package ("frame") of data compressed using zlib doCompression.
Definition: Inflater.h:29
contains all classes and functions of the DepthStream library.
Definition: Buffer.h:22
size_t getSize() const
Returns the inflated size of the last inflate operation.
Definition: Inflater.h:44
void emptyAndInflateBuffer(Buffer &buf, Frame::InputFunc func)
Definition: libs/DepthStream/src/functional.h:140
std::function< void(const void *, size_t)> InputFunc
Definition: Frame.h:40
std::function< FrameRef(const void *, size_t)> grayscale8bitConverter(int texWidth, int texHeight)
Generates converter lambda for specified width/height.
Definition: libs/DepthStream/src/functional.h:125
FrameRef convert_16bit_to_8bit(size_t targetSize, const void *data)
Definition: libs/DepthStream/src/functional.h:80
Compressed a package (frame) of data using zlib compression.
Definition: Compressor.h:29
FrameRef convert_32bit_to_8bit(size_t targetSize, const void *data)
Definition: libs/DepthStream/src/functional.h:61
bool compress(const void *data, size_t size)
Definition: Compressor.cpp:25
virtual FrameRef getRef()
Definition: Buffer.h:38
const void * data()
Definition: Frame.h:75
FrameRef compress(const void *data, size_t size)
Definition: libs/DepthStream/src/functional.h:39
std::shared_ptr< Frame > FrameRef
Definition: Frame.h:25
void emptyBuffer(Buffer &buf, Frame::InputFunc func)
Definition: libs/DepthStream/src/functional.h:133
static FrameRef refWithData(void *data, size_t size)
Initializes a Frame with owned data ("adopts" the provided data)
Definition: Frame.h:51
Manages a reference to a Frame instance.
Definition: Buffer.h:29
const void * getData()
Definition: Compressor.h:32
FrameRef convertTo8bitGrayscaleData(size_t texSize, const void *data)
Definition: libs/DepthStream/src/functional.h:101
static FrameRef refToExternalData(const void *data, size_t size)
Initializes a frame with external data.
Definition: Frame.h:46