JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
RawImage.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #include <jevois/Image/RawImage.H>
19 #include <jevois/Util/Utils.H>
20 
21 #include <algorithm> // for std::fill
22 
23 // ####################################################################################################
25 { }
26 
27 // ####################################################################################################
28 jevois::RawImage::RawImage(unsigned int w, unsigned int h, unsigned int f, float fs,
29  std::shared_ptr<VideoBuf> b, size_t bindex) :
30  width(w), height(h), fmt(f), fps(fs), buf(b), bufindex(bindex)
31 { }
32 
33 // ####################################################################################################
34 unsigned int jevois::RawImage::bytesperpix() const
35 { return jevois::v4l2BytesPerPix(fmt); }
36 
37 // ####################################################################################################
38 unsigned int jevois::RawImage::bytesize() const
39 { return width * height * jevois::v4l2BytesPerPix(fmt); }
40 
41 // ####################################################################################################
43 { buf.reset(); width = 0; height = 0; fmt = 0; fps = 0.0F; }
44 
45 // ####################################################################################################
47 { return (buf.get() != nullptr); }
48 
49 // ####################################################################################################
51 {
52  if (valid() == false) LFATAL("Cannot clear because not valid()");
53 
54  switch (fmt)
55  {
56  case V4L2_PIX_FMT_YUYV:
57  std::fill(pixelsw<unsigned short>(), pixelsw<unsigned short>() + width * height, jevois::yuyv::Black);
58  break;
59 
60  case V4L2_PIX_FMT_MJPEG:
61  break;
62 
63  case V4L2_PIX_FMT_GREY:
64  case V4L2_PIX_FMT_SRGGB8:
65 #ifdef JEVOIS_PRO
66  case V4L2_PIX_FMT_SBGGR16:
67  case V4L2_PIX_FMT_SGRBG16:
68 #endif
69  case V4L2_PIX_FMT_RGB565:
70  case V4L2_PIX_FMT_BGR24:
71  case V4L2_PIX_FMT_RGB24:
72  case V4L2_PIX_FMT_RGB32:
73  memset(pixelsw<void>(), 0, bytesize());
74  break;
75  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fmt));
76  }
77 }
78 
79 // ####################################################################################################
80 void jevois::RawImage::require(char const * info, unsigned int w, unsigned int h, unsigned int f) const
81 {
82  if (w != width || h != height || f != fmt)
83  LFATAL("Incorrect format for RawImage " << info << ": want " << w << 'x' << h << ' ' << jevois::fccstr(f)
84  << " but image is " << width << 'x' << height << ' ' << jevois::fccstr(fmt));
85 }
86 
87 // ####################################################################################################
88 bool jevois::RawImage::coordsOk(int x, int y) const
89 {
90  if (x >= 0 && x < int(width) && y >= 0 && y < int(height)) return true;
91  else return false;
92 }
jevois::RawImage::bytesperpix
unsigned int bytesperpix() const
Helper function to get the number of bytes/pixel given the RawImage pixel format.
Definition: RawImage.C:34
jevois::RawImage::clear
void clear()
Clear the pixels to all black.
Definition: RawImage.C:50
RawImage.H
jevois::RawImage::bytesize
unsigned int bytesize() const
Helper function to get the total number of bytes in the RawImage, i.e., width * height * bytesperpix(...
Definition: RawImage.C:38
jevois::RawImage::valid
bool valid() const
Check whether the image has a valid pixel buffer.
Definition: RawImage.C:46
jevois::RawImage::invalidate
void invalidate()
Invalidate the image by zero'ing out the pointer to pixel buffer and the dims and format.
Definition: RawImage.C:42
jevois::RawImage::require
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
Require a particular image size and format, issue a fatal error message and throw if no match.
Definition: RawImage.C:80
jevois::v4l2BytesPerPix
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:141
jevois::fccstr
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:45
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::RawImage::coordsOk
bool coordsOk(int x, int y) const
Helper function to check that coords are within image bounds.
Definition: RawImage.C:88
Utils.H
h
int h
Definition: GUIhelper.C:2373
jevois::RawImage::RawImage
RawImage()
Default constructor, uninitialized.
Definition: RawImage.C:24