Merge "Image Usage Removal" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / loader-png.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/internal/imaging/common/loader-png.h>
19
20 #include <cstring>
21
22 #include <zlib.h>
23 #include <png.h>
24
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/legacy/tizen/platform-capabilities.h>
27 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
28
29 namespace Dali
30 {
31 namespace TizenPlatform
32 {
33
34 namespace
35 {
36
37 // simple class to enforce clean-up of PNG structures
38 struct auto_png
39 {
40   auto_png(png_structp& _png, png_infop& _info)
41   : png(_png),
42     info(_info)
43   {
44   }
45
46   ~auto_png()
47   {
48     if(NULL != png)
49     {
50       png_destroy_read_struct(&png, &info, NULL);
51     }
52   }
53
54   png_structp& png;
55   png_infop& info;
56 }; // struct auto_png;
57
58 bool LoadPngHeader(FILE *fp, unsigned int &width, unsigned int &height, png_structp &png, png_infop &info)
59 {
60   png_byte header[8] = { 0 };
61
62   // Check header to see if it is a PNG file
63   size_t size = fread(header, 1, 8, fp);
64   if(size != 8)
65   {
66     return false;
67   }
68
69   if(png_sig_cmp(header, 0, 8))
70   {
71     return false;
72   }
73
74   png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
75
76   if(!png)
77   {
78     DALI_LOG_WARNING("Can't create PNG read structure\n");
79     return false;
80   }
81
82   info = png_create_info_struct(png);
83   if(!info)
84   {
85     DALI_LOG_WARNING("png_create_info_struct failed\n");
86     return false;
87   }
88
89   png_set_expand(png);
90
91   if(setjmp(png_jmpbuf(png)))
92   {
93     DALI_LOG_WARNING("error during png_init_io\n");
94     return false;
95   }
96
97   png_init_io(png, fp);
98   png_set_sig_bytes(png, 8);
99
100   // read image info
101   png_read_info(png, info);
102
103   // dimensions
104   width = png_get_image_width(png, info);
105   height = png_get_image_height(png, info);
106
107   return true;
108 }
109
110 } // namespace - anonymous
111
112 bool LoadPngHeader( const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height )
113 {
114   png_structp png = NULL;
115   png_infop info = NULL;
116   auto_png autoPng(png, info);
117
118   bool success = LoadPngHeader( input.file, width, height, png, info );
119
120   return success;
121 }
122
123 bool LoadBitmapFromPng( const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap )
124 {
125   png_structp png = NULL;
126   png_infop info = NULL;
127   auto_png autoPng(png, info);
128
129   /// @todo: consider parameters
130   unsigned int y;
131   unsigned int width, height;
132   png_bytep *rows;
133   unsigned int bpp = 0; // bytes per pixel
134   bool valid = false;
135
136   // Load info from the header
137   if( !LoadPngHeader( input.file, width, height, png, info ) )
138   {
139     return false;
140   }
141
142   Pixel::Format pixelFormat = Pixel::RGBA8888;
143
144   // decide pixel format
145   unsigned int colordepth = png_get_bit_depth(png, info);
146
147   // Ask PNGLib to convert high precision images into something we can use:
148   if (colordepth == 16)
149   {
150     png_set_strip_16(png);
151     colordepth = 8;
152   }
153
154   png_byte colortype = png_get_color_type(png, info);
155
156   if( colortype == PNG_COLOR_TYPE_GRAY ||
157       colortype == PNG_COLOR_TYPE_GRAY_ALPHA )
158   {
159     if( colortype == PNG_COLOR_TYPE_GRAY )
160     {
161       pixelFormat = Pixel::L8;
162       if( png_get_valid(png, info, PNG_INFO_tRNS) )
163       {
164         colortype = PNG_COLOR_TYPE_GRAY_ALPHA;
165         /* expand transparency entry -> alpha channel if present */
166         png_set_tRNS_to_alpha(png);
167         pixelFormat = Pixel::LA88;
168       }
169     }
170     else
171     {
172       pixelFormat = Pixel::LA88;
173     }
174
175     if( colordepth < 8 )
176     {
177       /* expand gray (w/reduced bits) -> 8-bit RGB if necessary */
178       png_set_expand_gray_1_2_4_to_8(png);
179       /* pack all pixels to byte boundaries */
180       png_set_packing(png);
181     }
182     valid = true;
183   }
184   else if(colortype == PNG_COLOR_TYPE_RGB )
185   {
186     switch(colordepth)
187     {
188       case 8:
189       {
190         pixelFormat = Pixel::RGB888;
191         valid = true;
192         break;
193       }
194       case 5:      /// @todo is this correct for RGB16 5-6-5 ?
195       {
196         pixelFormat = Pixel::RGB565;
197         valid = true;
198         break;
199       }
200       default:
201       {
202         break;
203       }
204     }
205   }
206   else if(colortype == PNG_COLOR_TYPE_RGBA)
207   {
208     switch(colordepth)
209     {
210       case 8:
211       {
212         pixelFormat = Pixel::RGBA8888;
213         valid = true;
214         break;
215       }
216       default:
217       {
218         break;
219       }
220     }
221   }
222   else if(colortype == PNG_COLOR_TYPE_PALETTE)
223   {
224     switch(colordepth)
225     {
226       case 1:
227       {
228         pixelFormat = Pixel::LA88;
229         valid = true;
230         break;
231       }
232
233       case 2:
234       case 4:
235       case 8:
236       {
237         /* Expand paletted or RGB images with transparency to full alpha channels
238          * so the data will be available as RGBA quartets. PNG_INFO_tRNS = 0x10
239          */
240         if(png_get_valid(png, info, PNG_INFO_tRNS) == 0x10)
241         {
242           pixelFormat = Pixel::RGBA8888;
243           valid = true;
244         }
245         else
246         {
247           pixelFormat = Pixel::RGB888;
248           png_set_packing(png);
249           png_set_packswap(png);
250           png_set_palette_to_rgb(png);
251           valid = true;
252         }
253         break;
254       }
255       default:
256       {
257         break;
258       }
259     }
260   }
261
262   if( !valid )
263   {
264     DALI_LOG_WARNING( "Unsupported png format\n" );
265     return false;
266   }
267
268   // bytes per pixel
269   bpp = Pixel::GetBytesPerPixel(pixelFormat);
270
271   png_read_update_info(png, info);
272
273   if(setjmp(png_jmpbuf(png)))
274   {
275     DALI_LOG_WARNING("error during png_read_image\n");
276     return false;
277   }
278
279   unsigned int rowBytes = png_get_rowbytes(png, info);
280
281   unsigned int bufferWidth   = GetTextureDimension(width);
282   unsigned int bufferHeight  = GetTextureDimension(height);
283   unsigned int stride        = bufferWidth*bpp;
284
285   // not sure if this ever happens
286   if( rowBytes > stride )
287   {
288     stride = GetTextureDimension(rowBytes);
289
290     bpp = stride / bufferWidth;
291     switch(bpp)
292     {
293       case 3:
294         pixelFormat = Pixel::RGB888;
295         break;
296       case 4:
297         pixelFormat = Pixel::RGBA8888;
298         break;
299       default:
300         break;
301     }
302
303   }
304
305   // decode the whole image into bitmap buffer
306   auto pixels = (bitmap = Dali::Devel::PixelBuffer::New(bufferWidth, bufferHeight, pixelFormat)).GetBuffer();
307
308   DALI_ASSERT_DEBUG(pixels);
309   rows = reinterpret_cast< png_bytep* >( malloc(sizeof(png_bytep) * height) );
310   for(y=0; y<height; y++)
311   {
312     rows[y] = pixels + y * stride;
313   }
314
315   // decode image
316   png_read_image(png, rows);
317
318   free(rows);
319
320   return true;
321 }
322
323 // simple class to enforce clean-up of PNG structures
324 struct AutoPngWrite
325 {
326   AutoPngWrite(png_structp& _png, png_infop& _info)
327   : png(_png),
328     info(_info)
329   {
330   }
331
332   ~AutoPngWrite()
333   {
334     if(NULL != png)
335     {
336       png_destroy_write_struct(&png, &info);
337     }
338   }
339
340   png_structp& png;
341   png_infop& info;
342 }; // struct AutoPngWrite;
343
344 namespace
345 {
346   // Custom libpng write callbacks that buffer to a vector instead of a file:
347
348   /**
349    * extern "C" linkage is used because this is a callback that we pass to a C
350    * library which is part of the underlying platform and so potentially compiled
351    * as C rather than C++.
352    * @see http://stackoverflow.com/a/2594222
353    * */
354   extern "C" void WriteData(png_structp png_ptr, png_bytep data, png_size_t length)
355   {
356     DALI_ASSERT_DEBUG(png_ptr && data);
357     if(!png_ptr || !data)
358     {
359       return;
360     }
361     // Make sure we don't try to propagate a C++ exception up the call stack of a pure C library:
362     try
363     {
364       // Recover our buffer for writing into:
365       Vector<unsigned char>* const encoded_img = static_cast< Vector<unsigned char>* >( png_get_io_ptr(png_ptr) );
366       if(encoded_img)
367       {
368         const Vector<unsigned char>::SizeType bufferSize = encoded_img->Count();
369         encoded_img->Resize( bufferSize + length ); //< Can throw OOM.
370         unsigned char* const bufferBack = encoded_img->Begin() + bufferSize;
371         memcpy(bufferBack, data, length);
372       }
373       else
374       {
375         DALI_LOG_ERROR("PNG buffer for write to memory was passed from libpng as null.\n");
376       }
377     }
378     catch(...)
379     {
380       DALI_LOG_ERROR("C++ Exception caught\n");
381     }
382   }
383
384   /** Override the flush with a NOP to prevent libpng trying cstdlib file io. */
385   extern "C" void FlushData(png_structp png_ptr)
386   {
387 #ifdef DEBUG_ENABLED
388     Debug::LogMessage(Debug::DebugInfo, "PNG Flush");
389 #endif // DEBUG_ENABLED
390   }
391 }
392
393 /**
394  * Potential improvements:
395  * 1. Detect <= 256 colours and write in palette mode.
396  * 2. Detect grayscale (will early-out quickly for colour images).
397  * 3. Store colour space / gamma correction info related to the device screen?
398  *    http://www.libpng.org/pub/png/book/chapter10.html
399  * 4. Refactor with callers to write straight through to disk and save keeping a big buffer around.
400  * 5. Prealloc buffer (reserve) to input size / <A number greater than 2 (expexcted few realloc but without using lots of memory) | 1 (expected zero reallocs but using a lot of memory)>.
401  * 6. Set the modification time with png_set_tIME(png_ptr, info_ptr, mod_time);
402  * 7. If caller asks for no compression, bypass libpng and blat raw data to
403  *    disk, topped and tailed with header/tail blocks.
404  */
405 bool EncodeToPng( const unsigned char* const pixelBuffer, Vector<unsigned char>& encodedPixels, std::size_t width, std::size_t height, Pixel::Format pixelFormat )
406 {
407   // Translate pixel format enum:
408   int pngPixelFormat = -1;
409   unsigned pixelBytes = 0;
410   bool rgbaOrder = true;
411
412   // Account for RGB versus BGR and presence of alpha in input pixels:
413   switch( pixelFormat )
414   {
415     case Pixel::RGB888:
416     {
417       pngPixelFormat = PNG_COLOR_TYPE_RGB;
418       pixelBytes = 3;
419       break;
420     }
421     case Pixel::BGRA8888:
422     {
423       rgbaOrder = false;
424       ///! No break: fall through:
425     }
426     case Pixel::RGBA8888:
427     {
428       pngPixelFormat = PNG_COLOR_TYPE_RGB_ALPHA;
429       pixelBytes = 4;
430       break;
431     }
432     default:
433     {
434       DALI_LOG_ERROR( "Unsupported pixel format for encoding to PNG.\n" );
435       return false;
436     }
437   }
438
439   const int interlace = PNG_INTERLACE_NONE;
440
441   png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
442   if(!png_ptr)
443   {
444     return false;
445   }
446   /* Allocate/initialize the image information data.  REQUIRED */
447   png_infop info_ptr = png_create_info_struct( png_ptr );
448   if(!info_ptr)
449   {
450     png_destroy_write_struct(&png_ptr, NULL);
451     return false;
452   }
453
454   /* Set error handling.  REQUIRED if you aren't supplying your own
455    * error handling functions in the png_create_write_struct() call.
456    */
457   if(setjmp(png_jmpbuf(png_ptr)))
458   {
459     png_destroy_write_struct(&png_ptr, &info_ptr);
460     return false;
461   }
462
463   // Since we are going to write to memory instead of a file, lets provide
464   // libpng with a custom write function and ask it to pass back our
465   // Vector buffer each time it calls back to flush data to "file":
466   png_set_write_fn(png_ptr, &encodedPixels, WriteData, FlushData);
467
468   // png_set_compression_level( png_ptr, Z_BEST_COMPRESSION);
469   png_set_compression_level(png_ptr, Z_BEST_SPEED);
470   // png_set_compression_level( png_ptr, Z_NO_COMPRESSION); //! We could just generate png directly without libpng in this case.
471
472   // Explicitly limit the number of filters used per scanline to speed us up:
473   // png_set_filter(png_ptr, 0, PNG_FILTER_NONE); ///!ToDo: Try this once baseline profile is in place.
474        // PNG_FILTER_SUB   |
475        // PNG_FILTER_UP    |
476        // PNG_FILTER_AVE   |
477        // PNG_FILTER_PAETH |
478        // PNG_ALL_FILTERS);
479   // Play with Zlib parameters in optimisation phase:
480     // png_set_compression_mem_level(png_ptr, 8);
481     // png_set_compression_strategy(png_ptr,
482         // Z_DEFAULT_STRATEGY);
483     // png_set_compression_window_bits(png_ptr, 15);
484     // png_set_compression_method(png_ptr, 8);
485     // png_set_compression_buffer_size(png_ptr, 8192)
486
487   // Let lib_png know if the pixel bytes are in BGR(A) order:
488   if(!rgbaOrder)
489   {
490     png_set_bgr( png_ptr );
491   }
492
493   // Set the image information:
494   png_set_IHDR(png_ptr, info_ptr, width, height, 8,
495      pngPixelFormat, interlace,
496      PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
497
498   // Start to output the PNG data to our buffer:
499   png_write_info(png_ptr, info_ptr);
500
501   // Walk the rows:
502   const unsigned row_step = width * pixelBytes;
503   png_bytep row_ptr = const_cast<png_bytep>(pixelBuffer);
504   const png_bytep row_end = row_ptr + height * row_step;
505   for(; row_ptr < row_end; row_ptr += row_step)
506   {
507     png_write_row(png_ptr, row_ptr);
508   }
509
510   /* It is REQUIRED to call this to finish writing the rest of the file */
511   png_write_end(png_ptr, info_ptr);
512   /* Clean up after the write, and free any memory allocated */
513   png_destroy_write_struct(&png_ptr, &info_ptr);
514   return true;
515 }
516
517 } // namespace TizenPlatform
518
519 } // namespace Dali