1 // Copyright (C) 2014  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #ifndef DLIB_SAVE_JPEG_Hh_
4 #define DLIB_SAVE_JPEG_Hh_
5 
6 #include "save_jpeg_abstract.h"
7 
8 #include "../enable_if.h"
9 #include "../matrix.h"
10 #include "../array2d.h"
11 #include "../pixel.h"
12 #include "../image_processing/generic_image.h"
13 #include <string>
14 
15 namespace dlib
16 {
17 
18 // ----------------------------------------------------------------------------------------
19 
20     void save_jpeg (
21         const array2d<rgb_pixel>& img,
22         const std::string& filename,
23         int quality = 75
24     );
25 
26 // ----------------------------------------------------------------------------------------
27 
28     void save_jpeg (
29         const array2d<unsigned char>& img,
30         const std::string& filename,
31         int quality = 75
32     );
33 
34 // ----------------------------------------------------------------------------------------
35 
36     template <
37         typename image_type
38         >
39     typename disable_if<is_matrix<image_type> >::type save_jpeg(
40         const image_type& img,
41         const std::string& filename,
42         int quality = 75
43     )
44     {
45         // Convert any kind of grayscale image to an unsigned char image
46         if (pixel_traits<typename image_traits<image_type>::pixel_type>::grayscale)
47         {
48             array2d<unsigned char> temp;
49             assign_image(temp, img);
50             save_jpeg(temp, filename, quality);
51         }
52         else
53         {
54             // This is some other kind of color image so just save it as an RGB image.
55             array2d<rgb_pixel> temp;
56             assign_image(temp, img);
57             save_jpeg(temp, filename, quality);
58         }
59     }
60 
61 // ----------------------------------------------------------------------------------------
62 
63     template <
64         typename EXP
65         >
66     void save_jpeg(
67         const matrix_exp<EXP>& img,
68         const std::string& file_name,
69         int quality = 75
70     )
71     {
72         array2d<typename EXP::type> temp;
73         assign_image(temp, img);
74         save_jpeg(temp, file_name, quality);
75     }
76 
77 // ----------------------------------------------------------------------------------------
78 
79 }
80 
81 #endif // DLIB_SAVE_JPEG_Hh_
82 
83