Critical Link MityCam SoC Firmware  1.0
Critical Link MityCam SoC Firmware
Image.h
Go to the documentation of this file.
1 /*
2  * Image.h
3  *
4  * Created on: May 2, 2014
5  * Author: mitycam
6  */
7 
8 #ifndef IMAGE_H_
9 #define IMAGE_H_
10 
11 #include <cstring>
12 #include <cstdlib>
13 
14 #include "Utility/RAMStreamer.h"
15 
16 #include "Utility/Types.h"
17 #include "IOCallback.h"
18 
19 #include "CameraTypes.h"
20 
21 typedef struct {
33 
34 class tcImage
35 {
36 private:
37  static const int32_t gnMAX_IMAGE_SIZE_BYTES = 1024*1024*100;
38 
39  uint16 *mpDataVector;
40  int32 mnWidth;
41  int32 mnHeight;
42 
56  void unpack32bit(uint32 *apData, int32 anWidth, int32 anHeight)
57  {
58  int32 cntr = 0;
59  for(int i = 0; i < anWidth*anHeight;) {
60  mpDataVector[i++] = ((apData[cntr] & (0x00000FFF)));
61  mpDataVector[i++] = ((apData[cntr] & (0x00FFF000)) >> 12);
62  mpDataVector[i++] = ((apData[cntr] & (0xFF000000)) >> 24) | ((apData[cntr + 1] & (0x0000000F)) << 8);
63  cntr++;
64  mpDataVector[i++] = ((apData[cntr] & (0x0000FFF0)) >> 4);
65  mpDataVector[i++] = ((apData[cntr] & (0x0FFF0000)) >> 16);
66  mpDataVector[i++] = ((apData[cntr] & (0xF0000000)) >> 28) | ((apData[cntr + 1] & (0x000000FF)) << 4);
67  cntr++;
68  mpDataVector[i++] = ((apData[cntr] & (0x000FFF00)) >> 8);
69  mpDataVector[i++] = ((apData[cntr] & (0xFFF00000)) >> 20);
70  cntr++;
71  }
72  }
73 
87  void unpack16bit(uint16 *apData, int32 anWidth, int32 anHeight)
88  {
89  uint16 word = 0;
90  int8 method = 0;
91  int32 cntr = 0;
92  for(int i = 0; i < anWidth*anHeight; i++) {
93  if(method == 0) {
94  // 0th word, get the left aligned pixel.
95  word = ((apData[cntr] & (0x0FFF)));
96  } else if(method == 1) {
97  // 1st word, get the nibble + byte
98  word = ((apData[cntr] & (0xF000)) >> 12) | ((apData[cntr + 1] & (0xFF)) << 4);
99  ++cntr;
100  } else if(method == 2) {
101  word = ((apData[cntr] & (0xFF00)) >> 8) | ((apData[cntr + 1] & (0xF)) << 8);
102  ++cntr;
103  } else if(method == 3) {
104  word = ((apData[cntr] & (0xFFF0)) >> 4);
105  ++cntr;
106  }
107  method = (method + 1) % 4; //Wrap every 4 words
108 
109  mpDataVector[i] = word;
110  }
111  }
112 
123  void alloc_and_unpack(unsigned char* apData, int32_t anWidth,
124  int32_t anHeight, tcRAMStreamer::tePackingMode aePacking)
125  {
126  switch(aePacking) {
127  default:
129  {
130  int num_bytes = sizeof(uint16_t)*anWidth*anHeight;
131  mpDataVector = (uint16_t*)malloc(num_bytes);
132  if (mpDataVector)
133  memcpy(mpDataVector, apData, num_bytes);
134  break;
135  }
137  {
138  // we will unpack to a 16 bit buffer
139  int num_bytes = sizeof(uint16_t)*anWidth*anHeight;
140  mpDataVector = (uint16_t*)malloc(num_bytes);
141  if (mpDataVector)
142  unpack16bit((uint16_t*)apData, anWidth, anHeight);
143  break;
144  }
145  case tcRAMStreamer::eeMono12p: /* GigE format? Not supported */
146  break;
147 
149  {
150  // we will unpack to an 8 bit buffer
151  int num_bytes = sizeof(uint8_t)*anWidth*anHeight;
152  mpDataVector = (uint16_t*)malloc(num_bytes);
153  if (mpDataVector)
154  memcpy(mpDataVector, apData, num_bytes);
155  break;
156  }
157  }
158  }
159 
160 public:
175  tcImage(void *apAddr, int anNumBytes, int32 anWidth, int32 anHeight,
177  bool abHasHeader = false)
178  : mnWidth(anWidth), mnHeight(anHeight), mbOwnData(anNumBytes)
179  {
180  unsigned char* lpData = (unsigned char*)apAddr;
181 
182  if(abHasHeader) {
183  memcpy(&msFrameHeader, lpData, sizeof(tsFrameHeader));
184 
185  // Increment the address
186  lpData = lpData + sizeof(tsFrameHeader);
187 
188  } else {
189  memset(&msFrameHeader, 0, sizeof(msFrameHeader));
190  }
191  mpDataVector = (reinterpret_cast<uint16 *>(lpData));
192  if (!mnWidth) mnWidth = msFrameHeader.mnFrameWidth;
193  if (!mnHeight) mnHeight = msFrameHeader.mnFrameHeight;
194 
195  /* if anNumBytes is non-zero, allocate and unpack data buffer */
196  if (anNumBytes) {
197  alloc_and_unpack(lpData, mnWidth, mnHeight, aePacking);
198  }
199 
200  }
201 
208  tcImage(void *apAddr, tcRAMStreamer::tePackingMode aePacking)
209  : mpDataVector(NULL)
210  {
211  unsigned char* lpData = (unsigned char*)apAddr;
212  memcpy(&msFrameHeader, lpData, sizeof(tsFrameHeader));
213  mnWidth = msFrameHeader.mnFrameWidth;
214  mnHeight = msFrameHeader.mnFrameHeight;
215  // sanity check the header
216  lpData = lpData + sizeof(tsFrameHeader);
217  if (mnWidth * mnHeight * 2 <= gnMAX_IMAGE_SIZE_BYTES)
218  alloc_and_unpack(lpData, mnWidth, mnHeight, aePacking);
219  }
220 
221  virtual ~tcImage()
222  {
223  if(mbOwnData)
224  free(mpDataVector);
225  }
226 
232  uint16_t* data()
233  {
234  return mpDataVector;
235  }
236 
237  const uint16_t* data() const
238  {
239  return mpDataVector;
240  }
241 
242  uint8_t* data8()
243  {
244  return (uint8_t*)mpDataVector;
245  }
246  const uint8_t* data8() const
247  {
248  return (const uint8_t*)mpDataVector;
249  }
250 
254  int32_t getWidth() const
255  {
256  return mnWidth;
257  }
258 
262  int32_t getHeight() const
263  {
264  return mnHeight;
265  }
266 
272  return msFrameHeader;
273  }
274 
275 private:
276  tsFrameHeader msFrameHeader;
277  bool mbOwnData;
278 };
279 
280 #endif /* IMAGE_H_ */
tsFrameHeader::mnSlowTimestamp
uint32 mnSlowTimestamp
Definition: Image.h:28
int32
int32_t int32
Definition: Types.h:8
tcImage::getHeight
int32_t getHeight() const
Definition: Image.h:262
tcImage::tcImage
tcImage(void *apAddr, tcRAMStreamer::tePackingMode aePacking)
Definition: Image.h:208
Types.h
RAMStreamer.h
tcImage::tcImage
tcImage(void *apAddr, int anNumBytes, int32 anWidth, int32 anHeight, tcRAMStreamer::tePackingMode aePacking=tcRAMStreamer::eeMono16, bool abHasHeader=false)
Definition: Image.h:175
tsFrameHeader::mnFrameHeight
uint16 mnFrameHeight
Definition: Image.h:23
tcRAMStreamer::eeMono8p
@ eeMono8p
Definition: RAMStreamer.h:104
CameraTypes.h
tcImage::getWidth
int32_t getWidth() const
Definition: Image.h:254
tsFrameHeader
Definition: Image.h:21
tcRAMStreamer::tePackingMode
tePackingMode
Definition: RAMStreamer.h:101
tcRAMStreamer::eeMono12p
@ eeMono12p
Definition: RAMStreamer.h:103
tcImage::data8
const uint8_t * data8() const
Definition: Image.h:246
tcImage::~tcImage
virtual ~tcImage()
Definition: Image.h:221
tcImage::data
uint16_t * data()
Definition: Image.h:232
tsFrameHeader::mnTimestamp
uint32 mnTimestamp
Definition: Image.h:27
tsFrameHeader::mnColOffset
uint16 mnColOffset
Definition: Image.h:24
tcRAMStreamer::eeMono12packed
@ eeMono12packed
Definition: RAMStreamer.h:105
tcImage::data8
uint8_t * data8()
Definition: Image.h:242
tcImage
Definition: Image.h:34
tcImage::data
const uint16_t * data() const
Definition: Image.h:237
IOCallback.h
tsFrameHeader::mnRowOffset
uint16 mnRowOffset
Definition: Image.h:25
tcRAMStreamer::eeMono16
@ eeMono16
Definition: RAMStreamer.h:102
uint32
uint32_t uint32
Definition: Types.h:11
tsFrameHeader::mnReserved5
uint32 mnReserved5
Definition: Image.h:29
tsFrameHeader::mnFrameWidth
uint16 mnFrameWidth
Definition: Image.h:22
uint16
uint16_t uint16
Definition: Types.h:10
tcImage::getFrameHeader
tsFrameHeader getFrameHeader() const
Definition: Image.h:271
tsFrameHeader::mnReserved6
uint32 mnReserved6
Definition: Image.h:30
tsFrameHeader::mnReserved7
uint32 mnReserved7
Definition: Image.h:31
tsFrameHeader::mnFrameIndex
uint32 mnFrameIndex
Definition: Image.h:26
int8
int8_t int8
Definition: Types.h:6