Subversion Repositories pentevo

Rev

Rev 796 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include "std.h"
  2.  
  3. #include "emul.h"
  4. #include "vars.h"
  5. #include "sshot_png.h"
  6.  
  7. typedef void (PNGAPI *png_write_end_ptr) PNGARG((png_structp png_ptr,
  8.    png_infop info_ptr));
  9. typedef void (PNGAPI *png_write_image_ptr) PNGARG((png_structp png_ptr,
  10.    png_bytepp image));
  11. typedef void (PNGAPI *png_set_bgr_ptr) PNGARG((png_structp png_ptr));
  12. typedef void (PNGAPI *png_write_info_ptr) PNGARG((png_structp png_ptr,
  13.    png_infop info_ptr));
  14. typedef void (PNGAPI *png_set_IHDR_ptr) PNGARG((png_structp png_ptr,
  15.    png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
  16.    int color_type, int interlace_method, int compression_method,
  17.    int filter_method));
  18. typedef void (PNGAPI *png_set_write_fn_ptr) PNGARG((png_structp png_ptr,
  19.    png_voidp io_ptr, png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
  20. typedef void (PNGAPI *png_destroy_write_struct_ptr)
  21.    PNGARG((png_structpp png_ptr_ptr, png_infopp info_ptr_ptr));
  22. typedef png_infop (PNGAPI *png_create_info_struct_ptr)
  23.    PNGARG((png_structp png_ptr));
  24. typedef void (PNGAPI *png_set_compression_level_ptr) PNGARG((png_structp png_ptr,
  25.    int level));
  26. typedef png_structp (PNGAPI *png_create_write_struct_ptr)
  27.    PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
  28.    png_error_ptr error_fn, png_error_ptr warn_fn));
  29. typedef void (PNGAPI *png_set_filter_ptr) PNGARG((png_structp png_ptr, int method,
  30.    int filters));
  31.  
  32. static png_error_ptr png_error_p = nullptr;
  33. static png_write_end_ptr png_write_end_p = nullptr;
  34. static png_write_image_ptr png_write_image_p = nullptr;
  35. static png_set_bgr_ptr png_set_bgr_p = nullptr;
  36. static png_write_info_ptr png_write_info_p = nullptr;
  37. static png_set_IHDR_ptr png_set_IHDR_p = nullptr;
  38. static png_set_write_fn_ptr png_set_write_fn_p = nullptr;
  39. static png_destroy_write_struct_ptr png_destroy_write_struct_p = nullptr;
  40. static png_create_info_struct_ptr png_create_info_struct_p = nullptr;
  41. static png_set_compression_level_ptr png_set_compression_level_p = nullptr;
  42. static png_create_write_struct_ptr png_create_write_struct_p = nullptr;
  43. static png_set_filter_ptr png_set_filter_p = nullptr;
  44.  
  45. static HMODULE PngDll = nullptr;
  46. bool PngInit()
  47. {
  48.     PngDll = LoadLibrary("libpng12.dll");
  49.     if(!PngDll)
  50.         return false;
  51.     png_error_p = (png_error_ptr)GetProcAddress(PngDll, "png_error");
  52.     if(!png_error_p)
  53.         return false;
  54.     png_write_end_p = (png_write_end_ptr)GetProcAddress(PngDll, "png_write_end");
  55.     if(!png_write_end_p)
  56.         return false;
  57.     png_write_image_p = (png_write_image_ptr)GetProcAddress(PngDll, "png_write_image");
  58.     if(!png_write_image_p)
  59.         return false;
  60.     png_set_bgr_p = (png_set_bgr_ptr)GetProcAddress(PngDll, "png_set_bgr");
  61.     if(!png_set_bgr_p)
  62.         return false;
  63.     png_write_info_p = (png_write_info_ptr)GetProcAddress(PngDll, "png_write_info");
  64.     if(!png_write_info_p)
  65.         return false;
  66.     png_set_IHDR_p = (png_set_IHDR_ptr)GetProcAddress(PngDll, "png_set_IHDR");
  67.     if(!png_set_IHDR_p)
  68.         return false;
  69.     png_set_write_fn_p = (png_set_write_fn_ptr)GetProcAddress(PngDll, "png_set_write_fn");
  70.     if(!png_set_write_fn_p)
  71.         return false;
  72.     png_destroy_write_struct_p = (png_destroy_write_struct_ptr)GetProcAddress(PngDll, "png_destroy_write_struct");
  73.     if(!png_destroy_write_struct_p)
  74.         return false;
  75.     png_create_info_struct_p = (png_create_info_struct_ptr)GetProcAddress(PngDll, "png_create_info_struct");
  76.     if(!png_create_info_struct_p)
  77.         return false;
  78.     png_set_compression_level_p = (png_set_compression_level_ptr)GetProcAddress(PngDll, "png_set_compression_level");
  79.     if(!png_set_compression_level_p)
  80.         return false;
  81.     png_create_write_struct_p = (png_create_write_struct_ptr)GetProcAddress(PngDll, "png_create_write_struct");
  82.     if(!png_create_write_struct_p)
  83.         return false;
  84.     png_set_filter_p = (png_set_filter_ptr)GetProcAddress(PngDll, "png_set_filter");
  85.     if(!png_set_filter_p)
  86.         return false;
  87.     return true;
  88. }
  89.  
  90. void PngDone()
  91. {
  92.     if(PngDll)
  93.         FreeLibrary(PngDll);
  94. }
  95.  
  96. static png_structp png_ptr = nullptr;
  97. static png_infop info_ptr = nullptr;
  98.  
  99. static void
  100. PNGAPI
  101. png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  102. {
  103.    size_t check;
  104.  
  105.    check = fwrite(data, 1, length, (FILE *)(png_ptr->io_ptr));
  106.    if (check != length)
  107.    {
  108.       png_error_p(png_ptr, "Write Error");
  109.    }
  110. }
  111.  
  112. static void
  113. PNGAPI
  114. png_flush(png_structp png_ptr)
  115. {
  116.    FILE *io_ptr;
  117.    io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
  118.    if (io_ptr != nullptr)
  119.       fflush(io_ptr);
  120. }
  121.  
  122. BOOL PngSaveImage (FILE *pfFile, png_byte *pDiData,
  123.                    int iWidth, int iHeight, png_color bkgColor)
  124. {
  125.     (void)bkgColor;
  126.  
  127.     const int           ciBitDepth = 8;
  128.     const int           ciChannels = 3;
  129.  
  130.     png_uint_32         ulRowBytes;
  131.     static png_byte   **ppbRowPointers = nullptr;
  132.     int                 i;
  133.  
  134.     // prepare the standard PNG structures
  135.     png_ptr = png_create_write_struct_p(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
  136.     if (!png_ptr)
  137.     {
  138.         return FALSE;
  139.     }
  140.  
  141.     png_set_filter_p(png_ptr, 0, PNG_NO_FILTERS);
  142.  
  143.     png_set_compression_level_p(png_ptr, 4/*Z_BEST_COMPRESSION*/);
  144.  
  145.     info_ptr = png_create_info_struct_p(png_ptr);
  146.     if (!info_ptr)
  147.     {
  148.         png_destroy_write_struct_p(&png_ptr, (png_infopp) nullptr);
  149.         return FALSE;
  150.     }
  151.  
  152.     // initialize the png structure
  153.     png_set_write_fn_p(png_ptr, (png_voidp)pfFile, png_write_data, png_flush);
  154.    
  155.     // we're going to write a very simple 3x8 bit RGB image
  156.     png_set_IHDR_p(png_ptr, info_ptr, png_uint_32(iWidth), png_uint_32(iHeight), ciBitDepth,
  157.         PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
  158.         PNG_FILTER_TYPE_BASE);
  159.    
  160.     // write the file header information
  161.     png_write_info_p(png_ptr, info_ptr);
  162.    
  163.     // swap the BGR pixels in the DiData structure to RGB
  164.     png_set_bgr_p(png_ptr);
  165.    
  166.     // row_bytes is the width x number of channels
  167.     ulRowBytes = png_uint_32(iWidth * ciChannels);
  168.    
  169.     // we can allocate memory for an array of row-pointers
  170.     ppbRowPointers = (png_bytepp) malloc(unsigned(iHeight) * sizeof(png_bytep));
  171.    
  172.     // set the individual row-pointers to point at the correct offsets
  173.     for (i = 0; i < iHeight; i++)
  174.         ppbRowPointers[i] = pDiData + unsigned(i) * (((ulRowBytes + 3) >> 2) << 2);
  175.  
  176.     // write out the entire image data in one call
  177.     png_write_image_p(png_ptr, ppbRowPointers);
  178.    
  179.     // write the additional chunks to the PNG file (not really needed)
  180.     png_write_end_p(png_ptr, info_ptr);
  181.    
  182.     // and we're done
  183.     free (ppbRowPointers);
  184.     ppbRowPointers = nullptr;
  185.    
  186.     // clean up after the write, and free any memory allocated
  187.     png_destroy_write_struct_p(&png_ptr, (png_infopp) nullptr);
  188.    
  189.     return TRUE;
  190. }
  191.