1 //============================================================================
2 //  Copyright (c) Kitware, Inc.
3 //  All rights reserved.
4 //  See LICENSE.txt for details.
5 //
6 //  This software is distributed WITHOUT ANY WARRANTY; without even
7 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 //  PURPOSE.  See the above copyright notice for more information.
9 //============================================================================
10 #include <vtkm/io/EncodePNG.h>
11 #include <vtkm/io/FileUtils.h>
12 
13 #include <vtkm/cont/Logging.h>
14 #include <vtkm/internal/Configure.h>
15 
16 VTKM_THIRDPARTY_PRE_INCLUDE
17 #include <vtkm/thirdparty/lodepng/vtkmlodepng/lodepng.h>
18 VTKM_THIRDPARTY_POST_INCLUDE
19 
20 namespace vtkm
21 {
22 namespace io
23 {
24 
EncodePNG(std::vector<unsigned char> const & image,unsigned long width,unsigned long height,std::vector<unsigned char> & output_png)25 vtkm::UInt32 EncodePNG(std::vector<unsigned char> const& image,
26                        unsigned long width,
27                        unsigned long height,
28                        std::vector<unsigned char>& output_png)
29 {
30   // The default is 8 bit RGBA; does anyone care to have more options?
31   // We can certainly add them in a backwards-compatible way if need be.
32   vtkm::UInt32 error = vtkm::png::lodepng::encode(
33     output_png, image, static_cast<unsigned int>(width), static_cast<unsigned int>(height));
34   if (error)
35   {
36     VTKM_LOG_S(vtkm::cont::LogLevel::Error,
37                "LodePNG Encoder error number " << error << ": " << png::lodepng_error_text(error));
38   }
39   return error;
40 }
41 
42 
SavePNG(std::string const & filename,std::vector<unsigned char> const & image,unsigned long width,unsigned long height)43 vtkm::UInt32 SavePNG(std::string const& filename,
44                      std::vector<unsigned char> const& image,
45                      unsigned long width,
46                      unsigned long height)
47 {
48   if (!vtkm::io::EndsWith(filename, ".png"))
49   {
50     VTKM_LOG_S(vtkm::cont::LogLevel::Error,
51                "File " << filename << " does not end with .png; this is required.");
52   }
53 
54   std::vector<unsigned char> output_png;
55   vtkm::UInt32 error = EncodePNG(image, width, height, output_png);
56   if (!error)
57   {
58     vtkm::png::lodepng::save_file(output_png, filename);
59   }
60   return error;
61 }
62 }
63 } // namespace vtkm::io
64