JeVois  1.5
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Jpeg.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/Jpeg.H>
19 #include <turbojpeg.h>
20 #include <stddef.h> // for size_t
21 
22 // ####################################################################################################
24 { itsCompressor = tjInitCompress(); }
25 
26 // ####################################################################################################
28 { tjDestroy(itsCompressor); }
29 
30 // ####################################################################################################
32 { return itsCompressor; }
33 
34 // ####################################################################################################
35 void jevois::convertYUYVtoYUV422(unsigned char const * src, int width, int height, unsigned char * dst)
36 {
37  size_t const sz = width * height;
38  unsigned char * uptr = dst + sz;
39  unsigned char * vptr = uptr + sz / 4;
40  size_t const sz2 = sz / 2;
41 
42  for (size_t i = 0; i < sz2; ++i)
43  {
44  *dst++ = *src++;
45  *uptr++ = *src++;
46  *dst++ = *src++;
47  *vptr++ = *src++;
48  }
49 }
50 
51 // ####################################################################################################
52 unsigned long jevois::compressBGRtoJpeg(unsigned char const * src, int width, int height, unsigned char * dst,
53  int quality)
54 {
55  unsigned long jpegsize = width * height * 2; // allocated output buffer size
56 
58 
59  tjCompress2(compressor, const_cast<unsigned char *>(src), width, 0, height, TJPF_BGR,
60  &dst, &jpegsize, TJSAMP_422, quality, TJFLAG_FASTDCT);
61 
62  return jpegsize;
63 }
64 
65 // ####################################################################################################
66 unsigned long jevois::compressRGBtoJpeg(unsigned char const * src, int width, int height, unsigned char * dst,
67  int quality)
68 {
69  unsigned long jpegsize = width * height * 2; // allocated output buffer size
70 
72 
73  tjCompress2(compressor, const_cast<unsigned char *>(src), width, 0, height, TJPF_RGB,
74  &dst, &jpegsize, TJSAMP_422, quality, TJFLAG_FASTDCT);
75 
76  return jpegsize;
77 }
78 
79 // ####################################################################################################
80 unsigned long jevois::compressRGBAtoJpeg(unsigned char const * src, int width, int height, unsigned char * dst,
81  int quality)
82 {
83  unsigned long jpegsize = width * height * 2; // allocated output buffer size
84 
86 
87  tjCompress2(compressor, const_cast<unsigned char *>(src), width, 0, height, TJPF_RGBA,
88  &dst, &jpegsize, TJSAMP_422, quality, TJFLAG_FASTDCT);
89 
90  return jpegsize;
91 }
92 
93 // ####################################################################################################
94 unsigned long jevois::compressGRAYtoJpeg(unsigned char const * src, int width, int height, unsigned char * dst,
95  int quality)
96 {
97  unsigned long jpegsize = width * height * 2; // allocated output buffer size
98 
100 
101  tjCompress2(compressor, const_cast<unsigned char *>(src), width, 0, height, TJPF_GRAY,
102  &dst, &jpegsize, TJSAMP_422, quality, TJFLAG_FASTDCT);
103 
104  return jpegsize;
105 }
106 
107 // ####################################################################################################
108 void jevois::compressBGRtoJpeg(cv::Mat const & src, RawImage & dst, int quality)
109 {
110  dst.buf->setBytesUsed(jevois::compressBGRtoJpeg(src.data, src.cols, src.rows, dst.pixelsw<unsigned char>(), quality));
111 }
112 
113 // ####################################################################################################
114 void jevois::compressRGBtoJpeg(cv::Mat const & src, RawImage & dst, int quality)
115 {
116  dst.buf->setBytesUsed(jevois::compressRGBtoJpeg(src.data, src.cols, src.rows, dst.pixelsw<unsigned char>(), quality));
117 }
118 
119 // ####################################################################################################
120 void jevois::compressRGBAtoJpeg(cv::Mat const & src, RawImage & dst, int quality)
121 {
122  dst.buf->setBytesUsed(jevois::compressRGBAtoJpeg(src.data, src.cols,src.rows, dst.pixelsw<unsigned char>(), quality));
123 }
124 
125 // ####################################################################################################
126 void jevois::compressGRAYtoJpeg(cv::Mat const & src, RawImage & dst, int quality)
127 {
128  dst.buf->setBytesUsed(jevois::compressGRAYtoJpeg(src.data, src.cols,src.rows, dst.pixelsw<unsigned char>(), quality));
129 }
T * pixelsw()
Shortcut access to pixels, read-write.
Definition: RawImageImpl.H:24
unsigned long compressGRAYtoJpeg(unsigned char const *src, int width, int height, unsigned char *dst, int quality=75)
Compress raw pixel buffer to jpeg.
Definition: Jpeg.C:94
std::shared_ptr< VideoBuf > buf
The pixel data buffer.
Definition: RawImage.H:149
void convertYUYVtoYUV422(unsigned char const *src, int width, int height, unsigned char *dst)
Helper to convert from packed YUYV to planar YUV422.
Definition: Jpeg.C:35
unsigned long compressBGRtoJpeg(unsigned char const *src, int width, int height, unsigned char *dst, int quality=75)
Compress raw pixel buffer to jpeg.
Definition: Jpeg.C:52
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
JpegCompressor()
Constructor, create the turbojpeg object.
Definition: Jpeg.C:23
void * compressor()
Access the compressor handle.
Definition: Jpeg.C:31
unsigned long compressRGBAtoJpeg(unsigned char const *src, int width, int height, unsigned char *dst, int quality=75)
Compress raw pixel buffer to jpeg.
Definition: Jpeg.C:80
virtual ~JpegCompressor()
Destructor, frees the turbojpeg object.
Definition: Jpeg.C:27
static JpegCompressor & instance()
Get the global, unique instance of the class T.
Definition: SingletonImpl.H:25
unsigned long compressRGBtoJpeg(unsigned char const *src, int width, int height, unsigned char *dst, int quality=75)
Compress raw pixel buffer to jpeg.
Definition: Jpeg.C:66