VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grimageconverter.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // grimageconverter.cpp
19 //
20 
21 #include "gdef.h"
22 #include "grimageconverter.h"
23 #include "grimagetype.h"
24 #include "grimagedecoder.h"
25 #include "grcolourspace.h"
26 #include "grjpeg.h"
27 #include "gdebug.h"
28 #include <sstream>
29 
31 {
32  m_image_tmp = Image( shared_ptr<ImageBuffer>(new ImageBuffer) , ImageType() ) ;
33 }
34 
36 {
37  return ( type.isRaw() || type.isPnm() ||
38  ( type.isJpeg() && Gr::Jpeg::available() ) || ( type.isPng() && Gr::Png::available() ) ) &&
39  type.dx() > 0 && type.dy() > 0 && ( type.channels() == 1 || type.channels() == 3 ) ;
40 }
41 
42 bool Gr::ImageConverter::toRaw( Image image_in , Image & image_out , int scale , bool monochrome_out )
43 {
44  return toRawImp( image_in , image_out , std::max(1,scale) , monochrome_out ) ;
45 }
46 
47 bool Gr::ImageConverter::toRawImp( Image image_in , Image & image_out , int scale , bool monochrome_out )
48 {
49  if( image_in.type().isRaw() && scale == 1 && (image_in.type().channels()==1 || !monochrome_out) )
50  {
51  image_out = image_in ;
52  return true ;
53  }
54  else if( convertible(image_in.type()) )
55  {
56  // delegate anything-to-raw to the ImageDecoder
57  ImageType type_out = ImageType::raw( image_in.type() , scale , monochrome_out ) ;
58  ImageBuffer * image_out_p = Gr::Image::blank( image_out , type_out , false ) ;
59  ImageData image_data_out( *image_out_p , type_out.dx() , type_out.dy() , type_out.channels() ) ;
60  m_decoder.setup( scale , monochrome_out ) ;
61  type_out = m_decoder.decode( image_in.type() , image_in.data() , image_data_out ) ;
62  return image_out.valid() ;
63  }
64  else
65  {
66  G_DEBUG( "Gr::ImageConverter::toRawImp: invalid input image type: " << image_in.type() ) ;
67  return false ;
68  }
69 }
70 
71 bool Gr::ImageConverter::toJpeg( Image image_in , Image & image_out , int scale , bool monochrome_out )
72 {
73  return toJpegImp( image_in , image_out , std::max(1,scale) , monochrome_out ) ;
74 }
75 
76 bool Gr::ImageConverter::toJpegImp( Image image_in , Image & image_out , int scale , bool monochrome_out )
77 {
78  if( convertible(image_in.type()) && image_in.type().isRaw() )
79  {
80  const ImageData data_in( const_cast<ImageBuffer&>(image_in.data()) , image_in.type().dx() , image_in.type().dy() , image_in.type().channels() ) ;
81 
82  ImageType type_out = ImageType::jpeg( image_in.type() , scale , monochrome_out ) ;
83  shared_ptr<ImageBuffer> ptr_out = image_out.recycle() ;
84 
85  m_jpeg_writer.setup( scale , monochrome_out ) ;
86  m_jpeg_writer.encode( data_in , *ptr_out ) ;
87  image_out = Image( ptr_out , type_out ) ;
88  return true ;
89  }
90  else if( convertible(image_in.type()) && image_in.type().isJpeg() )
91  {
92  if( scale == 1 && !monochrome_out )
93  {
94  image_out = image_in ;
95  return true ;
96  }
97  else
98  {
99  return
100  toRawImp( image_in , m_image_tmp , scale , false ) &&
101  toJpegImp( m_image_tmp , image_out , 1 , monochrome_out ) ;
102  }
103  }
104  else
105  {
106  return false ;
107  }
108 }
109 
110 /// \file grimageconverter.cpp
bool isRaw() const
Returns true if a raw image type.
A holder for image data, having eight bits per sample and one or three channels.
Definition: grimagedata.h:46
bool isJpeg() const
Returns true if a jpeg image type.
int channels() const
Returns the number of channels.
Definition: grimagetype.h:197
static ImageType jpeg(int dx, int dy, int channels=3)
Factory function for a jpeg image type.
ImageConverter()
Default constructor.
An encapsulation of image type, including width, height and number of channels, with support for a st...
Definition: grimagetype.h:43
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
Definition: grimagebuffer.h:47
bool toRaw(Image image_in, Image &image_out, int scale=1, bool monochrome_out=false)
Converts the image to raw format. Returns a false on error.
static ImageType raw(int dx, int dy, int channels)
Factory function for a raw image type.
A class holding shared read-only image data (Gr::ImageBuffer) and its associated image type (Gr::Imag...
Definition: grimage.h:41
static bool available()
Returns true if a jpeg library is available.
static ImageBuffer * blank(Image &, ImageType raw_type, bool contiguous=false)
Factory function for a not-really-blank raw image that is temporarily writable via the returned image...
Definition: grimage.cpp:54
bool isPng() const
Returns true if a png image type.
int dy() const
Returns the image height.
Definition: grimagetype.h:191
bool valid() const
Returns !empty() && type().valid().
Definition: grimage.cpp:80
ImageType type() const
Returns the image type.
Definition: grimage.cpp:85
static bool convertible(Gr::ImageType)
Returns true if the image type is convertible.
const ImageBuffer & data() const
Returns the image data.
Definition: grimage.cpp:112
bool toJpeg(Image image_in, Image &image_out, int scale=1, bool monochrome_out=false)
Converts the image to jpeg format. Returns false on error.
int dx() const
Returns the image width.
Definition: grimagetype.h:185
shared_ptr< ImageBuffer > recycle()
Creates a new shared pointer that can be deposited into a new Image, but if the current Image is not ...
Definition: grimage.cpp:117
static bool available()
Returns true if the png library is available.
Definition: grpng_none.cpp:27
bool isPnm() const
Returns true if a pnm image type.