YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
IplImage.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <cstdio>
8#include <cstring>
9
10#include <yarp/os/Log.h>
12
15inline int PAD_BYTES (int len, int pad)
16{
17 const int rem = len % pad;
18 return (rem != 0) ? (pad - rem) : 0;
19}
20
21char* AllocAligned (int size)
22{
23 char* ptr = new char[size + YARP_IMAGE_ALIGN];
24 const int rem = (((size_t)ptr) % YARP_IMAGE_ALIGN);
25 const char addbytes = YARP_IMAGE_ALIGN - rem;
26
27 char *p = ((char *)ptr) + addbytes;
28 *(p - 1) = addbytes;
29 return p;
30}
31
32void FreeAligned (char* ptr)
33{
34 if (ptr == nullptr)
35 {
36 return;
37 }
38
39 const char addbytes = *(ptr - 1);
40 delete[] (ptr - addbytes);
41}
42
44{
45 image->imageData = AllocAligned (image->imageSize);
46}
47
49{
50 if (image->imageData != nullptr)
51 {
52 FreeAligned (image->imageData);
53 }
54 image->imageData = nullptr;
55}
56
57
58/* /////////////////////////////////////////////////////////////////////////
59// Name: iplCreateImageHeader
60// Purpose: Creates an IPL image header according to the specified
61// attributes.
62// Returns: The newly constructed IPL image header.
63// Parameters:
64// nChannels - Number of channels in the image.
65// depth - Bit depth of pixels. Can be one of
66// IPL_DEPTH_1U,
67// IPL_DEPTH_8U,
68// IPL_DEPTH_8S,
69// IPL_DEPTH_16U,
70// IPL_DEPTH_16S,
71// IPL_DEPTH_32S.
72// IPL_DEPTH_32F.
73// align - Alignment of image data.
74// Can be IPL_ALIGN_4BYTES (IPL_ALIGN_DWORD) or
75// IPL_ALIGN_8BYTES (IPL_ALIGN_QWORD) or
76// IPL_ALIGN_16BYTES IPL_ALIGN_32BYTES.
77// width - Width of the image in pixels.
78// height - Height of the image in pixels.
79*/
80
82 (int nChannels, int depth,
83 int align,
84 int width, int height))
85{
86 switch (depth)
87 {
88 default:
89 case IPL_DEPTH_1U:
90 return nullptr;
91
92 case IPL_DEPTH_8U:
93 case IPL_DEPTH_8S:
94 case IPL_DEPTH_32F:
95 case IPL_DEPTH_16U:
96 case IPL_DEPTH_16S:
97 case IPL_DEPTH_32S:
98 break;
99 }
100
101 MiniIplImage* r = new MiniIplImage;
102 yAssert(r != nullptr);
103
104 r->nChannels = nChannels;
105 r->depth = depth;
106
107 r->align = align;
108 r->width = width;
109 r->height = height;
110
111 const int linew = width * (depth & IPL_DEPTH_MASK) / 8 * nChannels;
112 r->widthStep = linew + PAD_BYTES(linew, align);
113
114 r->imageSize = r->widthStep * height;
115 r->imageData = nullptr;
116
117 return r;
118}
119
121{
122 if (image == nullptr)
123 {
124 return;
125 }
126
127 if (image->imageData != nullptr)
128 {
129 FreeAligned (image->imageData);
130 }
131
132 delete image;
133}
134
135// not used outside this file.
136#undef IPLAPIIMPL
size_t size_t
void iplDeallocateHeader(MiniIplImage *image)
Definition IplImage.cpp:120
int PAD_BYTES(int len, int pad)
this might turn out to be useful.
Definition IplImage.cpp:15
void iplAllocateImage(MiniIplImage *image)
Definition IplImage.cpp:43
char * AllocAligned(int size)
Definition IplImage.cpp:21
MiniIplImage * iplCreateImageHeader(int nChannels, int depth, int align, int width, int height)
Definition IplImage.cpp:84
void iplDeallocateImage(MiniIplImage *image)
Definition IplImage.cpp:48
void FreeAligned(char *ptr)
Definition IplImage.cpp:32
#define IPLAPIIMPL(type, name, arg)
Definition for functions implemented within YARP_sig.
Definition IplImage.h:107
#define IPL_DEPTH_16U
Definition IplImage.h:60
#define IPL_DEPTH_1U
Definition IplImage.h:58
#define IPL_DEPTH_MASK
Definition IplImage.h:132
#define IPL_DEPTH_8U
Definition IplImage.h:59
#define IPL_DEPTH_8S
Definition IplImage.h:63
#define IPL_DEPTH_32S
Definition IplImage.h:65
struct _IplImage MiniIplImage
#define IPL_DEPTH_32F
Definition IplImage.h:61
#define YARP_IMAGE_ALIGN
Definition IplImage.h:144
#define IPL_DEPTH_16S
Definition IplImage.h:64
#define yAssert(x)
Definition Log.h:388
int nChannels
Most of OpenCV functions support 1,2,3 or 4 channels.
Definition IplImage.h:82
int height
image height in pixels
Definition IplImage.h:88
int align
Alignment of image rows (4 or 8).
Definition IplImage.h:85
char * imageData
pointer to aligned image data
Definition IplImage.h:92
int imageSize
image data size in bytes (==image->height*image->widthStep in case of interleaved data)
Definition IplImage.h:89
int widthStep
size of aligned image row in bytes
Definition IplImage.h:93
int depth
pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S, IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_...
Definition IplImage.h:83
int width
image width in pixels
Definition IplImage.h:87