< prev index next >

src/java.desktop/share/native/libsplashscreen/libpng/pngtrans.c

Print this page




  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* pngtrans.c - transforms the data in a row (used by both readers and writers)
  26  *
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file and, per its terms, should not be removed:
  31  *
  32  * Last changed in libpng 1.6.26 [October 20, 2016]
  33  * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
  34  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  35  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  36  *
  37  * This code is released under the libpng license.
  38  * For conditions of distribution and use, see the disclaimer
  39  * and license in png.h
  40  */
  41 
  42 #include "pngpriv.h"
  43 
  44 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  45 
  46 #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  47 /* Turn on BGR-to-RGB mapping */
  48 void PNGAPI
  49 png_set_bgr(png_structrp png_ptr)
  50 {
  51    png_debug(1, "in png_set_bgr");
  52 
  53    if (png_ptr == NULL)


 525    png_bytep sp = row; /* source pointer */
 526    png_bytep dp = row; /* destination pointer */
 527    png_bytep ep = row + row_info->rowbytes; /* One beyond end of row */
 528 
 529    /* At the start sp will point to the first byte to copy and dp to where
 530     * it is copied to.  ep always points just beyond the end of the row, so
 531     * the loop simply copies (channels-1) channels until sp reaches ep.
 532     *
 533     * at_start:        0 -- convert AG, XG, ARGB, XRGB, AAGG, XXGG, etc.
 534     *            nonzero -- convert GA, GX, RGBA, RGBX, GGAA, RRGGBBXX, etc.
 535     */
 536 
 537    /* GA, GX, XG cases */
 538    if (row_info->channels == 2)
 539    {
 540       if (row_info->bit_depth == 8)
 541       {
 542          if (at_start != 0) /* Skip initial filler */
 543             ++sp;
 544          else          /* Skip initial channel and, for sp, the filler */
 545             sp += 2, ++dp;


 546 
 547          /* For a 1 pixel wide image there is nothing to do */
 548          while (sp < ep)
 549             *dp++ = *sp, sp += 2;


 550 
 551          row_info->pixel_depth = 8;
 552       }
 553 
 554       else if (row_info->bit_depth == 16)
 555       {
 556          if (at_start != 0) /* Skip initial filler */
 557             sp += 2;
 558          else          /* Skip initial channel and, for sp, the filler */
 559             sp += 4, dp += 2;


 560 
 561          while (sp < ep)
 562             *dp++ = *sp++, *dp++ = *sp, sp += 3;


 563 
 564          row_info->pixel_depth = 16;
 565       }
 566 
 567       else
 568          return; /* bad bit depth */
 569 
 570       row_info->channels = 1;
 571 
 572       /* Finally fix the color type if it records an alpha channel */
 573       if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 574          row_info->color_type = PNG_COLOR_TYPE_GRAY;
 575    }
 576 
 577    /* RGBA, RGBX, XRGB cases */
 578    else if (row_info->channels == 4)
 579    {
 580       if (row_info->bit_depth == 8)
 581       {
 582          if (at_start != 0) /* Skip initial filler */
 583             ++sp;
 584          else          /* Skip initial channels and, for sp, the filler */
 585             sp += 4, dp += 3;


 586 
 587          /* Note that the loop adds 3 to dp and 4 to sp each time. */
 588          while (sp < ep)
 589             *dp++ = *sp++, *dp++ = *sp++, *dp++ = *sp, sp += 2;


 590 
 591          row_info->pixel_depth = 24;
 592       }
 593 
 594       else if (row_info->bit_depth == 16)
 595       {
 596          if (at_start != 0) /* Skip initial filler */
 597             sp += 2;
 598          else          /* Skip initial channels and, for sp, the filler */
 599             sp += 8, dp += 6;


 600 
 601          while (sp < ep)
 602          {
 603             /* Copy 6 bytes, skip 2 */
 604             *dp++ = *sp++, *dp++ = *sp++;
 605             *dp++ = *sp++, *dp++ = *sp++;
 606             *dp++ = *sp++, *dp++ = *sp, sp += 3;
 607          }
 608 
 609          row_info->pixel_depth = 48;
 610       }
 611 
 612       else
 613          return; /* bad bit depth */
 614 
 615       row_info->channels = 3;
 616 
 617       /* Finally fix the color type if it records an alpha channel */
 618       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 619          row_info->color_type = PNG_COLOR_TYPE_RGB;
 620    }
 621 
 622    else
 623       return; /* The filler channel has gone already */
 624 
 625    /* Fix the rowbytes value. */
 626    row_info->rowbytes = (unsigned int)(dp-row);
 627 }
 628 #endif
 629 
 630 #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
 631 /* Swaps red and blue bytes within a pixel */
 632 void /* PRIVATE */
 633 png_do_bgr(png_row_infop row_info, png_bytep row)
 634 {
 635    png_debug(1, "in png_do_bgr");
 636 
 637    if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
 638    {
 639       png_uint_32 row_width = row_info->width;
 640       if (row_info->bit_depth == 8)
 641       {
 642          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 643          {
 644             png_bytep rp;
 645             png_uint_32 i;
 646 


 705    }
 706 }
 707 #endif /* READ_BGR || WRITE_BGR */
 708 
 709 #if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \
 710     defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED)
 711 /* Added at libpng-1.5.10 */
 712 void /* PRIVATE */
 713 png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info)
 714 {
 715    if (png_ptr->num_palette < (1 << row_info->bit_depth) &&
 716       png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */
 717    {
 718       /* Calculations moved outside switch in an attempt to stop different
 719        * compiler warnings.  'padding' is in *bits* within the last byte, it is
 720        * an 'int' because pixel_depth becomes an 'int' in the expression below,
 721        * and this calculation is used because it avoids warnings that other
 722        * forms produced on either GCC or MSVC.
 723        */
 724       int padding = PNG_PADBITS(row_info->pixel_depth, row_info->width);
 725       png_bytep rp = png_ptr->row_buf + row_info->rowbytes;
 726 
 727       switch (row_info->bit_depth)
 728       {
 729          case 1:
 730          {
 731             /* in this case, all bytes must be 0 so we don't need
 732              * to unpack the pixels except for the rightmost one.
 733              */
 734             for (; rp > png_ptr->row_buf; rp--)
 735             {
 736               if ((*rp >> padding) != 0)
 737                  png_ptr->num_palette_max = 1;
 738               padding = 0;
 739             }
 740 
 741             break;
 742          }
 743 
 744          case 2:
 745          {




  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /* pngtrans.c - transforms the data in a row (used by both readers and writers)
  26  *
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file and, per its terms, should not be removed:
  31  *
  32  * Last changed in libpng 1.6.33 [September 28, 2017]
  33  * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson
  34  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  35  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  36  *
  37  * This code is released under the libpng license.
  38  * For conditions of distribution and use, see the disclaimer
  39  * and license in png.h
  40  */
  41 
  42 #include "pngpriv.h"
  43 
  44 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
  45 
  46 #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  47 /* Turn on BGR-to-RGB mapping */
  48 void PNGAPI
  49 png_set_bgr(png_structrp png_ptr)
  50 {
  51    png_debug(1, "in png_set_bgr");
  52 
  53    if (png_ptr == NULL)


 525    png_bytep sp = row; /* source pointer */
 526    png_bytep dp = row; /* destination pointer */
 527    png_bytep ep = row + row_info->rowbytes; /* One beyond end of row */
 528 
 529    /* At the start sp will point to the first byte to copy and dp to where
 530     * it is copied to.  ep always points just beyond the end of the row, so
 531     * the loop simply copies (channels-1) channels until sp reaches ep.
 532     *
 533     * at_start:        0 -- convert AG, XG, ARGB, XRGB, AAGG, XXGG, etc.
 534     *            nonzero -- convert GA, GX, RGBA, RGBX, GGAA, RRGGBBXX, etc.
 535     */
 536 
 537    /* GA, GX, XG cases */
 538    if (row_info->channels == 2)
 539    {
 540       if (row_info->bit_depth == 8)
 541       {
 542          if (at_start != 0) /* Skip initial filler */
 543             ++sp;
 544          else          /* Skip initial channel and, for sp, the filler */
 545          {
 546             sp += 2; ++dp;
 547          }
 548 
 549          /* For a 1 pixel wide image there is nothing to do */
 550          while (sp < ep)
 551          {
 552             *dp++ = *sp; sp += 2;
 553          }
 554 
 555          row_info->pixel_depth = 8;
 556       }
 557 
 558       else if (row_info->bit_depth == 16)
 559       {
 560          if (at_start != 0) /* Skip initial filler */
 561             sp += 2;
 562          else          /* Skip initial channel and, for sp, the filler */
 563          {
 564             sp += 4; dp += 2;
 565          }
 566 
 567          while (sp < ep)
 568          {
 569             *dp++ = *sp++; *dp++ = *sp; sp += 3;
 570          }
 571 
 572          row_info->pixel_depth = 16;
 573       }
 574 
 575       else
 576          return; /* bad bit depth */
 577 
 578       row_info->channels = 1;
 579 
 580       /* Finally fix the color type if it records an alpha channel */
 581       if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 582          row_info->color_type = PNG_COLOR_TYPE_GRAY;
 583    }
 584 
 585    /* RGBA, RGBX, XRGB cases */
 586    else if (row_info->channels == 4)
 587    {
 588       if (row_info->bit_depth == 8)
 589       {
 590          if (at_start != 0) /* Skip initial filler */
 591             ++sp;
 592          else          /* Skip initial channels and, for sp, the filler */
 593          {
 594             sp += 4; dp += 3;
 595          }
 596 
 597          /* Note that the loop adds 3 to dp and 4 to sp each time. */
 598          while (sp < ep)
 599          {
 600             *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp; sp += 2;
 601          }
 602 
 603          row_info->pixel_depth = 24;
 604       }
 605 
 606       else if (row_info->bit_depth == 16)
 607       {
 608          if (at_start != 0) /* Skip initial filler */
 609             sp += 2;
 610          else          /* Skip initial channels and, for sp, the filler */
 611          {
 612             sp += 8; dp += 6;
 613          }
 614 
 615          while (sp < ep)
 616          {
 617             /* Copy 6 bytes, skip 2 */
 618             *dp++ = *sp++; *dp++ = *sp++;
 619             *dp++ = *sp++; *dp++ = *sp++;
 620             *dp++ = *sp++; *dp++ = *sp; sp += 3;
 621          }
 622 
 623          row_info->pixel_depth = 48;
 624       }
 625 
 626       else
 627          return; /* bad bit depth */
 628 
 629       row_info->channels = 3;
 630 
 631       /* Finally fix the color type if it records an alpha channel */
 632       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 633          row_info->color_type = PNG_COLOR_TYPE_RGB;
 634    }
 635 
 636    else
 637       return; /* The filler channel has gone already */
 638 
 639    /* Fix the rowbytes value. */
 640    row_info->rowbytes = (png_size_t)(dp-row);
 641 }
 642 #endif
 643 
 644 #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
 645 /* Swaps red and blue bytes within a pixel */
 646 void /* PRIVATE */
 647 png_do_bgr(png_row_infop row_info, png_bytep row)
 648 {
 649    png_debug(1, "in png_do_bgr");
 650 
 651    if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
 652    {
 653       png_uint_32 row_width = row_info->width;
 654       if (row_info->bit_depth == 8)
 655       {
 656          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
 657          {
 658             png_bytep rp;
 659             png_uint_32 i;
 660 


 719    }
 720 }
 721 #endif /* READ_BGR || WRITE_BGR */
 722 
 723 #if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \
 724     defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED)
 725 /* Added at libpng-1.5.10 */
 726 void /* PRIVATE */
 727 png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info)
 728 {
 729    if (png_ptr->num_palette < (1 << row_info->bit_depth) &&
 730       png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */
 731    {
 732       /* Calculations moved outside switch in an attempt to stop different
 733        * compiler warnings.  'padding' is in *bits* within the last byte, it is
 734        * an 'int' because pixel_depth becomes an 'int' in the expression below,
 735        * and this calculation is used because it avoids warnings that other
 736        * forms produced on either GCC or MSVC.
 737        */
 738       int padding = PNG_PADBITS(row_info->pixel_depth, row_info->width);
 739       png_bytep rp = png_ptr->row_buf + row_info->rowbytes - 1;
 740 
 741       switch (row_info->bit_depth)
 742       {
 743          case 1:
 744          {
 745             /* in this case, all bytes must be 0 so we don't need
 746              * to unpack the pixels except for the rightmost one.
 747              */
 748             for (; rp > png_ptr->row_buf; rp--)
 749             {
 750               if ((*rp >> padding) != 0)
 751                  png_ptr->num_palette_max = 1;
 752               padding = 0;
 753             }
 754 
 755             break;
 756          }
 757 
 758          case 2:
 759          {


< prev index next >