VirtualBox

Ignore:
Timestamp:
Feb 12, 2024 3:57:56 PM (12 months ago)
Author:
vboxsync
Message:

libpng-1.6.42: Applied and adjusted our libpng changes to 1.6.42. bugref:8515

Location:
trunk/src/libs/libpng-1.6.42/contrib
Files:
8 deleted
35 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/libpng-1.6.42/contrib/arm-neon/android-ndk.c

    r96425 r103316  
    33 * Copyright (c) 2014 Glenn Randers-Pehrson
    44 * Written by John Bowler, 2014.
    5  * Last changed in libpng 1.6.10 [March 6, 2014]
    65 *
    76 * This code is released under the libpng license.
     
    2524 * only the header files exist in the NDK.
    2625 */
     26
    2727#include <cpu-features.h>
    2828
  • trunk/src/libs/libpng-1.6.42/contrib/arm-neon/linux-auxv.c

    r96425 r103316  
    33 * Copyright (c) 2014 Glenn Randers-Pehrson
    44 * Written by Mans Rullgard, 2011.
    5  * Last changed in libpng 1.6.10 [March 6, 2014]
    65 *
    76 * This code is released under the libpng license.
     
    2322 * looking at each element for one that records NEON capabilities.
    2423 */
     24
    2525#include <unistd.h> /* for POSIX 1003.1 */
    2626#include <errno.h>  /* for EINTR */
  • trunk/src/libs/libpng-1.6.42/contrib/arm-neon/linux.c

    r96425 r103316  
    11/* contrib/arm-neon/linux.c
    22 *
    3  * Last changed in libpng 1.6.31 [July 27, 2017]
    43 * Copyright (c) 2014, 2017 Glenn Randers-Pehrson
    54 * Written by John Bowler, 2014, 2017.
     
    2019 * however use <stdio.h> and it assumes that /proc/cpuinfo is never localized.
    2120 */
     21
    2222#include <stdio.h>
    2323
  • trunk/src/libs/libpng-1.6.42/contrib/examples/pngtopng.c

    r96425 r103316  
    55 * related or neighboring rights to this work.  This work is published from:
    66 * United States.
    7  *
    8  * Last changed in libpng 1.6.29 [March 16, 2017]
    97 *
    108 * Read a PNG and write it out in a fixed format, using the 'simplified API'
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/gentests.sh

    r96425 r103316  
    22#
    33# Copyright (c) 2013 John Cunningham Bowler
    4 #
    5 # Last changed in libpng 1.6.0 [February 14, 2013]
    64#
    75# This code is released under the libpng license.
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/makepng.c

    r96425 r103316  
    44#define COPYRIGHT "\251 2013,2015 John Cunningham Bowler"
    55/*
    6  * Last changed in libpng 1.6.20 [November 24, 2015]
    7  *
    86 * This code is released under the libpng license.
    97 * For conditions of distribution and use, see the disclaimer
     
    300298            volatile unsigned int ip = 0;
    301299
    302             for (x=0; x<size; ++x) for (y=0; y<size; ++y)
     300            for (x=0; x<size; ++x)
    303301            {
    304                ip = x + (size * y);
    305 
    306                /* size is at most 16, so the scaled value below fits in 16 bits
    307                 */
    308 #              define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
    309 #              define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
    310                   (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
    311                   (size*size))
    312 
    313                set_color(palette+ip, trans+ip,
    314                   /* color:    green, red,blue,white */
    315                   xyinterp(x, y,   0, 255,   0, 255),
    316                   xyinterp(x, y, 255,   0,   0, 255),
    317                   xyinterp(x, y,   0,   0, 255, 255),
    318                   /* alpha:        0, 102, 204, 255) */
    319                   xyinterp(x, y,   0, 102, 204, 255),
    320                   gamma_table);
     302               for (y=0; y<size; ++y)
     303               {
     304                  ip = x + (size * y);
     305
     306                  /* size is at most 16, so the scaled value below fits in 16 bits
     307                  */
     308#                 define interp(pos, c1, c2) ((pos * c1) + ((size-pos) * c2))
     309#                 define xyinterp(x, y, c1, c2, c3, c4) (((size * size / 2) +\
     310                     (interp(x, c1, c2) * y + (size-y) * interp(x, c3, c4))) /\
     311                     (size*size))
     312
     313                  set_color(palette+ip, trans+ip,
     314                     /* color:    green, red,blue,white */
     315                     xyinterp(x, y,   0, 255,   0, 255),
     316                     xyinterp(x, y, 255,   0,   0, 255),
     317                     xyinterp(x, y,   0,   0, 255, 255),
     318                     /* alpha:        0, 102, 204, 255) */
     319                     xyinterp(x, y,   0, 102, 204, 255),
     320                     gamma_table);
     321               }
    321322            }
    322323
     
    397398   png_uint_32 depth_max = (1U << bit_depth)-1; /* up to 65536 */
    398399
    399    if (colors[0] == 0) if (small)
     400   if (colors[0] == 0 && small)
    400401   {
    401402      unsigned int pixel_depth = pixel_depth_of_type(color_type, bit_depth);
     
    496497         case 48:
    497498         case 64:
    498             /* The rows are filled by an alogorithm similar to the above, in the
     499            /* The rows are filled by an algorithm similar to the above, in the
    499500             * first row pixel bytes are all equal, increasing from 0 by 1 for
    500501             * each pixel.  In the second row the bytes within a pixel are
     
    859860         unsigned int i;
    860861
    861          if (real_gamma == 45455) for (i=0; i<256; ++i)
    862          {
    863             gamma_table[i] = (png_byte)i;
    864             conv = 1.;
     862         if (real_gamma == 45455)
     863         {
     864            for (i=0; i<256; ++i)
     865            {
     866               gamma_table[i] = (png_byte)i;
     867               conv = 1.;
     868            }
    865869         }
    866870
     
    14311435   {
    14321436      list[i] = param;
    1433       while (*++param) if (*param == '\n' || *param == ':')
    1434       {
    1435          *param++ = 0; /* Terminate last parameter */
    1436          break;        /* And start a new one. */
     1437      while (*++param)
     1438      {
     1439         if (*param == '\n' || *param == ':')
     1440         {
     1441            *param++ = 0; /* Terminate last parameter */
     1442            break;        /* And start a new one. */
     1443         }
    14371444      }
    14381445   }
     
    18701877   }
    18711878
    1872    /* small and colors are incomparible (will probably crash if both are used at
     1879   /* small and colors are incompatible (will probably crash if both are used at
    18731880    * the same time!)
    18741881    */
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/pngimage.c

    r96425 r103316  
     1
    12/* pngimage.c
    23 *
     4 * Copyright (c) 2021 Cosmin Truta
    35 * Copyright (c) 2015,2016 John Cunningham Bowler
    4  *
    5  * Last changed in libpng 1.6.24 [August 4, 2016]
    66 *
    77 * This code is released under the libpng license.
     
    1313 * transforms.
    1414 */
     15
    1516#include <stdarg.h>
    1617#include <stdlib.h>
     
    318319   t &= -t; /* first set bit */
    319320
    320    for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
    321    {
    322       if ((transform_info[i].transform & t) != 0)
    323          return transform_info[i].name;
    324    }
     321   for (i=0; i<TTABLE_SIZE; ++i)
     322      if (transform_info[i].name != NULL)
     323         if ((transform_info[i].transform & t) != 0)
     324            return transform_info[i].name;
    325325
    326326   return "invalid transform";
     
    339339   unsigned int i;
    340340
    341    for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
    342    {
    343       if (transform_info[i].when & TRANSFORM_R)
    344          read_transforms |= transform_info[i].transform;
    345 
    346       if (transform_info[i].when & TRANSFORM_W)
    347          write_transforms |= transform_info[i].transform;
     341   for (i=0; i<TTABLE_SIZE; ++i)
     342   {
     343      if (transform_info[i].name != NULL)
     344      {
     345         if (transform_info[i].when & TRANSFORM_R)
     346            read_transforms |= transform_info[i].transform;
     347
     348         if (transform_info[i].when & TRANSFORM_W)
     349            write_transforms |= transform_info[i].transform;
     350      }
    348351   }
    349352
     
    963966      unsigned int i;
    964967
    965       for (i=0; i<TTABLE_SIZE; ++i) if (transform_info[i].name != NULL)
     968      for (i=0; i<TTABLE_SIZE; ++i)
    966969      {
    967          int transform = transform_info[i].transform;
    968 
    969          if ((transform_info[i].valid_chunks == 0 ||
    970                (transform_info[i].valid_chunks & chunks) != 0) &&
    971             (transform_info[i].color_mask_required & ct) ==
    972                transform_info[i].color_mask_required &&
    973             (transform_info[i].color_mask_absent & ct) == 0 &&
    974             (transform_info[i].bit_depths & bd) != 0 &&
    975             (transform_info[i].when & TRANSFORM_R) != 0)
    976             active |= transform;
    977 
    978          else if ((transform_info[i].when & TRANSFORM_R) != 0)
    979             inactive |= transform;
     970         if (transform_info[i].name != NULL)
     971         {
     972            int transform = transform_info[i].transform;
     973
     974            if ((transform_info[i].valid_chunks == 0 ||
     975                  (transform_info[i].valid_chunks & chunks) != 0) &&
     976               (transform_info[i].color_mask_required & ct) ==
     977                  transform_info[i].color_mask_required &&
     978               (transform_info[i].color_mask_absent & ct) == 0 &&
     979               (transform_info[i].bit_depths & bd) != 0 &&
     980               (transform_info[i].when & TRANSFORM_R) != 0)
     981               active |= transform;
     982
     983            else if ((transform_info[i].when & TRANSFORM_R) != 0)
     984               inactive |= transform;
     985         }
    980986      }
    981987
     
    10011007   const char *e = NULL;
    10021008
    1003    png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
    1004       &color_type, &interlace_method, &compression_method, &filter_method);
     1009   if (!png_get_IHDR(dp->read_pp, dp->read_ip, &width, &height, &bit_depth,
     1010      &color_type, &interlace_method, &compression_method, &filter_method))
     1011      display_log(dp, LIBPNG_BUG, "png_get_IHDR failed");
    10051012
    10061013#  define C(item) if (item != dp->item) \
     
    10821089
    10831090               /* Find the first error */
    1084                for (x=0; x<rowbytes-1; ++x) if (row[x] != orig[x])
    1085                   break;
     1091               for (x=0; x<rowbytes-1; ++x)
     1092                  if (row[x] != orig[x])
     1093                     break;
    10861094
    10871095               display_log(dp, APP_FAIL,
     
    11381146                  color_type);
    11391147               /*NOTREACHED*/
     1148               memset(sig_bits, 0, sizeof(sig_bits));
    11401149               bpp = 0;
    11411150               break;
     
    11991208               break;
    12001209
    1201             case 4: /* Relicate twice */
     1210            case 4: /* Replicate twice */
    12021211               /* Value is 1, 2, 3 or 4 */
    12031212               b = 0xf & ((0xf << 4) >> sig_bits[0]);
     
    16871696               printf("%s: pngimage ", pass ? "PASS" : "FAIL");
    16881697
    1689                for (j=1; j<option_end; ++j) if (j != ilog)
    1690                   printf("%s ", argv[j]);
     1698               for (j=1; j<option_end; ++j)
     1699                  if (j != ilog)
     1700                     printf("%s ", argv[j]);
    16911701
    16921702               printf("%s\n", d.filename);
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/pngstest.c

    r96425 r103316  
    1 /*-
    2  * pngstest.c
     1
     2/* pngstest.c
    33 *
    4  * Last changed in libpng 1.6.31 [July 27, 2017]
     4 * Copyright (c) 2021 Cosmin Truta
    55 * Copyright (c) 2013-2017 John Cunningham Bowler
    66 *
     
    1111 * Test for the PNG 'simplified' APIs.
    1212 */
     13
    1314#define _ISOC90_SOURCE 1
    14 #define MALLOC_CHECK_ 2/*glibc facility: turn on debugging*/
     15#define MALLOC_CHECK_ 2 /*glibc facility: turn on debugging*/
    1516
    1617#include <stddef.h>
     
    11511152 * 2) Remove color by mapping to grayscale.  (Grayscale to color is a no-op.)
    11521153 *
    1153  * 3) Convert between 8-bit and 16-bit components.  (Both directtions are
     1154 * 3) Convert between 8-bit and 16-bit components.  (Both directions are
    11541155 *    relevant.)
    11551156 *
     
    27022703               if ((a->opts & ACCUMULATE) == 0)
    27032704               {
    2704                   char pindex[9];
     2705                  char pindex[16];
    27052706                  sprintf(pindex, "%lu[%lu]", (unsigned long)y,
    27062707                     (unsigned long)a->image.colormap_entries);
     
    27132714            {
    27142715               if ((b->opts & ACCUMULATE) == 0)
    2715                   {
    2716                   char pindex[9];
     2716               {
     2717                  char pindex[16];
    27172718                  sprintf(pindex, "%lu[%lu]", (unsigned long)y,
    27182719                     (unsigned long)b->image.colormap_entries);
    27192720                  logerror(b, b->file_name, ": bad pixel index: ", pindex);
    2720                   }
     2721               }
    27212722               result = 0;
    27222723            }
     
    28212822
    28222823         /* Hence the btoa array. */
    2823          for (i=0; i<4; ++i) if (bloc[i] < 4)
    2824             btoa[bloc[i]] = aloc[i]; /* may be '4' for alpha */
     2824         for (i=0; i<4; ++i)
     2825         {
     2826            if (bloc[i] < 4)
     2827               btoa[bloc[i]] = aloc[i]; /* may be '4' for alpha */
     2828         }
    28252829
    28262830         if (alpha_added)
     
    32103214   {
    32113215#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
    3212       static int counter = 0;
     3216      static unsigned int counter = 0;
    32133217      char name[32];
    32143218
    3215       sprintf(name, "%s%d.png", tmpf, ++counter);
     3219      sprintf(name, "%s%u.png", tmpf, ++counter);
    32163220
    32173221      if (png_image_write_to_file(&image->image, name, convert_to_8bit,
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/pngunknown.c

    r96425 r103316  
    22/* pngunknown.c - test the read side unknown chunk handling
    33 *
    4  * Last changed in libpng 1.6.32 [August 24, 2017]
     4 * Copyright (c) 2021 Cosmin Truta
    55 * Copyright (c) 2015,2017 Glenn Randers-Pehrson
    66 * Written by John Cunningham Bowler
     
    371371   int i = NINFO;
    372372
    373    while (--i >= 0) if (chunk_info[i].flag == flag) return i;
     373   while (--i >= 0)
     374      if (chunk_info[i].flag == flag)
     375         return i;
    374376
    375377   fprintf(stderr, "pngunknown: internal error\n");
     
    548550      case PNG_HANDLE_CHUNK_AS_DEFAULT:
    549551      case PNG_HANDLE_CHUNK_NEVER:
    550          discard = 1/*handled; discard*/;
     552         discard = 1; /*handled; discard*/
    551553         break;
    552554
    553555      case PNG_HANDLE_CHUNK_IF_SAFE:
    554556      case PNG_HANDLE_CHUNK_ALWAYS:
    555          discard = 0/*not handled; keep*/;
     557         discard = 0; /*not handled; keep*/
    556558         break;
    557559   }
     
    560562    * is set if the chunk is to be kept ('not handled'.)
    561563    */
    562    if (chunk >= 0) if (!discard) /* stupidity to stop a GCC warning */
    563    {
    564       png_uint_32 flag = chunk_info[chunk].flag;
    565 
    566       if (pc->location & PNG_AFTER_IDAT)
    567          d->after_IDAT |= flag;
    568 
    569       else
    570          d->before_IDAT |= flag;
     564   if (chunk >= 0)
     565   {
     566      if (!discard) /* stupidity to stop a GCC warning */
     567      {
     568         png_uint_32 flag = chunk_info[chunk].flag;
     569
     570         if (pc->location & PNG_AFTER_IDAT)
     571            d->after_IDAT |= flag;
     572
     573         else
     574            d->before_IDAT |= flag;
     575      }
    571576   }
    572577
     
    842847               png_uint_32 y;
    843848
    844                for (y=0; y<height; ++y) if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
    845                   png_read_row(d->png_ptr, NULL, NULL);
     849               for (y=0; y<height; ++y)
     850                  if (PNG_ROW_IN_INTERLACE_PASS(y, ipass))
     851                     png_read_row(d->png_ptr, NULL, NULL);
    846852            }
    847853         }
     
    10921098static const char *standard_tests[] =
    10931099{
    1094  "discard", "default=discard", 0,
    1095  "save", "default=save", 0,
    1096  "if-safe", "default=if-safe", 0,
    1097  "vpAg", "vpAg=if-safe", 0,
    1098  "sTER", "sTER=if-safe", 0,
    1099  "IDAT", "default=discard", "IDAT=save", 0,
    1100  "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
    1101    "sBIT=save", "sRGB=save", "eXIf=save", 0,
    1102  0/*end*/
     1100   "discard", "default=discard", NULL,
     1101   "save", "default=save", NULL,
     1102   "if-safe", "default=if-safe", NULL,
     1103   "vpAg", "vpAg=if-safe", NULL,
     1104   "sTER", "sTER=if-safe", NULL,
     1105   "IDAT", "default=discard", "IDAT=save", NULL,
     1106   "sAPI", "bKGD=save", "cHRM=save", "gAMA=save", "all=discard", "iCCP=save",
     1107      "sBIT=save", "sRGB=save", "eXIf=save", NULL,
     1108   NULL /*end*/
    11031109};
    11041110
     
    11161122{
    11171123   FILE *fp;
    1118    png_uint_32 default_flags[4/*valid,unknown{before,after}*/];
     1124   png_uint_32 default_flags[4]; /*valid,unknown{before,after}*/
    11191125   int strict = 0, default_tests = 0;
    11201126   const char *count_argv = "default=save";
     
    11541160    * optimization which causes warnings with -Wstrict-overflow!
    11551161    */
    1156    else if (default_tests) if (argc != 1)
    1157       usage(d.program, "extra arguments");
     1162   else if (default_tests)
     1163      if (argc != 1)
     1164         usage(d.program, "extra arguments");
    11581165
    11591166   /* The name of the test file is the last argument; remove it. */
     
    12171224         int arg_count = 0;
    12181225
    1219          while (*next) ++next, ++arg_count;
     1226         while (*next != NULL)
     1227         {
     1228            ++next;
     1229            ++arg_count;
     1230         }
    12201231
    12211232         perform_one_test_safe(fp, arg_count, test, default_flags, &d,
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/pngvalid.c

    r96425 r103316  
    22/* pngvalid.c - validate libpng by constructing then reading png files.
    33 *
    4  * Last changed in libpng 1.6.31 [July 27, 2017]
     4 * Copyright (c) 2021 Cosmin Truta
    55 * Copyright (c) 2014-2017 John Cunningham Bowler
    66 *
     
    2323#define _GNU_SOURCE 1 /* For the floating point exception extension */
    2424#define _BSD_SOURCE 1 /* For the floating point exception extension */
     25#define _DEFAULT_SOURCE 1 /* For the floating point exception extension */
    2526
    2627#include <signal.h>
     
    58235824           interlace_type < INTERLACE_LAST; ++interlace_type)
    58245825      {
    5825          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5826             interlace_type, 0, 0, 0), do_read_interlace, pm->use_update_info);
     5826         standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5827            0/*palette*/, interlace_type, 0, 0, 0), do_read_interlace,
     5828            pm->use_update_info);
    58275829
    58285830         if (fail(pm))
     
    58775879      png_uint_32 h, w;
    58785880
    5879       for (h=1; h<=16; h+=hinc[bdlo]) for (w=1; w<=16; w+=winc[bdlo])
    5880       {
    5881          /* First test all the 'size' images against the sequential
    5882           * reader using libpng to deinterlace (where required.)  This
    5883           * validates the write side of libpng.  There are four possibilities
    5884           * to validate.
    5885           */
    5886          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5887             PNG_INTERLACE_NONE, w, h, 0), 0/*do_interlace*/,
    5888             pm->use_update_info);
    5889 
    5890          if (fail(pm))
    5891             return 0;
    5892 
    5893          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5894             PNG_INTERLACE_NONE, w, h, 1), 0/*do_interlace*/,
    5895             pm->use_update_info);
    5896 
    5897          if (fail(pm))
    5898             return 0;
    5899 
    5900          /* Now validate the interlaced read side - do_interlace true,
    5901           * in the progressive case this does actually make a difference
    5902           * to the code used in the non-interlaced case too.
    5903           */
    5904          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5905             PNG_INTERLACE_NONE, w, h, 0), 1/*do_interlace*/,
    5906             pm->use_update_info);
    5907 
    5908          if (fail(pm))
    5909             return 0;
    5910 
    5911 #     if CAN_WRITE_INTERLACE
    5912          /* Validate the pngvalid code itself: */
    5913          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5914             PNG_INTERLACE_ADAM7, w, h, 1), 1/*do_interlace*/,
    5915             pm->use_update_info);
    5916 
    5917          if (fail(pm))
    5918             return 0;
    5919 #     endif
     5881      for (h=1; h<=16; h+=hinc[bdlo])
     5882      {
     5883         for (w=1; w<=16; w+=winc[bdlo])
     5884         {
     5885            /* First test all the 'size' images against the sequential
     5886            * reader using libpng to deinterlace (where required.)  This
     5887            * validates the write side of libpng.  There are four possibilities
     5888            * to validate.
     5889            */
     5890            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5891               0/*palette*/, PNG_INTERLACE_NONE, w, h, 0), 0/*do_interlace*/,
     5892               pm->use_update_info);
     5893
     5894            if (fail(pm))
     5895               return 0;
     5896
     5897            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5898               0/*palette*/, PNG_INTERLACE_NONE, w, h, 1), 0/*do_interlace*/,
     5899               pm->use_update_info);
     5900
     5901            if (fail(pm))
     5902               return 0;
     5903
     5904            /* Now validate the interlaced read side - do_interlace true,
     5905            * in the progressive case this does actually make a difference
     5906            * to the code used in the non-interlaced case too.
     5907            */
     5908            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5909               0/*palette*/, PNG_INTERLACE_NONE, w, h, 0), 1/*do_interlace*/,
     5910               pm->use_update_info);
     5911
     5912            if (fail(pm))
     5913               return 0;
     5914
     5915#        if CAN_WRITE_INTERLACE
     5916            /* Validate the pngvalid code itself: */
     5917            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5918               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 1), 1/*do_interlace*/,
     5919               pm->use_update_info);
     5920
     5921            if (fail(pm))
     5922               return 0;
     5923#        endif
     5924         }
    59205925      }
    59215926   }
     
    59285933      png_uint_32 h, w;
    59295934
    5930       for (h=1; h<=16; h+=hinc[bdlo]) for (w=1; w<=16; w+=winc[bdlo])
    5931       {
    5932 #     ifdef PNG_READ_INTERLACING_SUPPORTED
    5933          /* Test with pngvalid generated interlaced images first; we have
    5934           * already verify these are ok (unless pngvalid has self-consistent
    5935           * read/write errors, which is unlikely), so this detects errors in the
    5936           * read side first:
    5937           */
    5938 #     if CAN_WRITE_INTERLACE
    5939          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5940             PNG_INTERLACE_ADAM7, w, h, 1), 0/*do_interlace*/,
    5941             pm->use_update_info);
    5942 
    5943          if (fail(pm))
    5944             return 0;
    5945 #     endif
    5946 #     endif /* READ_INTERLACING */
    5947 
    5948 #     ifdef PNG_WRITE_INTERLACING_SUPPORTED
    5949          /* Test the libpng write side against the pngvalid read side: */
    5950          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5951             PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
    5952             pm->use_update_info);
    5953 
    5954          if (fail(pm))
    5955             return 0;
    5956 #     endif
    5957 
    5958 #     ifdef PNG_READ_INTERLACING_SUPPORTED
    5959 #     ifdef PNG_WRITE_INTERLACING_SUPPORTED
    5960          /* Test both together: */
    5961          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
    5962             PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
    5963             pm->use_update_info);
    5964 
    5965          if (fail(pm))
    5966             return 0;
    5967 #     endif
    5968 #     endif /* READ_INTERLACING */
     5935      for (h=1; h<=16; h+=hinc[bdlo])
     5936      {
     5937         for (w=1; w<=16; w+=winc[bdlo])
     5938         {
     5939#        ifdef PNG_READ_INTERLACING_SUPPORTED
     5940            /* Test with pngvalid generated interlaced images first; we have
     5941            * already verify these are ok (unless pngvalid has self-consistent
     5942            * read/write errors, which is unlikely), so this detects errors in
     5943            * the read side first:
     5944            */
     5945#        if CAN_WRITE_INTERLACE
     5946            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5947               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 1), 0/*do_interlace*/,
     5948               pm->use_update_info);
     5949
     5950            if (fail(pm))
     5951               return 0;
     5952#        endif
     5953#        endif /* READ_INTERLACING */
     5954
     5955#        ifdef PNG_WRITE_INTERLACING_SUPPORTED
     5956            /* Test the libpng write side against the pngvalid read side: */
     5957            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5958               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
     5959               pm->use_update_info);
     5960
     5961            if (fail(pm))
     5962               return 0;
     5963#        endif
     5964
     5965#        ifdef PNG_READ_INTERLACING_SUPPORTED
     5966#        ifdef PNG_WRITE_INTERLACING_SUPPORTED
     5967            /* Test both together: */
     5968            standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo),
     5969               0/*palette*/, PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
     5970               pm->use_update_info);
     5971
     5972            if (fail(pm))
     5973               return 0;
     5974#        endif
     5975#        endif /* READ_INTERLACING */
     5976         }
    59695977      }
    59705978   }
     
    67126720   const char *name, double digitization_error)
    67136721{
    6714    /* Compare the scaled, digitzed, values of our local calculation (in+-err)
     6722   /* Compare the scaled, digitized, values of our local calculation (in+-err)
    67156723    * with the digitized values libpng produced;  'sample_depth' is the actual
    67166724    * digitization depth of the libpng output colors (the bit depth except for
     
    76367644   else
    76377645   {
    7638       /* The default (built in) coeffcients, as above: */
     7646      /* The default (built in) coefficients, as above: */
    76397647#     if PNG_LIBPNG_VER < 10700
    76407648         data.red_coefficient = 6968 / 32768.;
     
    90589066image_transform_test_counter(png_uint_32 counter, unsigned int max)
    90599067{
    9060    /* Test the list to see if there is any point contining, given a current
     9068   /* Test the list to see if there is any point continuing, given a current
    90619069    * counter and a 'max' value.
    90629070    */
     
    1065010658      unsigned int i, j;
    1065110659
    10652       for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
    10653          if (i != j)
     10660      for (i=0; i<pm->ngamma_tests; ++i)
     10661      {
     10662         for (j=0; j<pm->ngamma_tests; ++j)
    1065410663         {
    10655             gamma_transform_test(pm, colour_type, bit_depth, palette_number,
    10656                pm->interlace_type, 1/pm->gammas[i], pm->gammas[j], 0/*sBIT*/,
    10657                pm->use_input_precision, 0 /*do not scale16*/);
    10658 
    10659             if (fail(pm))
    10660                return;
     10664            if (i != j)
     10665            {
     10666               gamma_transform_test(pm, colour_type, bit_depth, palette_number,
     10667                  pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
     10668                  0/*sBIT*/, pm->use_input_precision, 0/*do not scale16*/);
     10669
     10670               if (fail(pm))
     10671                  return;
     10672            }
    1066110673         }
     10674      }
    1066210675   }
    1066310676}
     
    1068810701            unsigned int j;
    1068910702
    10690             for (j=0; j<pm->ngamma_tests; ++j) if (i != j)
     10703            for (j=0; j<pm->ngamma_tests; ++j)
    1069110704            {
    10692                gamma_transform_test(pm, colour_type, bit_depth, npalette,
    10693                   pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
    10694                   sbit, pm->use_input_precision_sbit, 0 /*scale16*/);
    10695 
    10696                if (fail(pm))
    10697                   return;
     10705               if (i != j)
     10706               {
     10707                  gamma_transform_test(pm, colour_type, bit_depth, npalette,
     10708                     pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
     10709                     sbit, pm->use_input_precision_sbit, 0 /*scale16*/);
     10710
     10711                  if (fail(pm))
     10712                     return;
     10713               }
    1069810714            }
    1069910715         }
     
    1094710963
    1094810964      /* Don't skip the i==j case here - it's relevant. */
    10949       for (i=0; i<pm->ngamma_tests; ++i) for (j=0; j<pm->ngamma_tests; ++j)
    10950       {
    10951          gamma_composition_test(pm, colour_type, bit_depth, palette_number,
    10952             pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
    10953             pm->use_input_precision, do_background, expand_16);
    10954 
    10955          if (fail(pm))
    10956             return;
     10965      for (i=0; i<pm->ngamma_tests; ++i)
     10966      {
     10967         for (j=0; j<pm->ngamma_tests; ++j)
     10968         {
     10969            gamma_composition_test(pm, colour_type, bit_depth, palette_number,
     10970               pm->interlace_type, 1/pm->gammas[i], pm->gammas[j],
     10971               pm->use_input_precision, do_background, expand_16);
     10972
     10973            if (fail(pm))
     10974               return;
     10975         }
    1095710976      }
    1095810977   }
     
    1105711076
    1105811077      if (summary)
    11059          summarize_gamma_errors(pm, 0/*who*/, 1/*low bit depth*/, 1/*indexed*/);
     11078         summarize_gamma_errors(pm, NULL/*who*/, 1/*low bit depth*/, 1/*indexed*/);
    1106011079
    1106111080      if (fail(pm))
     
    1118311202   int x, y;
    1118411203   ++pass;
    11185    for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
    11186       return y;
     11204   for (y=0; y<8; ++y)
     11205      for (x=0; x<8; ++x)
     11206         if (adam7[y][x] == pass)
     11207            return y;
    1118711208   return 0xf;
    1118811209}
     
    1119311214   int x, y;
    1119411215   ++pass;
    11195    for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
    11196       return x;
     11216   for (x=0; x<8; ++x)
     11217      for (y=0; y<8; ++y)
     11218         if (adam7[y][x] == pass)
     11219            return x;
    1119711220   return 0xf;
    1119811221}
     
    1120311226   int x, y, base=(-1), inc=8;
    1120411227   ++pass;
    11205    for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
    11206    {
    11207       if (base == (-1))
    11208          base = y;
    11209       else if (base == y)
    11210          {}
    11211       else if (inc == y-base)
    11212          base=y;
    11213       else if (inc == 8)
    11214          inc = y-base, base=y;
    11215       else if (inc != y-base)
    11216          return 0xff; /* error - more than one 'inc' value! */
     11228   for (y=0; y<8; ++y)
     11229   {
     11230      for (x=0; x<8; ++x)
     11231      {
     11232         if (adam7[y][x] == pass)
     11233         {
     11234            if (base == (-1))
     11235               base = y;
     11236            else if (base == y)
     11237               {}
     11238            else if (inc == y-base)
     11239               base=y;
     11240            else if (inc == 8)
     11241               inc = y-base, base=y;
     11242            else if (inc != y-base)
     11243               return 0xff; /* error - more than one 'inc' value! */
     11244         }
     11245      }
    1121711246   }
    1121811247
     
    1123711266   int x, y, base=(-1), inc=8;
    1123811267   ++pass;
    11239    for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
    11240    {
    11241       if (base == (-1))
    11242          base = x;
    11243       else if (base == x)
    11244          {}
    11245       else if (inc == x-base)
    11246          base=x;
    11247       else if (inc == 8)
    11248          inc = x-base, base=x;
    11249       else if (inc != x-base)
    11250          return 0xff; /* error - more than one 'inc' value! */
     11268   for (x=0; x<8; ++x)
     11269   {
     11270      for (y=0; y<8; ++y)
     11271      {
     11272         if (adam7[y][x] == pass)
     11273         {
     11274            if (base == (-1))
     11275               base = x;
     11276            else if (base == x)
     11277               {}
     11278            else if (inc == x-base)
     11279               base=x;
     11280            else if (inc == 8)
     11281               inc = x-base, base=x;
     11282            else if (inc != x-base)
     11283               return 0xff; /* error - more than one 'inc' value! */
     11284         }
     11285      }
    1125111286   }
    1125211287
     
    1131211347   y &= 7;
    1131311348   ++pass;
    11314    for (x=0; x<8; ++x) if (adam7[y][x] == pass)
    11315       return 1;
     11349   for (x=0; x<8; ++x)
     11350      if (adam7[y][x] == pass)
     11351         return 1;
    1131611352
    1131711353   return 0;
     
    1132511361   x &= 7;
    1132611362   ++pass;
    11327    for (y=0; y<8; ++y) if (adam7[y][x] == pass)
    11328       return 1;
     11363   for (y=0; y<8; ++y)
     11364      if (adam7[y][x] == pass)
     11365         return 1;
    1132911366
    1133011367   return 0;
     
    1134011377   height &= 7;
    1134111378   ++pass;
    11342    for (y=0; y<8; ++y) for (x=0; x<8; ++x) if (adam7[y][x] == pass)
    11343    {
    11344       rows += tiles;
    11345       if (y < height) ++rows;
    11346       break; /* i.e. break the 'x', column, loop. */
     11379   for (y=0; y<8; ++y)
     11380   {
     11381      for (x=0; x<8; ++x)
     11382      {
     11383         if (adam7[y][x] == pass)
     11384         {
     11385            rows += tiles;
     11386            if (y < height) ++rows;
     11387            break; /* i.e. break the 'x', column, loop. */
     11388         }
     11389      }
    1134711390   }
    1134811391
     
    1135911402   width &= 7;
    1136011403   ++pass;
    11361    for (x=0; x<8; ++x) for (y=0; y<8; ++y) if (adam7[y][x] == pass)
    11362    {
    11363       cols += tiles;
    11364       if (x < width) ++cols;
    11365       break; /* i.e. break the 'y', row, loop. */
     11404   for (x=0; x<8; ++x)
     11405   {
     11406      for (y=0; y<8; ++y)
     11407      {
     11408         if (adam7[y][x] == pass)
     11409         {
     11410            cols += tiles;
     11411            if (x < width) ++cols;
     11412            break; /* i.e. break the 'y', row, loop. */
     11413         }
     11414      }
    1136611415   }
    1136711416
     
    1172011769   /* The following allows results to pass if they correspond to anything in the
    1172111770    * transformed range [input-.5,input+.5]; this is is required because of the
    11722     * way libpng treates the 16_TO_8 flag when building the gamma tables in
     11771    * way libpng treats the 16_TO_8 flag when building the gamma tables in
    1172311772    * releases up to 1.6.0.
    1172411773    *
     
    1181111860      else if (strncmp(*argv, "--transform-disable=",
    1181211861         sizeof "--transform-disable") == 0)
    11813          {
     11862      {
    1181411863         pm.test_transform = 1;
    1181511864         transform_disable(*argv + sizeof "--transform-disable");
    11816          }
     11865      }
    1181711866
    1181811867      else if (strncmp(*argv, "--transform-enable=",
    1181911868         sizeof "--transform-enable") == 0)
    11820          {
     11869      {
    1182111870         pm.test_transform = 1;
    1182211871         transform_enable(*argv + sizeof "--transform-enable");
    11823          }
     11872      }
    1182411873#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
    1182511874
    1182611875      else if (strcmp(*argv, "--gamma") == 0)
    11827          {
     11876      {
    1182811877         /* Just do two gamma tests here (2.2 and linear) for speed: */
    1182911878         pm.ngamma_tests = 2U;
     
    1183411883         pm.test_gamma_background = 1; /* composition */
    1183511884         pm.test_gamma_alpha_mode = 1;
    11836          }
     11885      }
    1183711886
    1183811887      else if (strcmp(*argv, "--nogamma") == 0)
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/readpng.c

    r96425 r103316  
     1
    12/* readpng.c
    23 *
    34 * Copyright (c) 2013 John Cunningham Bowler
    4  *
    5  * Last changed in libpng 1.6.1 [March 28, 2013]
    65 *
    76 * This code is released under the libpng license.
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/tarith.c

    r96425 r103316  
    22/* tarith.c
    33 *
     4 * Copyright (c) 2021 Cosmin Truta
    45 * Copyright (c) 2011-2013 John Cunningham Bowler
    5  *
    6  * Last changed in libpng 1.6.0 [February 14, 2013]
    76 *
    87 * This code is released under the libpng license.
     
    8988
    9089   while (--argc > 0)
     90   {
    9191      if (strcmp(*++argv, "-a") == 0)
    9292         showall = 1;
     
    106106         return 1;
    107107      }
     108   }
    108109
    109110   do
     
    131132      if (buffer[precision+7] != 71)
    132133      {
    133          fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n", test,
    134             precision, buffer, (unsigned long)strlen(buffer));
     134         fprintf(stderr, "%g[%d] -> '%s'[%lu] buffer overflow\n",
     135            test, precision, buffer, (unsigned long)strlen(buffer));
    135136         failed = 1;
    136137      }
     
    147148             test <  0 && strcmp(buffer, "-inf"))
    148149         {
    149             fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n", test,
    150                precision, buffer);
     150            fprintf(stderr, "%g[%d] -> '%s' but expected 'inf'\n",
     151               test, precision, buffer);
    151152            failed = 1;
    152153         }
     
    155156          buffer[index] != 0)
    156157      {
    157          fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n", test,
    158          precision, buffer, buffer[index]);
     158         fprintf(stderr, "%g[%d] -> '%s' but has bad format ('%c')\n",
     159            test, precision, buffer, buffer[index]);
    159160         failed = 1;
    160161      }
     
    188189         double out = atof(buffer);
    189190         double change = fabs((out - test)/test);
    190          double allow = .5/pow(10,
     191         double allow = .5 / pow(10,
    191192            (precision >= DBL_DIG) ? DBL_DIG-1 : precision-1);
    192193
     
    258259   while (--count);
    259260
    260    printf("Tested %d finite values, %d non-finite, %d OK (%d failed) %d minor "
    261       "arithmetic errors\n", finite, nonfinite, ok, failcount, minorarith);
     261   printf("Tested %d finite values, %d non-finite, %d OK (%d failed) "
     262      "%d minor arithmetic errors\n",
     263      finite, nonfinite, ok, failcount, minorarith);
    262264   printf(" Error with >=%d digit precision %.2f%%\n", DBL_DIG, max_abs);
    263265   printf(" Error with < %d digit precision %.2f%%\n", DBL_DIG, max);
     
    372374   if (index != 0 && index != 1)
    373375   {
    374       fprintf(stderr, "%s: read beyond end of string (%lu)\n", co->number,
    375          (unsigned long)index);
     376      fprintf(stderr, "%s: read beyond end of string (%lu)\n",
     377         co->number, (unsigned long)index);
    376378      return 0;
    377379   }
     
    504506      {
    505507         fprintf(stderr,
    506             "%s: character '%c' [0x%.2x] changed number validity\n", co->number,
    507             ch, ch);
     508            "%s: character '%c' [0x%.2x] changed number validity\n",
     509            co->number, ch, ch);
    508510         return 0;
    509511      }
     
    522524   int ch;
    523525
    524    if (c.cnumber+4 < sizeof co->number) for (ch=0; ch<256; ++ch)
    525    {
    526       if (!check_one_character(co, c, ch))
    527          return 0;
     526   if (c.cnumber+4 < sizeof co->number)
     527   {
     528      for (ch=0; ch<256; ++ch)
     529      {
     530         if (!check_one_character(co, c, ch))
     531            return 0;
     532      }
    528533   }
    529534
     
    540545   if (c.cnumber+4 < sizeof co->number && c.limit >= 0)
    541546   {
    542       if (c.limit > 0) for (i=0; tests[i]; ++i)
    543       {
    544          if (!check_one_character(co, c, tests[i]))
    545                return 0;
     547      if (c.limit > 0)
     548      {
     549         for (i=0; tests[i]; ++i)
     550         {
     551            if (!check_one_character(co, c, tests[i]))
     552                  return 0;
     553         }
    546554      }
    547555
     
    617625
    618626   while (--argc > 0)
    619       {
    620          fprintf(stderr, "unknown argument %s\n", *++argv);
    621          return 1;
    622       }
     627   {
     628      fprintf(stderr, "unknown argument %s\n", *++argv);
     629      return 1;
     630   }
    623631
    624632   /* Find out about the random number generator. */
     
    688696
    689697      if (verbose)
    690          fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n", a, times, div,
    691             fp, ok ? "ok" : "overflow");
     698         fprintf(stderr, "TEST %d * %d / %d -> %lld (%s)\n",
     699            a, times, div, fp, ok ? "ok" : "overflow");
    692700
    693701      ++tested;
     
    696704         ++error;
    697705         if (ok)
    698              fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n", a,
    699                 times, div, fp);
     706             fprintf(stderr, "%d * %d / %d -> overflow (expected %lld)\n",
     707                a, times, div, fp);
    700708         else
    701              fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n", a,
    702                 times, div, result, fp);
     709             fprintf(stderr, "%d * %d / %d -> %d (expected overflow %lld)\n",
     710                a, times, div, result, fp);
    703711      }
    704712      else if (ok && result != fpround)
    705713      {
    706714         ++error;
    707          fprintf(stderr, "%d * %d / %d -> %d not %lld\n", a, times, div, result,
    708             fp);
     715         fprintf(stderr, "%d * %d / %d -> %d not %lld\n",
     716            a, times, div, result, fp);
    709717      }
    710718      else
     
    722730   while (--count > 0);
    723731
    724    printf("%d tests including %d overflows, %d passed, %d failed (%d 64-bit "
    725       "errors)\n", tested, overflow, passed, error, error64);
     732   printf("%d tests including %d overflows, %d passed, %d failed "
     733      "(%d 64-bit errors)\n", tested, overflow, passed, error, error64);
    726734   return 0;
    727735}
     
    822830            if (error > .68) /* By experiment error is less than .68 */
    823831            {
    824                fprintf(stderr, "16-bit log error: %d: got %u, expected %f"
    825                   " error: %f\n", i, png_log16bit(i), correct, error);
     832               fprintf(stderr,
     833                  "16-bit log error: %d: got %u, expected %f error: %f\n",
     834                  i, png_log16bit(i), correct, error);
    826835            }
    827836         }
     
    842851         if (fabs(error) > 1883) /* By experiment. */
    843852         {
    844             fprintf(stderr, "32-bit exp error: %d: got %u, expected %f"
    845                   " error: %f\n", i, png_exp(i), correct, error);
     853            fprintf(stderr,
     854               "32-bit exp error: %d: got %u, expected %f error: %f\n",
     855               i, png_exp(i), correct, error);
    846856         }
    847857      }
     
    860870         if (fabs(error) > .50002) /* By experiment */
    861871         {
    862             fprintf(stderr, "8-bit exp error: %d: got %u, expected %f"
    863                   " error: %f\n", i, png_exp8bit(i), correct, error);
     872            fprintf(stderr,
     873               "8-bit exp error: %d: got %u, expected %f error: %f\n",
     874               i, png_exp8bit(i), correct, error);
    864875         }
    865876      }
     
    878889         if (fabs(error) > .524) /* By experiment */
    879890         {
    880             fprintf(stderr, "16-bit exp error: %d: got %u, expected %f"
    881                   " error: %f\n", i, png_exp16bit(i), correct, error);
     891            fprintf(stderr,
     892               "16-bit exp error: %d: got %u, expected %f error: %f\n",
     893               i, png_exp16bit(i), correct, error);
    882894         }
    883895      }
     
    941953/**************************** VALIDATION TESTS ********************************/
    942954/* Various validation routines are included herein, they require some
    943  * definition for png_warning and png_error, seetings of VALIDATION:
     955 * definition for png_warning and png_error, settings of VALIDATION:
    944956 *
    945957 * 1: validates the ASCII to floating point conversions
  • trunk/src/libs/libpng-1.6.42/contrib/libtests/timepng.c

    r96425 r103316  
     1
    12/* timepng.c
    23 *
    34 * Copyright (c) 2013,2016 John Cunningham Bowler
    4  *
    5  * Last changed in libpng 1.6.22 [May 26, 2016]
    65 *
    76 * This code is released under the libpng license.
     
    529528   }
    530529
    531    else if (nfiles == 0) /* Read from stdin withoout --dissemble */
     530   else if (nfiles == 0) /* Read from stdin without --dissemble */
    532531   {
    533532      char filename[FILENAME_MAX+1];
  • trunk/src/libs/libpng-1.6.42/contrib/mips-msa/linux.c

    r96425 r103316  
     1
    12/* contrib/mips-msa/linux.c
    23 *
     4 * Copyright (c) 2020-2023 Cosmin Truta
    35 * Copyright (c) 2016 Glenn Randers-Pehrson
    46 * Written by Mandar Sahastrabuddhe, 2016.
    5  * Last changed in libpng 1.6.25beta03 [August 29, 2016]
     7 * Updated by Sui Jingfeng, 2021.
    68 *
    79 * This code is released under the libpng license.
     
    911 * and license in png.h
    1012 *
    11  * SEE contrib/mips-msa/README before reporting bugs
     13 * On Linux, png_have_msa is implemented by reading the pseudo-file
     14 * "/proc/self/auxv".
     15 *
     16 * See contrib/mips-msa/README before reporting bugs.
    1217 *
    1318 * STATUS: SUPPORTED
    1419 * BUG REPORTS: [email protected]
    15  *
    16  * png_have_msa implemented for Linux by reading the widely available
    17  * pseudo-file /proc/cpuinfo.
    18  *
    19  * This code is strict ANSI-C and is probably moderately portable; it does
    20  * however use <stdio.h> and it assumes that /proc/cpuinfo is never localized.
    2120 */
    2221
    23 #include <stdio.h>
    24 #include <string.h>
     22#include <elf.h>
     23#include <fcntl.h>
    2524#include <stdlib.h>
     25#include <unistd.h>
    2626
    2727static int
    2828png_have_msa(png_structp png_ptr)
    2929{
    30    FILE *f = fopen("/proc/cpuinfo", "rb");
     30   Elf64_auxv_t aux;
     31   int fd;
     32   int has_msa = 0;
    3133
    32    char *string = "msa";
    33    char word[10];
     34   fd = open("/proc/self/auxv", O_RDONLY);
     35   if (fd >= 0)
     36   {
     37      while (read(fd, &aux, sizeof(Elf64_auxv_t)) == sizeof(Elf64_auxv_t))
     38      {
     39         if (aux.a_type == AT_HWCAP)
     40         {
     41            uint64_t hwcap = aux.a_un.a_val;
    3442
    35    if (f != NULL)
    36    {
    37       while(!feof(f))
    38       {
    39          int ch = fgetc(f);
    40          static int i = 0;
    41 
    42          while(!(ch <= 32))
    43          {
    44             word[i++] = ch;
    45             ch = fgetc(f);
     43            has_msa = (hwcap >> 1) & 1;
     44            break;
    4645         }
    47 
    48          int val = strcmp(string, word);
    49 
    50          if (val == 0)
    51             return 1;
    52 
    53          i = 0;
    54          memset(word, 0, 10);
    5546      }
    56 
    57       fclose(f);
     47      close(fd);
    5848   }
    5949#ifdef PNG_WARNINGS_SUPPORTED
    6050   else
    61       png_warning(png_ptr, "/proc/cpuinfo open failed");
     51      png_warning(png_ptr, "/proc/self/auxv open failed");
    6252#endif
    63    return 0;
     53
     54   return has_msa;
    6455}
  • trunk/src/libs/libpng-1.6.42/contrib/oss-fuzz/README.txt

    r96425 r103316  
    1 Last changed in libpng 1.6.33 [September 28, 2017]
    21Copyright (c) 2017 Glenn Randers-Pehrson
    32
  • trunk/src/libs/libpng-1.6.42/contrib/oss-fuzz/build.sh

    r96425 r103316  
    11#!/bin/bash -eu
     2
    23# Copyright 2017-2018 Glenn Randers-Pehrson
    34# Copyright 2016 Google Inc.
     
    1415# See the License for the specific language governing permissions and
    1516# limitations under the License.
    16 #
    17 # Last changed in libpng 1.6.35 [July 15, 2018]
    1817#
    1918# Revisions by Glenn Randers-Pehrson, 2017:
  • trunk/src/libs/libpng-1.6.42/contrib/oss-fuzz/libpng_read_fuzzer.cc

    r96425 r103316  
    55// Use of this source code is governed by a BSD-style license that may
    66// be found in the LICENSE file https://cs.chromium.org/chromium/src/LICENSE
    7 
    8 // Last changed in libpng 1.6.35 [July 15, 2018]
    97
    108// The modifications in 2017 by Glenn Randers-Pehrson include
     
    1816#include <stddef.h>
    1917#include <stdint.h>
     18#include <stdlib.h>
    2019#include <string.h>
    2120
     
    6160    if (end_info_ptr)
    6261      png_destroy_read_struct(&png_ptr, &info_ptr, &end_info_ptr);
    63     else if (info_ptr) 
     62    else if (info_ptr)
    6463      png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
    6564    else
     
    7776  buf_state->bytes_left -= length;
    7877  buf_state->data += length;
     78}
     79
     80void* limited_malloc(png_structp, png_alloc_size_t size) {
     81  // libpng may allocate large amounts of memory that the fuzzer reports as
     82  // an error. In order to silence these errors, make libpng fail when trying
     83  // to allocate a large amount. This allocator used to be in the Chromium
     84  // version of this fuzzer.
     85  // This number is chosen to match the default png_user_chunk_malloc_max.
     86  if (size > 8000000)
     87    return nullptr;
     88
     89  return malloc(size);
     90}
     91
     92void default_free(png_structp, png_voidp ptr) {
     93  return free(ptr);
    7994}
    8095
     
    118133    return 0;
    119134  }
     135
     136  // Use a custom allocator that fails for large allocations to avoid OOM.
     137  png_set_mem_fn(png_handler.png_ptr, nullptr, limited_malloc, default_free);
    120138
    121139  png_set_crc_action(png_handler.png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE);
  • trunk/src/libs/libpng-1.6.42/contrib/pngminus/CHANGES.txt

    r96425 r103316  
    44
    55version 1.0 - 1999.10.15 - First version.
    6         1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
     6        1.1 - 2015.07.29 - Fix memory leaks (Glenn Randers-Pehrson)
    77        1.2 - 2017.04.22 - Add buffer-size check
    88        1.3 - 2017.08.24 - Fix potential overflow in buffer-size check
     
    1212        1.6 - 2018.08.05 - Improve portability and fix style (Cosmin Truta)
    1313        1.7 - 2019.01.22 - Change license to MIT (Willem van Schaik)
     14        1.8 - 2024.01.09 - Fix, improve, modernize (Cosmin Truta)
  • trunk/src/libs/libpng-1.6.42/contrib/pngminus/CMakeLists.txt

    r96425 r103316  
    1 cmake_minimum_required(VERSION 3.1)
    2 cmake_policy(VERSION 3.1)
     1# Copyright (c) 2018-2024 Cosmin Truta
     2#
     3# This software is released under the MIT license. For conditions of
     4# distribution and use, see the LICENSE file part of this package.
     5
     6cmake_minimum_required(VERSION 3.5)
    37
    48project(PNGMINUS C)
    59
    6 option(PNGMINUS_USE_STATIC_LIBRARIES "Use the static library builds" ON)
     10option(PNGMINUS_USE_SYSTEM_PNG
     11       "Use the libpng build found in the system" OFF)
    712
    8 # libpng
    9 add_subdirectory(../.. libpng)
    10 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../..)
    11 include_directories(${CMAKE_CURRENT_BINARY_DIR}/libpng)
    12 if(PNGMINUS_USE_STATIC_LIBRARIES)
    13   set(PNGMINUS_PNG_LIBRARY png_static)
     13add_executable(png2pnm png2pnm.c)
     14add_executable(pnm2png pnm2png.c)
     15
     16if(PNGMINUS_USE_SYSTEM_PNG)
     17    # Use the system libpng.
     18    find_package(PNG REQUIRED)
     19    target_link_libraries(png2pnm PRIVATE PNG::PNG)
     20    target_link_libraries(pnm2png PRIVATE PNG::PNG)
    1421else()
    15   set(PNGMINUS_PNG_LIBRARY png)
     22    # Build and use the internal libpng.
     23    # Configure libpng for static linking, to produce single-file executables.
     24    set(PNG_STATIC ON
     25        CACHE STRING "Build the internal libpng as a static library" FORCE)
     26    set(PNG_SHARED OFF
     27        CACHE STRING "Build the internal libpng as a shared library" FORCE)
     28    set(PNG_FRAMEWORK OFF
     29        CACHE STRING "Build the internal libpng as a framework bundle" FORCE)
     30    add_subdirectory(../.. libpng)
     31    target_include_directories(png2pnm PRIVATE
     32        "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>"
     33        "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/libpng>"
     34    )
     35    target_include_directories(pnm2png PRIVATE
     36        "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../..>"
     37        "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/libpng>"
     38    )
     39    target_link_libraries(png2pnm PRIVATE png_static)
     40    target_link_libraries(pnm2png PRIVATE png_static)
    1641endif()
    17 
    18 # png2pnm
    19 add_executable(png2pnm png2pnm.c)
    20 target_link_libraries(png2pnm ${PNGMINUS_PNG_LIBRARY})
    21 
    22 # pnm2png
    23 add_executable(pnm2png pnm2png.c)
    24 target_link_libraries(pnm2png ${PNGMINUS_PNG_LIBRARY})
  • trunk/src/libs/libpng-1.6.42/contrib/pngminus/LICENSE.txt

    r96425 r103316  
    1717implied, including but not limited to the warranties of merchantability,
    1818fitness for a particular purpose and noninfringement. In no event shall the
    19 authors or copyight holders be liable for any claim, damages or other
     19authors or copyright holders be liable for any claim, damages or other
    2020liability, whether in an action of contract, tort or otherwise, arising from,
    2121out of or in connection with the software or the use or other dealings in the
  • trunk/src/libs/libpng-1.6.42/contrib/pngminus/png2pnm.c

    r96425 r103316  
    1515#endif
    1616#ifndef TRUE
    17 #define TRUE (BOOL) 1
     17#define TRUE ((BOOL) 1)
    1818#endif
    1919#ifndef FALSE
    20 #define FALSE (BOOL) 0
    21 #endif
    22 
    23 /* make png2pnm verbose so we can find problems (needs to be before png.h) */
    24 #ifndef PNG_DEBUG
    25 #define PNG_DEBUG 0
     20#define FALSE ((BOOL) 0)
    2621#endif
    2722
     
    3429BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
    3530              BOOL raw, BOOL alpha);
     31BOOL do_png2pnm (png_struct *png_ptr, png_info *info_ptr,
     32                 FILE *pnm_file, FILE *alpha_file,
     33                 BOOL raw, BOOL alpha);
    3634
    3735/*
     
    4442  FILE *fp_wr = stdout;
    4543  FILE *fp_al = NULL;
     44  const char *fname_wr = NULL;
     45  const char *fname_al = NULL;
    4646  BOOL raw = TRUE;
    4747  BOOL alpha = FALSE;
    4848  int argi;
     49  int ret;
    4950
    5051  for (argi = 1; argi < argc; argi++)
     
    6566          if ((fp_al = fopen (argv[argi], "wb")) == NULL)
    6667          {
     68            fname_al = argv[argi];
    6769            fprintf (stderr, "PNM2PNG\n");
    6870            fprintf (stderr, "Error:  cannot create alpha-channel file %s\n",
     
    9597    else if (fp_wr == stdout)
    9698    {
     99      fname_wr = argv[argi];
    97100      if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
    98101      {
     
    120123
    121124  /* call the conversion program itself */
    122   if (png2pnm (fp_rd, fp_wr, fp_al, raw, alpha) == FALSE)
    123   {
    124     fprintf (stderr, "PNG2PNM\n");
    125     fprintf (stderr, "Error:  unsuccessful conversion of PNG-image\n");
    126     exit (1);
    127   }
     125  ret = png2pnm (fp_rd, fp_wr, fp_al, raw, alpha);
    128126
    129127  /* close input file */
     
    134132  if (alpha)
    135133    fclose (fp_al);
     134
     135  if (!ret)
     136  {
     137    fprintf (stderr, "PNG2PNM\n");
     138    fprintf (stderr, "Error:  unsuccessful conversion of PNG-image\n");
     139    if (fname_wr)
     140      remove (fname_wr); /* no broken output file shall remain behind */
     141    if (fname_al)
     142      remove (fname_al); /* ditto */
     143    exit (1);
     144  }
    136145
    137146  return 0;
     
    164173              BOOL raw, BOOL alpha)
    165174{
    166   png_struct    *png_ptr = NULL;
    167   png_info      *info_ptr = NULL;
    168   png_byte      buf[8];
    169   png_byte      *png_pixels = NULL;
    170   png_byte      **row_pointers = NULL;
    171   png_byte      *pix_ptr = NULL;
    172   png_uint_32   row_bytes;
    173 
     175  png_struct    *png_ptr;
     176  png_info      *info_ptr;
     177  BOOL          ret;
     178
     179  /* initialize the libpng context for reading from png_file */
     180
     181  png_ptr = png_create_read_struct (png_get_libpng_ver(NULL),
     182                                    NULL, NULL, NULL);
     183  if (!png_ptr)
     184    return FALSE; /* out of memory */
     185
     186  info_ptr = png_create_info_struct (png_ptr);
     187  if (!info_ptr)
     188  {
     189    png_destroy_read_struct (&png_ptr, NULL, NULL);
     190    return FALSE; /* out of memory */
     191  }
     192
     193  if (setjmp (png_jmpbuf (png_ptr)))
     194  {
     195    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
     196    return FALSE; /* generic libpng error */
     197  }
     198
     199  png_init_io (png_ptr, png_file);
     200
     201  /* do the actual conversion */
     202  ret = do_png2pnm (png_ptr, info_ptr, pnm_file, alpha_file, raw, alpha);
     203
     204  /* clean up the libpng structures and their internally-managed data */
     205  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
     206
     207  return ret;
     208}
     209
     210/*
     211 *  do_png2pnm - does the conversion in a fully-initialized libpng context
     212 */
     213
     214BOOL do_png2pnm (png_struct *png_ptr, png_info *info_ptr,
     215                 FILE *pnm_file, FILE *alpha_file,
     216                 BOOL raw, BOOL alpha)
     217{
     218  png_byte      **row_pointers;
     219  png_byte      *pix_ptr;
    174220  png_uint_32   width;
    175221  png_uint_32   height;
     
    178224  int           color_type;
    179225  int           alpha_present;
    180   int           row, col;
    181   int           ret;
    182   int           i;
     226  png_uint_32   row, col, i;
    183227  long          dep_16;
    184228
    185   /* read and check signature in PNG file */
    186   ret = fread (buf, 1, 8, png_file);
    187   if (ret != 8)
    188     return FALSE;
    189 
    190   ret = png_sig_cmp (buf, 0, 8);
    191   if (ret != 0)
    192     return FALSE;
    193 
    194   /* create png and info structures */
    195 
    196   png_ptr = png_create_read_struct (png_get_libpng_ver(NULL),
    197                                     NULL, NULL, NULL);
    198   if (!png_ptr)
    199     return FALSE; /* out of memory */
    200 
    201   info_ptr = png_create_info_struct (png_ptr);
    202   if (!info_ptr)
    203   {
    204     png_destroy_read_struct (&png_ptr, NULL, NULL);
    205     return FALSE; /* out of memory */
    206   }
    207 
    208   if (setjmp (png_jmpbuf (png_ptr)))
    209   {
    210     png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
    211     return FALSE;
    212   }
    213 
    214   /* set up the input control for C streams */
    215   png_init_io (png_ptr, png_file);
    216   png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
    217 
    218   /* read the file information */
    219   png_read_info (png_ptr, info_ptr);
    220 
    221   /* get size and bit-depth of the PNG-image */
    222   png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
    223                 NULL, NULL, NULL);
    224 
    225   /* set-up the transformations */
    226 
    227   /* transform paletted images into full-color rgb */
    228   if (color_type == PNG_COLOR_TYPE_PALETTE)
    229     png_set_expand (png_ptr);
    230   /* expand images to bit-depth 8 (only applicable for grayscale images) */
    231   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
    232     png_set_expand (png_ptr);
    233   /* transform transparency maps into full alpha-channel */
    234   if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
    235     png_set_expand (png_ptr);
     229  /* set up the image transformations that are necessary for the PNM format */
     230
     231  /* set up (if applicable) the expansion of paletted images to full-color rgb,
     232   * and the expansion of transparency maps to full alpha-channel */
     233  png_set_expand (png_ptr);
     234
     235  /* set up (if applicable) the expansion of grayscale images to bit-depth 8 */
     236  png_set_expand_gray_1_2_4_to_8 (png_ptr);
    236237
    237238#ifdef NJET
     
    243244      color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
    244245    png_set_gray_to_rgb (png_ptr);
    245   /* only if file has a file gamma, we do a correction */
    246   if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
    247     png_set_gamma (png_ptr, (double) 2.2, file_gamma);
    248 #endif
    249 
    250   /* all transformations have been registered; now update info_ptr data,
    251    * get rowbytes and channels, and allocate image memory */
    252 
    253   png_read_update_info (png_ptr, info_ptr);
    254 
    255   /* get the new color-type and bit-depth (after expansion/stripping) */
     246  /* if the PNG image has a gAMA chunk then gamma-correct the output image */
     247  {
     248    double file_gamma;
     249    if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
     250      png_set_gamma (png_ptr, (double) 2.2, file_gamma);
     251  }
     252#endif
     253
     254  /* read the image file, with all of the above image transforms applied */
     255  png_read_png (png_ptr, info_ptr, 0, NULL);
     256
     257  /* get the image size, bit-depth and color-type */
    256258  png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
    257259                NULL, NULL, NULL);
    258260
    259   /* check for 16-bit files */
    260   if (bit_depth == 16)
    261   {
    262     raw = FALSE;
    263 #if defined(O_BINARY) && (O_BINARY != 0)
    264     setmode (fileno (pnm_file), O_BINARY);
    265 #endif
    266   }
    267 
    268   /* calculate new number of channels and store alpha-presence */
     261  /* calculate the number of channels and store alpha-presence */
    269262  if (color_type == PNG_COLOR_TYPE_GRAY)
    270263    channels = 1;
     
    283276  {
    284277    fprintf (stderr, "PNG2PNM\n");
    285     fprintf (stderr, "Error:  PNG-file doesn't contain alpha channel\n");
    286     exit (1);
    287   }
    288 
    289   /* row_bytes is the width x number of channels x (bit-depth / 8) */
    290   row_bytes = png_get_rowbytes (png_ptr, info_ptr);
    291 
    292   if ((row_bytes == 0) ||
    293       ((size_t) height > (size_t) (-1) / (size_t) row_bytes))
    294   {
    295     /* too big */
    296     png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
     278    fprintf (stderr, "Warning:  no alpha channel in PNG file\n");
    297279    return FALSE;
    298280  }
    299   if ((png_pixels = (png_byte *)
    300        malloc ((size_t) row_bytes * (size_t) height)) == NULL)
    301   {
    302     png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
    303     return FALSE;
    304   }
    305 
    306   if ((row_pointers = (png_byte **)
    307        malloc ((size_t) height * sizeof (png_byte *))) == NULL)
    308   {
    309     png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
    310     free (png_pixels);
    311     return FALSE;
    312   }
    313 
    314   /* set the individual row_pointers to point at the correct offsets */
    315   for (i = 0; i < ((int) height); i++)
    316     row_pointers[i] = png_pixels + i * row_bytes;
    317 
    318   /* now we can go ahead and just read the whole image */
    319   png_read_image (png_ptr, row_pointers);
    320 
    321   /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
    322   png_read_end (png_ptr, info_ptr);
    323 
    324   /* clean up after the read, and free any memory allocated - REQUIRED */
    325   png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
     281
     282  /* get address of internally-allocated image data */
     283  row_pointers = png_get_rows (png_ptr, info_ptr);
    326284
    327285  /* write header of PNM file */
     
    354312
    355313  /* write data to PNM file */
    356   pix_ptr = png_pixels;
    357 
    358   for (row = 0; row < (int) height; row++)
    359   {
    360     for (col = 0; col < (int) width; col++)
    361     {
    362       for (i = 0; i < (channels - alpha_present); i++)
     314
     315  for (row = 0; row < height; row++)
     316  {
     317    pix_ptr = row_pointers[row];
     318    for (col = 0; col < width; col++)
     319    {
     320      for (i = 0; i < (png_uint_32) (channels - alpha_present); i++)
    363321      {
    364322        if (raw)
    365323        {
    366324          fputc ((int) *pix_ptr++, pnm_file);
     325          if (bit_depth == 16)
     326            fputc ((int) *pix_ptr++, pnm_file);
    367327        }
    368328        else
     
    370330          if (bit_depth == 16)
    371331          {
    372             dep_16 = (long) *pix_ptr++;
    373             fprintf (pnm_file, "%ld ", (dep_16 << 8) + ((long) *pix_ptr++));
     332            dep_16 = ((long) *pix_ptr++) << 8;
     333            dep_16 += ((long) *pix_ptr++);
     334            fprintf (pnm_file, "%ld ", dep_16);
    374335          }
    375336          else
     
    383344        if (!alpha)
    384345        {
    385           pix_ptr++; /* alpha */
     346          /* skip the alpha-channel */
     347          pix_ptr++;
    386348          if (bit_depth == 16)
    387349            pix_ptr++;
    388350        }
    389         else /* output alpha-channel as pgm file */
     351        else
    390352        {
     353          /* output the alpha-channel as pgm file */
    391354          if (raw)
    392355          {
    393356            fputc ((int) *pix_ptr++, alpha_file);
     357            if (bit_depth == 16)
     358              fputc ((int) *pix_ptr++, alpha_file);
    394359          }
    395360          else
     
    397362            if (bit_depth == 16)
    398363            {
    399               dep_16 = (long) *pix_ptr++;
    400               fprintf (alpha_file, "%ld ", (dep_16 << 8) + (long) *pix_ptr++);
     364              dep_16 = ((long) *pix_ptr++) << 8;
     365              dep_16 += ((long) *pix_ptr++);
     366              fprintf (alpha_file, "%ld ", dep_16);
    401367            }
    402368            else
     
    418384  } /* end for row */
    419385
    420   if (row_pointers != NULL)
    421     free (row_pointers);
    422   if (png_pixels != NULL)
    423     free (png_pixels);
    424 
    425386  return TRUE;
    426 
    427387} /* end of source */
  • trunk/src/libs/libpng-1.6.42/contrib/pngminus/pnm2png.c

    r96425 r103316  
    77 */
    88
     9#include <limits.h>
    910#include <stdio.h>
    1011#include <stdlib.h>
     
    1516#endif
    1617#ifndef TRUE
    17 #define TRUE (BOOL) 1
     18#define TRUE ((BOOL) 1)
    1819#endif
    1920#ifndef FALSE
    20 #define FALSE (BOOL) 0
    21 #endif
    22 
    23 /* make pnm2png verbose so we can find problems (needs to be before png.h) */
    24 #ifndef PNG_DEBUG
    25 #define PNG_DEBUG 0
     21#define FALSE ((BOOL) 0)
    2622#endif
    2723
     
    3430BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
    3531              BOOL interlace, BOOL alpha);
    36 void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size);
    37 png_uint_32 get_data (FILE *pnm_file, int depth);
    38 png_uint_32 get_value (FILE *pnm_file, int depth);
     32BOOL do_pnm2png (png_struct *png_ptr, png_info *info_ptr,
     33                 FILE *pnm_file, FILE *alpha_file,
     34                 BOOL interlace, BOOL alpha);
     35int fscan_pnm_magic (FILE *pnm_file, char *magic_buf, size_t magic_buf_size);
     36int fscan_pnm_token (FILE *pnm_file, char *token_buf, size_t token_buf_size);
     37int fscan_pnm_uint_32 (FILE *pnm_file, png_uint_32 *num_ptr);
     38png_uint_32 get_pnm_data (FILE *pnm_file, int depth);
     39png_uint_32 get_pnm_value (FILE *pnm_file, int depth);
    3940
    4041/*
     
    4748  FILE *fp_al = NULL;
    4849  FILE *fp_wr = stdout;
     50  const char *fname_wr = NULL;
    4951  BOOL interlace = FALSE;
    5052  BOOL alpha = FALSE;
    5153  int argi;
     54  int ret;
    5255
    5356  for (argi = 1; argi < argc; argi++)
     
    9598    else if (fp_wr == stdout)
    9699    {
     100      fname_wr = argv[argi];
    97101      if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
    98102      {
     
    122126
    123127  /* call the conversion program itself */
    124   if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
    125   {
    126     fprintf (stderr, "PNM2PNG\n");
    127     fprintf (stderr, "Error:  unsuccessful converting to PNG-image\n");
    128     exit (1);
    129   }
     128  ret = pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha);
    130129
    131130  /* close input file */
     
    136135  if (alpha)
    137136    fclose (fp_al);
     137
     138  if (!ret)
     139  {
     140    fprintf (stderr, "PNM2PNG\n");
     141    fprintf (stderr, "Error:  unsuccessful converting to PNG-image\n");
     142    if (fname_wr)
     143      remove (fname_wr); /* no broken output file shall remain behind */
     144    exit (1);
     145  }
    138146
    139147  return 0;
     
    164172              BOOL interlace, BOOL alpha)
    165173{
    166   png_struct    *png_ptr = NULL;
    167   png_info      *info_ptr = NULL;
    168   png_byte      *png_pixels = NULL;
    169   png_byte      **row_pointers = NULL;
    170   png_byte      *pix_ptr = NULL;
    171   volatile png_uint_32 row_bytes;
    172 
    173   char          type_token[16];
    174   char          width_token[16];
    175   char          height_token[16];
    176   char          maxval_token[16];
    177   volatile int  color_type = 1;
    178   unsigned long ul_width = 0, ul_alpha_width = 0;
    179   unsigned long ul_height = 0, ul_alpha_height = 0;
    180   unsigned long ul_maxval = 0;
    181   volatile png_uint_32 width = 0, height = 0;
    182   volatile png_uint_32 alpha_width = 0, alpha_height = 0;
    183   png_uint_32   maxval;
    184   volatile int  bit_depth = 0;
    185   int           channels = 0;
    186   int           alpha_depth = 0;
    187   int           alpha_present = 0;
    188   int           row, col;
    189   BOOL          raw, alpha_raw = FALSE;
    190 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
     174  png_struct    *png_ptr;
     175  png_info      *info_ptr;
     176  BOOL          ret;
     177
     178  /* initialize the libpng context for writing to png_file */
     179
     180  png_ptr = png_create_write_struct (png_get_libpng_ver(NULL),
     181                                     NULL, NULL, NULL);
     182  if (!png_ptr)
     183    return FALSE; /* out of memory */
     184
     185  info_ptr = png_create_info_struct (png_ptr);
     186  if (!info_ptr)
     187  {
     188    png_destroy_write_struct (&png_ptr, NULL);
     189    return FALSE; /* out of memory */
     190  }
     191
     192  if (setjmp (png_jmpbuf (png_ptr)))
     193  {
     194    png_destroy_write_struct (&png_ptr, &info_ptr);
     195    return FALSE; /* generic libpng error */
     196  }
     197
     198  png_init_io (png_ptr, png_file);
     199
     200  /* do the actual conversion */
     201  ret = do_pnm2png (png_ptr, info_ptr, pnm_file, alpha_file, interlace, alpha);
     202
     203  /* clean up the libpng structures and their internally-managed data */
     204  png_destroy_write_struct (&png_ptr, &info_ptr);
     205
     206  return ret;
     207}
     208
     209/*
     210 *  do_pnm2png - does the conversion in a fully-initialized libpng context
     211 */
     212
     213BOOL do_pnm2png (png_struct *png_ptr, png_info *info_ptr,
     214                 FILE *pnm_file, FILE *alpha_file,
     215                 BOOL interlace, BOOL alpha)
     216{
     217  png_byte      **row_pointers;
     218  png_byte      *pix_ptr;
     219  int           bit_depth;
     220  int           color_type;
     221  int           channels;
     222  char          magic_token[4];
     223  BOOL          raw;
     224  png_uint_32   width, height, maxval;
     225  png_uint_32   row_bytes;
     226  png_uint_32   row, col;
     227  png_uint_32   val16, i;
     228  png_uint_32   alpha_width = 0, alpha_height = 0;
     229  int           alpha_depth = 0, alpha_present = 0;
     230  BOOL          alpha_raw = FALSE;
    191231  BOOL          packed_bitmap = FALSE;
    192 #endif
    193   png_uint_32   tmp16;
    194   int           i;
    195232
    196233  /* read header of PNM file */
    197234
    198   get_token (pnm_file, type_token, sizeof (type_token));
    199   if (type_token[0] != 'P')
    200   {
    201     return FALSE;
    202   }
    203   else if ((type_token[1] == '1') || (type_token[1] == '4'))
    204   {
    205 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
    206     raw = (type_token[1] == '4');
     235  if (fscan_pnm_magic (pnm_file, magic_token, sizeof (magic_token)) != 1)
     236    return FALSE; /* not a PNM file */
     237
     238  if ((magic_token[1] == '1') || (magic_token[1] == '4'))
     239  {
     240    if ((fscan_pnm_uint_32 (pnm_file, &width) != 1) ||
     241        (fscan_pnm_uint_32 (pnm_file, &height) != 1))
     242      return FALSE; /* bad PBM file header */
     243  } else if ((magic_token[1] == '2') || (magic_token[1] == '5') ||
     244             (magic_token[1] == '3') || (magic_token[1] == '6'))
     245  {
     246    if ((fscan_pnm_uint_32 (pnm_file, &width) != 1) ||
     247        (fscan_pnm_uint_32 (pnm_file, &height) != 1) ||
     248        (fscan_pnm_uint_32 (pnm_file, &maxval) != 1))
     249      return FALSE; /* bad PGM/PPM file header */
     250  }
     251
     252  if ((magic_token[1] == '1') || (magic_token[1] == '4'))
     253  {
     254    raw = (magic_token[1] == '4');
     255    bit_depth = 1;
    207256    color_type = PNG_COLOR_TYPE_GRAY;
    208     get_token (pnm_file, width_token, sizeof (width_token));
    209     sscanf (width_token, "%lu", &ul_width);
    210     width = (png_uint_32) ul_width;
    211     get_token (pnm_file, height_token, sizeof (height_token));
    212     sscanf (height_token, "%lu", &ul_height);
    213     height = (png_uint_32) ul_height;
    214     bit_depth = 1;
    215257    packed_bitmap = TRUE;
    216 #else
    217     fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and\n");
    218     fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
    219     return FALSE;
    220 #endif
    221   }
    222   else if ((type_token[1] == '2') || (type_token[1] == '5'))
    223   {
    224     raw = (type_token[1] == '5');
     258  }
     259  else if ((magic_token[1] == '2') || (magic_token[1] == '5'))
     260  {
     261    raw = (magic_token[1] == '5');
    225262    color_type = PNG_COLOR_TYPE_GRAY;
    226     get_token (pnm_file, width_token, sizeof (width_token));
    227     sscanf (width_token, "%lu", &ul_width);
    228     width = (png_uint_32) ul_width;
    229     get_token (pnm_file, height_token, sizeof (height_token));
    230     sscanf (height_token, "%lu", &ul_height);
    231     height = (png_uint_32) ul_height;
    232     get_token (pnm_file, maxval_token, sizeof (maxval_token));
    233     sscanf (maxval_token, "%lu", &ul_maxval);
    234     maxval = (png_uint_32) ul_maxval;
    235 
    236     if (maxval <= 1)
     263    if (maxval == 0)
     264      return FALSE;
     265    else if (maxval == 1)
    237266      bit_depth = 1;
    238267    else if (maxval <= 3)
     
    247276      return FALSE;
    248277  }
    249   else if ((type_token[1] == '3') || (type_token[1] == '6'))
    250   {
    251     raw = (type_token[1] == '6');
     278  else if ((magic_token[1] == '3') || (magic_token[1] == '6'))
     279  {
     280    raw = (magic_token[1] == '6');
    252281    color_type = PNG_COLOR_TYPE_RGB;
    253     get_token (pnm_file, width_token, sizeof (width_token));
    254     sscanf (width_token, "%lu", &ul_width);
    255     width = (png_uint_32) ul_width;
    256     get_token (pnm_file, height_token, sizeof (height_token));
    257     sscanf (height_token, "%lu", &ul_height);
    258     height = (png_uint_32) ul_height;
    259     get_token (pnm_file, maxval_token, sizeof (maxval_token));
    260     sscanf (maxval_token, "%lu", &ul_maxval);
    261     maxval = (png_uint_32) ul_maxval;
    262     if (maxval <= 1)
     282    if (maxval == 0)
     283      return FALSE;
     284    else if (maxval == 1)
    263285      bit_depth = 1;
    264286    else if (maxval <= 3)
     
    273295      return FALSE;
    274296  }
     297  else if (magic_token[1] == '7')
     298  {
     299    fprintf (stderr, "PNM2PNG can't read PAM (P7) files\n");
     300    return FALSE;
     301  }
    275302  else
    276303  {
     
    282309  if (alpha)
    283310  {
    284     if (color_type == PNG_COLOR_TYPE_GRAY)
    285       color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
    286     if (color_type == PNG_COLOR_TYPE_RGB)
    287       color_type = PNG_COLOR_TYPE_RGB_ALPHA;
    288 
    289     get_token (alpha_file, type_token, sizeof (type_token));
    290     if (type_token[0] != 'P')
    291     {
     311    if ((fscan_pnm_magic (alpha_file, magic_token, sizeof (magic_token)) != 1)
     312        || ((magic_token[1] != '2') && (magic_token[1] != '5')))
     313      return FALSE; /* not a PGM file */
     314
     315    if ((fscan_pnm_uint_32 (alpha_file, &alpha_width) != 1) ||
     316        (fscan_pnm_uint_32 (alpha_file, &alpha_height) != 1) ||
     317        (fscan_pnm_uint_32 (alpha_file, &maxval) != 1))
     318      return FALSE; /* bad PGM file header */
     319
     320    if ((alpha_width != width) || (alpha_height != height))
     321      return FALSE; /* mismatched PGM dimensions */
     322
     323    alpha_raw = (magic_token[1] == '5');
     324    color_type |= PNG_COLOR_MASK_ALPHA;
     325    if (maxval == 0)
    292326      return FALSE;
    293     }
    294     else if ((type_token[1] == '2') || (type_token[1] == '5'))
    295     {
    296       alpha_raw = (type_token[1] == '5');
    297       get_token (alpha_file, width_token, sizeof (width_token));
    298       sscanf (width_token, "%lu", &ul_alpha_width);
    299       alpha_width = (png_uint_32) ul_alpha_width;
    300       if (alpha_width != width)
    301         return FALSE;
    302       get_token (alpha_file, height_token, sizeof (height_token));
    303       sscanf (height_token, "%lu", &ul_alpha_height);
    304       alpha_height = (png_uint_32) ul_alpha_height;
    305       if (alpha_height != height)
    306         return FALSE;
    307       get_token (alpha_file, maxval_token, sizeof (maxval_token));
    308       sscanf (maxval_token, "%lu", &ul_maxval);
    309       maxval = (png_uint_32) ul_maxval;
    310       if (maxval <= 1)
    311         alpha_depth = 1;
    312       else if (maxval <= 3)
    313         alpha_depth = 2;
    314       else if (maxval <= 15)
    315         alpha_depth = 4;
    316       else if (maxval <= 255)
    317         alpha_depth = 8;
    318       else if (maxval <= 65535U)
    319         alpha_depth = 16;
    320       else /* maxval > 65535U */
    321         return FALSE;
    322       if (alpha_depth != bit_depth)
    323         return FALSE;
    324     }
    325     else
    326     {
     327    else if (maxval == 1)
     328      alpha_depth = 1;
     329    else if (maxval <= 3)
     330      alpha_depth = 2;
     331    else if (maxval <= 15)
     332      alpha_depth = 4;
     333    else if (maxval <= 255)
     334      alpha_depth = 8;
     335    else if (maxval <= 65535U)
     336      alpha_depth = 16;
     337    else /* maxval > 65535U */
    327338      return FALSE;
    328     }
     339    if (alpha_depth != bit_depth)
     340      return FALSE;
    329341  } /* end if alpha */
    330342
     
    338350  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
    339351    channels = 4;
    340 #if 0
    341352  else
    342     channels = 0; /* cannot happen */
    343 #endif
     353    return FALSE; /* NOTREACHED */
    344354
    345355  alpha_present = (channels - 1) % 2;
    346356
    347 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
    348357  if (packed_bitmap)
    349358  {
     
    352361  }
    353362  else
    354 #endif
    355363  {
    356364    /* row_bytes is the width x number of channels x (bit-depth / 8) */
     
    364372    return FALSE;
    365373  }
    366   if ((png_pixels = (png_byte *)
    367        malloc ((size_t) row_bytes * (size_t) height)) == NULL)
    368   {
    369     /* out of memory */
    370     return FALSE;
    371   }
    372 
    373   /* read data from PNM file */
    374   pix_ptr = png_pixels;
    375 
    376   for (row = 0; row < (int) height; row++)
    377   {
    378 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
     374
     375  /* allocate the rows using the same memory layout as libpng, and transfer
     376   * their ownership to libpng, with the responsibility to clean everything up;
     377   * please note the use of png_calloc instead of png_malloc */
     378  row_pointers = (png_byte **)
     379                 png_calloc (png_ptr, height * sizeof (png_byte *));
     380  png_set_rows (png_ptr, info_ptr, row_pointers);
     381  png_data_freer (png_ptr, info_ptr, PNG_DESTROY_WILL_FREE_DATA, PNG_FREE_ALL);
     382  for (row = 0; row < height; row++)
     383  {
     384    /* the individual rows should only be allocated after all the previous
     385     * steps completed successfully, because libpng must handle correctly
     386     * any image allocation left incomplete after an out-of-memory error */
     387    row_pointers[row] = (png_byte *) png_malloc (png_ptr, row_bytes);
     388  }
     389
     390  /* read the data from PNM file */
     391
     392  for (row = 0; row < height; row++)
     393  {
     394    pix_ptr = row_pointers[row];
    379395    if (packed_bitmap)
    380396    {
    381       for (i = 0; i < (int) row_bytes; i++)
     397      for (i = 0; i < row_bytes; i++)
    382398      {
    383399        /* png supports this format natively so no conversion is needed */
    384         *pix_ptr++ = get_data (pnm_file, 8);
     400        *pix_ptr++ = get_pnm_data (pnm_file, 8);
    385401      }
    386402    }
    387403    else
    388 #endif
    389     {
    390       for (col = 0; col < (int) width; col++)
     404    {
     405      for (col = 0; col < width; col++)
    391406      {
    392         for (i = 0; i < (channels - alpha_present); i++)
     407        for (i = 0; i < (png_uint_32) (channels - alpha_present); i++)
    393408        {
    394409          if (raw)
    395410          {
    396             *pix_ptr++ = get_data (pnm_file, bit_depth);
     411            *pix_ptr++ = get_pnm_data (pnm_file, bit_depth);
     412            if (bit_depth == 16)
     413              *pix_ptr++ = get_pnm_data (pnm_file, bit_depth);
    397414          }
    398415          else
     
    400417            if (bit_depth <= 8)
    401418            {
    402               *pix_ptr++ = get_value (pnm_file, bit_depth);
     419              *pix_ptr++ = get_pnm_value (pnm_file, bit_depth);
    403420            }
    404421            else
    405422            {
    406               tmp16 = get_value (pnm_file, bit_depth);
    407               *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
     423              val16 = get_pnm_value (pnm_file, bit_depth);
     424              *pix_ptr = (png_byte) ((val16 >> 8) & 0xFF);
    408425              pix_ptr++;
    409               *pix_ptr = (png_byte) (tmp16 & 0xFF);
     426              *pix_ptr = (png_byte) (val16 & 0xFF);
    410427              pix_ptr++;
    411428            }
     
    417434          if (alpha_raw)
    418435          {
    419             *pix_ptr++ = get_data (alpha_file, alpha_depth);
     436            *pix_ptr++ = get_pnm_data (alpha_file, alpha_depth);
     437            if (alpha_depth == 16)
     438              *pix_ptr++ = get_pnm_data (alpha_file, alpha_depth);
    420439          }
    421440          else
     
    423442            if (alpha_depth <= 8)
    424443            {
    425               *pix_ptr++ = get_value (alpha_file, bit_depth);
     444              *pix_ptr++ = get_pnm_value (alpha_file, bit_depth);
    426445            }
    427446            else
    428447            {
    429               tmp16 = get_value (alpha_file, bit_depth);
    430               *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
    431               *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
     448              val16 = get_pnm_value (alpha_file, bit_depth);
     449              *pix_ptr++ = (png_byte) ((val16 >> 8) & 0xFF);
     450              *pix_ptr++ = (png_byte) (val16 & 0xFF);
    432451            }
    433452          }
     
    437456  } /* end for row */
    438457
    439   /* prepare the standard PNG structures */
    440   png_ptr = png_create_write_struct (png_get_libpng_ver(NULL),
    441                                      NULL, NULL, NULL);
    442   if (!png_ptr)
    443   {
    444     free (png_pixels);
    445     return FALSE;
    446   }
    447   info_ptr = png_create_info_struct (png_ptr);
    448   if (!info_ptr)
    449   {
    450     png_destroy_write_struct (&png_ptr, NULL);
    451     free (png_pixels);
    452     return FALSE;
    453   }
    454 
    455 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
    456   if (packed_bitmap == TRUE)
    457   {
    458     png_set_packing (png_ptr);
    459     png_set_invert_mono (png_ptr);
    460   }
    461 #endif
    462 
    463   if (setjmp (png_jmpbuf (png_ptr)))
    464   {
    465     png_destroy_write_struct (&png_ptr, &info_ptr);
    466     free (png_pixels);
    467     return FALSE;
    468   }
    469 
    470   /* initialize the png structure */
    471   png_init_io (png_ptr, png_file);
    472 
    473458  /* we're going to write more or less the same PNG as the input file */
    474459  png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
     
    476461                PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
    477462
     463  if (packed_bitmap == TRUE)
     464  {
     465    png_set_packing (png_ptr);
     466    png_set_invert_mono (png_ptr);
     467  }
     468
    478469  /* write the file header information */
    479470  png_write_info (png_ptr, info_ptr);
    480471
    481   /* if needed we will allocate memory for an new array of row-pointers */
    482   if (row_pointers == NULL)
    483   {
    484     if ((row_pointers = (png_byte **)
    485          malloc (height * sizeof (png_byte *))) == NULL)
    486     {
    487       png_destroy_write_struct (&png_ptr, &info_ptr);
    488       free (png_pixels);
    489       return FALSE;
    490     }
    491   }
    492 
    493   /* set the individual row_pointers to point at the correct offsets */
    494   for (i = 0; i < (int) height; i++)
    495     row_pointers[i] = png_pixels + i * row_bytes;
    496 
    497472  /* write out the entire image data in one call */
    498473  png_write_image (png_ptr, row_pointers);
     
    501476  png_write_end (png_ptr, info_ptr);
    502477
    503   /* clean up after the write, and free any memory allocated */
    504   png_destroy_write_struct (&png_ptr, &info_ptr);
    505 
    506   if (row_pointers != NULL)
    507     free (row_pointers);
    508   if (png_pixels != NULL)
    509     free (png_pixels);
    510 
    511478  return TRUE;
    512479} /* end of pnm2png */
    513480
    514481/*
    515  * get_token - gets the first string after whitespace
    516  */
    517 
    518 void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size)
     482 * fscan_pnm_magic - like fscan_pnm_token below, but expects the magic string
     483 *                   to start immediately, without any comment or whitespace,
     484 *                   and to match the regex /^P[1-9]$/
     485 */
     486
     487int fscan_pnm_magic (FILE *pnm_file, char *magic_buf, size_t magic_buf_size)
     488{
     489  int ret;
     490
     491  ret = fgetc (pnm_file);
     492  if (ret == EOF) return 0;
     493  ungetc (ret, pnm_file);
     494  if (ret != 'P') return 0;
     495
     496  /* the string buffer must be at least four bytes long, i.e., the capacity
     497   * required for strings of at least three characters long, i.e., the minimum
     498   * required for ensuring that our magic string is exactly two characters long
     499   */
     500  if (magic_buf_size < 4) return -1;
     501
     502  ret = fscan_pnm_token (pnm_file, magic_buf, magic_buf_size);
     503  if (ret < 1) return ret;
     504
     505  if ((magic_buf[1] < '1') || (magic_buf[1] > '9')) return 0;
     506  if (magic_buf[2] != '\0') return 0;
     507
     508  return 1;
     509}
     510
     511/*
     512 * fscan_pnm_token - extracts the first string token after whitespace,
     513 *                   and (like fscanf) returns the number of successful
     514 *                   extractions, which can be either 0 or 1
     515 */
     516
     517int fscan_pnm_token (FILE *pnm_file, char *token_buf, size_t token_buf_size)
    519518{
    520519  size_t i = 0;
     
    544543    ret = fgetc (pnm_file);
    545544    if (ret == EOF) break;
     545    if (ret == '0')
     546    {
     547      /* avoid storing more than one leading '0' in the token buffer,
     548       * to ensure that all valid (in-range) numeric inputs can fit in. */
     549      if ((i == 0) && (token_buf[i] == '0')) continue;
     550    }
    546551    if (++i == token_buf_size - 1) break;
    547552    token_buf[i] = (char) ret;
     
    550555
    551556  token_buf[i] = '\0';
    552 
    553   return;
    554 }
    555 
    556 /*
    557  *  get_data - takes first byte and converts into next pixel value,
    558  *             taking as much bits as defined by bit-depth and
    559  *             using the bit-depth to fill up a byte (0Ah -> AAh)
    560  */
    561 
    562 png_uint_32 get_data (FILE *pnm_file, int depth)
     557  return (i > 0) ? 1 : 0;
     558}
     559
     560/*
     561 * fscan_pnm_uint_32 - like fscan_token above, but expects the extracted token
     562 *                     to be numeric, and converts it to an unsigned 32-bit int
     563 */
     564
     565int fscan_pnm_uint_32 (FILE *pnm_file, png_uint_32 *num_ptr)
     566{
     567  char token[16];
     568  unsigned long token_value;
     569  int ret;
     570
     571  ret = fscan_pnm_token (pnm_file, token, sizeof (token));
     572  if (ret < 1) return ret;
     573
     574  if ((token[0] < '0') && (token[0] > '9'))
     575    return 0; /* the token starts with junk, or a +/- sign, which is invalid */
     576
     577  ret = sscanf (token, "%lu%*c", &token_value);
     578  if (ret != 1)
     579    return 0; /* the token ends with junk */
     580
     581  *num_ptr = (png_uint_32) token_value;
     582
     583#if ULONG_MAX > 0xFFFFFFFFUL
     584  /* saturate the converted number, following the fscanf convention */
     585  if (token_value > 0xFFFFFFFFUL)
     586    *num_ptr = 0xFFFFFFFFUL;
     587#endif
     588
     589  return 1;
     590}
     591
     592/*
     593 *  get_pnm_data - takes first byte and converts into next pixel value,
     594 *                 taking as many bits as defined by bit-depth and
     595 *                 using the bit-depth to fill up a byte (0x0A -> 0xAA)
     596 */
     597
     598png_uint_32 get_pnm_data (FILE *pnm_file, int depth)
    563599{
    564600  static int bits_left = 0;
    565601  static int old_value = 0;
    566602  static int mask = 0;
     603  png_uint_32 ret_value;
    567604  int i;
    568   png_uint_32 ret_value;
    569605
    570606  if (mask == 0)
     
    574610  if (bits_left <= 0)
    575611  {
     612    /* FIXME:
     613     * signal the premature end of file, instead of pretending to read zeroes
     614     */
    576615    old_value = fgetc (pnm_file);
     616    if (old_value == EOF) return 0;
    577617    bits_left = 8;
    578618  }
     
    589629
    590630/*
    591  *  get_value - takes first (numeric) string and converts into number,
    592  *              using the bit-depth to fill up a byte (0Ah -> AAh)
    593  */
    594 
    595 png_uint_32 get_value (FILE *pnm_file, int depth)
     631 *  get_pnm_value - takes first (numeric) string and converts into number,
     632 *                  using the bit-depth to fill up a byte (0x0A -> 0xAA)
     633 */
     634
     635png_uint_32 get_pnm_value (FILE *pnm_file, int depth)
    596636{
    597637  static png_uint_32 mask = 0;
    598   char token[16];
    599   unsigned long ul_ret_value;
    600638  png_uint_32 ret_value;
    601   int i = 0;
     639  int i;
    602640
    603641  if (mask == 0)
     
    605643      mask = (mask << 1) | 0x01;
    606644
    607   get_token (pnm_file, token, sizeof (token));
    608   sscanf (token, "%lu", &ul_ret_value);
    609   ret_value = (png_uint_32) ul_ret_value;
     645  if (fscan_pnm_uint_32 (pnm_file, &ret_value) != 1)
     646  {
     647    /* FIXME:
     648     * signal the invalid numeric tokens or the premature end of file,
     649     * instead of pretending to read zeroes
     650     */
     651    return 0;
     652  }
    610653
    611654  ret_value &= mask;
  • trunk/src/libs/libpng-1.6.42/contrib/powerpc-vsx/linux.c

    r96425 r103316  
    33 * Copyright (c) 2017 Glenn Randers-Pehrson
    44 * Written by Vadim Barkov, 2017.
    5  * Last changed in libpng 1.6.29 [March 16, 2017]
    65 *
    76 * This code is released under the libpng license.
     
    1312 *
    1413 * png_have_vsx implemented for Linux by reading the widely available
    15  * pseudo-file /proc/cpuinfo. 
     14 * pseudo-file /proc/cpuinfo.
    1615 *
    1716 * This code is strict ANSI-C and is probably moderately portable; it does
  • trunk/src/libs/libpng-1.6.42/contrib/powerpc-vsx/linux_aux.c

    r96425 r103316  
    33 * Copyright (c) 2017 Glenn Randers-Pehrson
    44 * Written by Vadim Barkov, 2017.
    5  * Last changed in libpng 1.6.29 [March 16, 2017]
    65 *
    76 * This code is released under the libpng license.
  • trunk/src/libs/libpng-1.6.42/contrib/testpngs/makepngs.sh

    r96425 r103316  
    1 #!/bin/sh
    2 #
     1#!/usr/bin/env bash
     2
    33# Make a set of test PNG files, MAKEPNG is the name of the makepng executable
    44# built from contrib/libtests/makepng.c
    55
    66# Copyright (c) 2015 John Cunningham Bowler
    7 
    8 # Last changed in libpng 1.6.20 [December 3, 2015]
    97
    108# This code is released under the libpng license.
  • trunk/src/libs/libpng-1.6.42/contrib/tools/checksum-icc.c

    r96425 r103316  
    22 *
    33 * Copyright (c) 2013 John Cunningham Bowler
    4  *
    5  * Last changed in libpng 1.6.0 [February 14, 2013]
    64 *
    75 * This code is released under the libpng license.
     
    1210 * generate check-codes for use when matching ICC profiles within libpng.
    1311 */
     12
    1413#include <stdio.h>
    15 
    1614#include <zlib.h>
    1715
  • trunk/src/libs/libpng-1.6.42/contrib/tools/cvtcolor.c

    r96425 r103316  
    1 /*-
    2  * convert.c
    3  *
    4  * Last changed in libpng 1.6.0 [February 14, 2013]
     1/* convert.c
    52 *
    63 * COPYRIGHT: Written by John Cunningham Bowler, 2013.
     
    118 * Convert 8-bit sRGB or 16-bit linear values to another format.
    129 */
     10
    1311#define _ISOC99_SOURCE 1
    1412
  • trunk/src/libs/libpng-1.6.42/contrib/tools/genpng.c

    r96425 r103316  
    1 /*- genpng
     1/* genpng
    22 *
    33 * COPYRIGHT: Written by John Cunningham Bowler, 2015.
     
    7070 * intersection.
    7171 */
     72
    7273#include <stddef.h>
    7374#include <stdlib.h>
     
    454455 * { inside_circle, check_circle }
    455456 *
    456  * The functions here are analoguous to the square ones; however, they check
     457 * The functions here are analogous to the square ones; however, they check
    457458 * the corresponding ellipse as opposed to the rectangle.
    458459 */
  • trunk/src/libs/libpng-1.6.42/contrib/tools/intgamma.sh

    r96425 r103316  
    11#!/bin/sh
    2 #
     2
    33# intgamma.sh
    4 #
    5 # Last changed in libpng 1.6.0 [February 14, 2013]
    64#
    75# COPYRIGHT: Written by John Cunningham Bowler, 2013.
    86# To the extent possible under law, the author has waived all copyright and
    9 # related or neighboring rights to this work.  This work is published from:
    10 # United States.
     7# related or neighboring rights to this work.  The author published this work
     8# from the United States.
    119#
    1210# Shell script to generate png.c 8-bit and 16-bit log tables (see the code in
     
    1816#
    1917# "bc" must be on the path when the script is executed, and the math library
    20 # (-lm) must be available
    21 #
    22 # function to print out a list of numbers as integers; the function truncates
    23 # the integers which must be one-per-line
    24 function print(){
     18# (-lm) must be available.
     19
     20# Function to print out a list of numbers as integers; the function truncates
     21# the integers which must be one-per-line.
     22print(){
    2523   awk 'BEGIN{
    2624      str = ""
  • trunk/src/libs/libpng-1.6.42/contrib/tools/makesRGB.c

    r96425 r103316  
    11/* makesRGB.c -- build sRGB-to-linear and linear-to-sRGB conversion tables
    2  *
    3  * Last changed in libpng 1.6.0 [February 14, 2013]
    42 *
    53 * COPYRIGHT: Written by John Cunningham Bowler, 2013.
     
    1513 * tables and display it.
    1614 */
     15
    1716#define _C99_SOURCE 1
    1817#include <stdio.h>
  • trunk/src/libs/libpng-1.6.42/contrib/tools/png-fix-itxt.c

    r96425 r103316  
    1 
    2 /* png-fix-itxt version 1.0.0
     1/* png-fix-itxt
    32 *
    43 * Copyright 2015 Glenn Randers-Pehrson
    5  * Last changed in libpng 1.6.18 [July 23, 2015]
    64 *
    75 * This code is released under the libpng license.
     
    119 * Usage:
    1210 *
    13  *     png-fix-itxt.exe < bad.png > good.png
     11 *     png-fix-itxt < bad.png > good.png
    1412 *
    1513 * Fixes a PNG file written with libpng-1.6.0 or 1.6.1 that has one or more
  • trunk/src/libs/libpng-1.6.42/contrib/tools/pngcp.c

    r96425 r103316  
    11/* pngcp.c
    22 *
    3  * Copyright (c) 2016 John Cunningham Bowler
    4  *
    5  * Last changed in libpng 1.6.24 [August 4, 2016]
     3 * Copyright (c) 2016,2022,2024 John Cunningham Bowler
    64 *
    75 * This code is released under the libpng license.
     
    1513 * For a more extensive example that uses the transforms see
    1614 * contrib/libtests/pngimage.c in the libpng distribution.
     15 *
     16 * This code is not intended for installation in a release system; the command
     17 * line options are not documented and most of the behavior is intended for
     18 * testing libpng performance, both speed and compression.
    1719 */
     20
    1821#include "pnglibconf.h" /* To find how libpng was configured. */
    1922
     
    8588#  define voidcast(type, value) (value)
    8689#endif /* __cplusplus */
    87 
    88 #ifdef __GNUC__
    89    /* Many versions of GCC erroneously report that local variables unmodified
    90     * within the scope of a setjmp may be clobbered.  This hacks round the
    91     * problem (sometimes) without harming other compilers.
    92     */
    93 #  define gv volatile
    94 #else
    95 #  define gv
    96 #endif
    9790
    9891/* 'CLOCK_PROCESS_CPUTIME_ID' is one of the clock timers for clock_gettime.  It
     
    337330
    338331#  ifdef PNG_SW_COMPRESS_png_level
    339       /* The libpng compression level isn't searched because it justs sets the
     332      /* The libpng compression level isn't searched because it just sets the
    340333       * other things that are searched!
    341334       */
     
    393386   jmp_buf          error_return;      /* Where to go to on error */
    394387   unsigned int     errset;            /* error_return is set */
     388   int              errlevel;          /* error level from longjmp */
    395389
    396390   const char      *operation;         /* What is happening */
     
    504498
    505499static void
    506 display_clean_read(struct display *dp)
     500display_clean_read(struct display *dp, int freeinfo)
    507501{
    508502   if (dp->read_pp != NULL)
    509       png_destroy_read_struct(&dp->read_pp, NULL, NULL);
     503      png_destroy_read_struct(&dp->read_pp, freeinfo ? &dp->ip : NULL, NULL);
    510504
    511505   if (dp->fp != NULL)
     
    518512
    519513static void
    520 display_clean_write(struct display *dp)
     514display_clean_write(struct display *dp, int freeinfo)
    521515{
    522516   if (dp->fp != NULL)
     
    528522
    529523   if (dp->write_pp != NULL)
    530       png_destroy_write_struct(&dp->write_pp, dp->tsp > 0 ? NULL : &dp->ip);
     524      png_destroy_write_struct(&dp->write_pp, freeinfo ? &dp->ip : NULL);
    531525}
    532526
     
    534528display_clean(struct display *dp)
    535529{
    536    display_clean_read(dp);
    537    display_clean_write(dp);
     530   display_clean_read(dp, 1/*freeinfo*/);
     531   display_clean_write(dp, 1/*freeinfo*/);
    538532   dp->output_file = NULL;
    539533
     
    632626   {
    633627      if (dp->errset)
     628      {
     629         dp->errlevel = level;
    634630         longjmp(dp->error_return, level);
     631      }
    635632
    636633      else
     
    779776   /* Add the appropriate option string to dp->curr. */
    780777{
    781    dp->stack[sp].opt_string_end = set_opt_string_(dp, sp, dp->stack[sp].opt, 
     778   dp->stack[sp].opt_string_end = set_opt_string_(dp, sp, dp->stack[sp].opt,
    782779      options[dp->stack[sp].opt].values[dp->stack[sp].entry].name);
    783780}
     
    17461743read_png(struct display *dp, const char *filename)
    17471744{
    1748    display_clean_read(dp); /* safety */
     1745   /* This is an assumption of the code; it may happen if a previous write fails
     1746    * and there is a bug in the cleanup handling below (look for setjmp).
     1747    * Passing freeinfo==1 to display_clean_read below avoids a second error
     1748    * on dp->ip != NULL below.
     1749    */
     1750   if (dp->read_pp != NULL)
     1751   {
     1752      display_log(dp, APP_FAIL, "unexpected png_read_struct");
     1753      display_clean_read(dp, 1/*freeinfo*/); /* recovery */
     1754   }
     1755
    17491756   display_start_read(dp, filename);
    17501757
     
    17701777#  endif /* IGNORE_INDEX */
    17711778
     1779   if (dp->ip != NULL)
     1780   {
     1781      /* UNEXPECTED: some problem in the display_clean function calls! */
     1782      display_log(dp, APP_FAIL, "read_png: freeing old info struct");
     1783      png_destroy_info_struct(dp->read_pp, &dp->ip);
     1784   }
     1785
    17721786   /* The png_read_png API requires us to make the info struct, but it does the
    17731787    * call to png_read_info.
     
    18491863#endif /* FIX_INDEX */
    18501864
    1851    display_clean_read(dp);
     1865   /* NOTE: dp->ip is where all the information about the PNG that was just read
     1866    * is stored.  It can be used to write and write again a single PNG file,
     1867    * however be aware that prior to libpng 1.7 text chunks could only be
     1868    * written once; this is a bug which would require a significant code rewrite
     1869    * to fix, it has been there in several versions of libpng (it was introduced
     1870    * to fix another bug involving duplicate writes of the text chunks.)
     1871    */
     1872   display_clean_read(dp, 0/*freeiinfo*/);
    18521873   dp->operation = "none";
    18531874}
     
    19761997write_png(struct display *dp, const char *destname)
    19771998{
    1978    display_clean_write(dp); /* safety */
     1999   /* If this test fails png_write_png would fail *silently* below; this
     2000    * is not helpful, so catch the problem now and give up:
     2001    */
     2002   if (dp->ip == NULL)
     2003      display_log(dp, INTERNAL_ERROR, "missing png_info");
     2004
     2005   /* This is an assumption of the code; it may happen if a previous
     2006    * write fails and there is a bug in the cleanup handling below.
     2007    */
     2008   if (dp->write_pp != NULL)
     2009   {
     2010      display_log(dp, APP_FAIL, "unexpected png_write_struct");
     2011      display_clean_write(dp, 0/*!freeinfo*/);
     2012   }
     2013
    19792014   display_start_write(dp, destname);
    19802015
     
    20742109   }
    20752110
    2076    /* Clean it on the way out - if control returns to the caller then the
    2077     * written_file contains the required data.
    2078     */
    2079    display_clean_write(dp);
    20802111   dp->operation = "none";
    20812112}
     
    22442275      do
    22452276      {
     2277         /* Clean before each write_png; this just removes *dp->write_pp which
     2278          * cannot be reused.
     2279          */
     2280         display_clean_write(dp, 0/*!freeinfo*/);
    22462281         write_png(dp, tmpname);
    22472282
     
    22732308      dp->write_size = dp->best_size;
    22742309   }
     2310
     2311   display_clean_write(dp, 1/*freeinfo*/);
    22752312}
    22762313
    22772314static int
    2278 cppng(struct display *dp, const char *file, const char *gv dest)
    2279    /* Exists solely to isolate the setjmp clobbers which some versions of GCC
    2280     * erroneously generate.
    2281     */
    2282 {
    2283    int ret = setjmp(dp->error_return);
    2284 
    2285    if (ret == 0)
     2315cppng(struct display *dp, const char *file, const char *dest)
     2316{
     2317   if (setjmp(dp->error_return) == 0)
    22862318   {
    22872319      dp->errset = 1;
     
    22952327      dp->errset = 0;
    22962328
    2297       if (ret < ERRORS) /* shouldn't longjmp on warnings */
    2298          display_log(dp, INTERNAL_ERROR, "unexpected return code %d", ret);
    2299 
    2300       return ret;
     2329      if (dp->errlevel < ERRORS) /* shouldn't longjmp on warnings */
     2330         display_log(dp, INTERNAL_ERROR, "unexpected return code %d",
     2331               dp->errlevel);
     2332
     2333      return dp->errlevel;
    23012334   }
    23022335}
  • trunk/src/libs/libpng-1.6.42/contrib/tools/pngfix.c

    r96425 r103316  
    11/* pngfix.c
    22 *
    3  * Last changed in libpng 1.6.31 [July 27, 2017]
    4  * Copyright (c) 2014-2017 John Cunningham Bowler
     3 * Copyright (c) 2014-2017,2024 John Cunningham Bowler
    54 *
    65 * This code is released under the libpng license.
     
    1110 * See the usage message for more information.
    1211 */
     12
    1313#include <stdlib.h>
    1414#include <stdio.h>
     
    2020
    2121#define implies(x,y) assert(!(x) || (y))
    22 
    23 #ifdef __GNUC__
    24    /* This is used to fix the error:
    25     *
    26     * pngfix.c:
    27     * In function 'zlib_advance':
    28     * pngfix.c:181:13: error: assuming signed overflow does not
    29     *   occur when simplifying conditional to constant [-Werror=strict-overflow]
    30     */
    31 #  define FIX_GCC volatile
    32 #else
    33 #  define FIX_GCC
    34 #endif
    3522
    3623#define PROGRAM_NAME "pngfix"
     
    219206    */
    220207{
    221    FIX_GCC int out_digits = 0;
     208   int out_digits = 0;
    222209
    223210   while (out_digits < in_digits)
     
    264251
    265252static int
    266 uarb_mult_digit(uarb acc, int a_digits, uarb num, FIX_GCC int n_digits,
     253uarb_mult_digit(uarb acc, int a_digits, uarb num, int n_digits,
    267254   png_uint_16 val)
    268255   /* Primitive one-digit multiply - 'val' must be 0..65535. Note that this
     
    337324    */
    338325{
    339    FIX_GCC int i = ndigits;
     326   int i = ndigits;
    340327   png_uint_16 carry = 0;
    341328
     
    352339
    353340      /* The shift may reduce ndigits */
    354       if (i == ndigits-1 && temp == 0)
     341      if (i+1 == ndigits && temp == 0)
    355342         ndigits = i;
    356343   }
     
    868855    *
    869856    * When a chunk control structure is instantiated these values are copied
    870     * into the structure and can then be overritten with the data for the next
     857    * into the structure and can then be overwritten with the data for the next
    871858    * chunk.
    872859    */
     
    1006993   if (file->out != NULL)
    1007994   {
    1008       /* NOTE: this is bitwise |, all the following functions must execute and
    1009        * must succeed.
     995      /* On some systems 'fclose' deletes the FILE struct (making it
     996       * inaccessbile).  There is no guarantee that fclose returns an error
     997       * code from fflush or, indeed, from the FILE error indicator.  There is
     998       * also no explicit (or clear) guarantee in the standard that anything
     999       * other than a read or write operation sets the error indicator; fflush
     1000       * is not a read or write operation, so both conditions must be checked
     1001       * to ensure the close succeeded and in ANSI-C conformant code they must
     1002       * be checked before the fclose call.
    10101003       */
    1011       if (ferror(file->out) | fflush(file->out) | fclose(file->out))
     1004      const int err = fflush(file->out) || ferror(file->out);
     1005
     1006      if (fclose(file->out) || err)
    10121007      {
    10131008         perror(file->out_name);
     
    15991594   chunk->chunk_type = file->type;
    16001595
    1601    /* Compresssed/uncompressed size information (from the zlib control structure
     1596   /* Compressed/uncompressed size information (from the zlib control structure
    16021597    * that is used to check the compressed data in a chunk.)
    16031598    */
     
    29302925
    29312926   /* Control reaches this point if the chunk must be skipped.  For chunks other
    2932     * than IDAT this means that the zlib compressed data is fatally damanged and
     2927    * than IDAT this means that the zlib compressed data is fatally damaged and
    29332928    * the chunk will not be passed to libpng.  For IDAT it means that the end of
    29342929    * the IDAT stream has not yet been reached and we must handle the next
     
    39623957         size_t outlen = strlen(*argv);
    39633958
     3959         if (outlen > FILENAME_MAX)
     3960         {
     3961            fprintf(stderr, "%s: output file name too long: %s%s%s\n",
     3962               prog, prefix, *argv, suffix ? suffix : "");
     3963            global.status_code |= WRITE_ERROR;
     3964            continue;
     3965         }
     3966
    39643967         if (outfile == NULL) /* else this takes precedence */
    39653968         {
     
    40474050}
    40484051#endif /* PNG_SETJMP_SUPPORTED */
    4049 
     4052/* vi: set textwidth=80 shiftwidth=3 softtabstop=-1 expandtab: */
  • trunk/src/libs/libpng-1.6.42/contrib/tools/sRGB.h

    r96425 r103316  
    1 /*-
    2  * sRGB.h
    3  *
    4  * Last changed in libpng 1.6.0 [February 14, 2013]
     1/* sRGB.h
    52 *
    63 * COPYRIGHT: Written by John Cunningham Bowler, 2013.
     
    1714 * https://en.wikipedia.org/wiki/SRGB).
    1815 */
     16
    1917static double
    2018sRGB_from_linear(double l)
  • trunk/src/libs/libpng-1.6.42/contrib/visupng/VisualPng.c

    r96425 r103316  
    104104    }
    105105
    106     /* if filename given on commandline, store it */
     106    /* if filename given on command line, store it */
    107107    if ((szCmdLine != NULL) && (*szCmdLine != '\0'))
    108108        if (szCmdLine[0] == '"')
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette