JeVois  1.2
JeVois Smart Embedded Machine Vision Toolkit
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: std::fill(pixelsw<unsigned short>(), pixelsw<unsigned short>() + width * height,
57  jevois::yuyv::Black); break;
58  case V4L2_PIX_FMT_GREY: memset(pixelsw<void>(), 0, bytesize()); break;
59  case V4L2_PIX_FMT_SRGGB8: memset(pixelsw<void>(), 0, bytesize()); break;
60  case V4L2_PIX_FMT_RGB565: memset(pixelsw<void>(), 0, bytesize()); break;
61  case V4L2_PIX_FMT_MJPEG: break;
62  case V4L2_PIX_FMT_BGR24: memset(pixelsw<void>(), 0, bytesize()); break;
63  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fmt));
64  }
65 }
66 
67 // ####################################################################################################
68 void jevois::RawImage::require(char const * info, unsigned int w, unsigned int h, unsigned int f) const
69 {
70  if (w != width || h != height || f != fmt)
71  LFATAL("Incorrect format for RawImage " << info << ": want " << w << 'x' << h << ' ' << jevois::fccstr(f)
72  << " but image is " << width << 'x' << height << ' ' << jevois::fccstr(fmt));
73 }
74 
75 // ####################################################################################################
76 bool jevois::RawImage::coordsOk(int x, int y) const
77 {
78  if (x >= 0 && x < int(width) && y >= 0 && y < int(height)) return true;
79  else return false;
80 }
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:68
void invalidate()
Invalidate the image by zero'ing out the pointer to pixel buffer and the dims and format...
Definition: RawImage.C:42
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:49
bool valid() const
Check whether the image has a valid pixel buffer.
Definition: RawImage.C:46
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:36
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
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:212
unsigned int bytesperpix() const
Helper function to get the number of bytes/pixel given the RawImage pixel format. ...
Definition: RawImage.C:34
void clear()
Clear the pixels to all black.
Definition: RawImage.C:50
bool coordsOk(int x, int y) const
Helper function to check that coords are within image bounds.
Definition: RawImage.C:76
RawImage()
Default constructor, uninitialized.
Definition: RawImage.C:24