<commit_before>/*****************************************************************************
 * 
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

//$Id$
#include <mapnik/global.hpp>
#include <mapnik/octree.hpp>
extern "C"
{
#include <png.h>
}

namespace mapnik {
   
   template <typename T>
   void write_data (png_structp png_ptr, png_bytep data, png_size_t length)
   {
      T * out = static_cast<T*>(png_get_io_ptr(png_ptr));
      out->write(reinterpret_cast<char*>(data), length);
   }

   template <typename T>
   void flush_data (png_structp png_ptr)
   {
      T * out = static_cast<T*>(png_get_io_ptr(png_ptr));
      out->flush();
   }
   
   template <typename T1, typename T2>
   void save_as_png(T1 & file , T2 const& image)
   {        
      png_voidp error_ptr=0;
      png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
                                                  error_ptr,0, 0);
       
      if (!png_ptr) return;
      
      // switch on optimization only if supported
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) && defined(PNG_MMX_CODE_SUPPORTED)
      png_uint_32 mask, flags;
      flags = png_get_asm_flags(png_ptr);
      mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
      png_set_asm_flags(png_ptr, flags | mask);
#endif
      png_set_filter (png_ptr, 0, PNG_FILTER_NONE);
      png_infop info_ptr = png_create_info_struct(png_ptr);
      if (!info_ptr)
      {
         png_destroy_write_struct(&png_ptr,(png_infopp)0);
         return;
      }
      if (setjmp(png_jmpbuf(png_ptr)))
      {
         png_destroy_write_struct(&png_ptr, &info_ptr);
         return;
      }
      png_set_write_fn (png_ptr, &file, &write_data<T1>, &flush_data<T1>);
        
      //png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
      //png_set_compression_strategy(png_ptr, Z_FILTERED);
      png_set_IHDR(png_ptr, info_ptr,image.width(),image.height(),8,
                   PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE,
                   PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
      png_write_info(png_ptr, info_ptr);
      for (unsigned i=0;i<image.height();i++)
      {
         png_write_row(png_ptr,(png_bytep)image.getRow(i));
      }

      png_write_end(png_ptr, info_ptr);
      png_destroy_write_struct(&png_ptr, &info_ptr);
   }

   template <typename T>
   void reduce_8  (T const& in, ImageData8 & out, octree<rgb> & tree)
   {
      unsigned width = in.width();
      unsigned height = in.height();
      for (unsigned y = 0; y < height; ++y)
      {
         mapnik::ImageData32::pixel_type const * row = in.getRow(y);
         mapnik::ImageData8::pixel_type  * row_out = out.getRow(y);
         for (unsigned x = 0; x < width; ++x)
         {
            unsigned val = row[x];
            mapnik::rgb c((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff);
            byte index = tree.quantize(c);
            if (!((val>>24)&0x80)) index = 0;//alfa
            row_out[x] = index;
         }
      }
   }

   template <typename T>
   void reduce_4 (T const& in, ImageData8 & out, octree<rgb> & tree)
   {
      unsigned width = in.width();
      unsigned height = in.height();

      for (unsigned y = 0; y < height; ++y)
      {
         mapnik::ImageData32::pixel_type const * row = in.getRow(y);
         mapnik::ImageData8::pixel_type  * row_out = out.getRow(y);

         for (unsigned x = 0; x < width; ++x)
         {
            unsigned val = row[x];
            mapnik::rgb c((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff);
            byte index = tree.quantize(c);
            if (x%2 >  0) index = index<<4;
            if ((val>>24)^0x80) index = 0;//alfa
            row_out[x>>1] |= index;
         }
      }
   }<commit_msg>Fix reduce_4() to make it consistent with reduce_8(). This was breaking tiles which were compressing down to a 16 entry colormap. All the pixels were being set to the alpha index.<commit_after>/*****************************************************************************
 * 
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2006 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

//$Id$
#include <mapnik/global.hpp>
#include <mapnik/octree.hpp>
extern "C"
{
#include <png.h>
}

namespace mapnik {
   
   template <typename T>
   void write_data (png_structp png_ptr, png_bytep data, png_size_t length)
   {
      T * out = static_cast<T*>(png_get_io_ptr(png_ptr));
      out->write(reinterpret_cast<char*>(data), length);
   }

   template <typename T>
   void flush_data (png_structp png_ptr)
   {
      T * out = static_cast<T*>(png_get_io_ptr(png_ptr));
      out->flush();
   }
   
   template <typename T1, typename T2>
   void save_as_png(T1 & file , T2 const& image)
   {        
      png_voidp error_ptr=0;
      png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
                                                  error_ptr,0, 0);
       
      if (!png_ptr) return;
      
      // switch on optimization only if supported
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) && defined(PNG_MMX_CODE_SUPPORTED)
      png_uint_32 mask, flags;
      flags = png_get_asm_flags(png_ptr);
      mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
      png_set_asm_flags(png_ptr, flags | mask);
#endif
      png_set_filter (png_ptr, 0, PNG_FILTER_NONE);
      png_infop info_ptr = png_create_info_struct(png_ptr);
      if (!info_ptr)
      {
         png_destroy_write_struct(&png_ptr,(png_infopp)0);
         return;
      }
      if (setjmp(png_jmpbuf(png_ptr)))
      {
         png_destroy_write_struct(&png_ptr, &info_ptr);
         return;
      }
      png_set_write_fn (png_ptr, &file, &write_data<T1>, &flush_data<T1>);
        
      //png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
      //png_set_compression_strategy(png_ptr, Z_FILTERED);
      png_set_IHDR(png_ptr, info_ptr,image.width(),image.height(),8,
                   PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE,
                   PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
      png_write_info(png_ptr, info_ptr);
      for (unsigned i=0;i<image.height();i++)
      {
         png_write_row(png_ptr,(png_bytep)image.getRow(i));
      }

      png_write_end(png_ptr, info_ptr);
      png_destroy_write_struct(&png_ptr, &info_ptr);
   }

   template <typename T>
   void reduce_8  (T const& in, ImageData8 & out, octree<rgb> & tree)
   {
      unsigned width = in.width();
      unsigned height = in.height();
      for (unsigned y = 0; y < height; ++y)
      {
         mapnik::ImageData32::pixel_type const * row = in.getRow(y);
         mapnik::ImageData8::pixel_type  * row_out = out.getRow(y);
         for (unsigned x = 0; x < width; ++x)
         {
            unsigned val = row[x];
            mapnik::rgb c((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff);
            byte index = tree.quantize(c);
            if (!((val>>24)&0x80)) index = 0;//alfa
            row_out[x] = index;
         }
      }
   }

   template <typename T>
   void reduce_4 (T const& in, ImageData8 & out, octree<rgb> & tree)
   {
      unsigned width = in.width();
      unsigned height = in.height();

      for (unsigned y = 0; y < height; ++y)
      {
         mapnik::ImageData32::pixel_type const * row = in.getRow(y);
         mapnik::ImageData8::pixel_type  * row_out = out.getRow(y);

         for (unsigned x = 0; x < width; ++x)
         {
            unsigned val = row[x];
            mapnik::rgb c((val)&0xff, (val>>8)&0xff, (val>>16) & 0xff);
            byte index = tree.quantize(c);
            if (x%2 >  0) index = index<<4;
            if (!((val>>24)&0x80)) index = 0;//alfa
            row_out[x>>1] |= index;
         }
      }
   }
{'fraction_non_alphanumeric': 0.14558243632100606, 'fraction_numerical': 0.017265266972183738, 'mean_word_length': 2.7416267942583734, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7556453', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3046, 'n_words': 764}