Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 354985
Collapse All | Expand All

(-)a/plug-ins/common/file-png.c (-55 / +84 lines)
Lines 652-658 on_read_error (png_structp png_ptr, png_const_charp error_msg) Link Here
652
                                error_data->drawable->width, num);
652
                                error_data->drawable->width, num);
653
    }
653
    }
654
654
655
#if (PNG_LIBPNG_VER < 10500)
655
  longjmp (png_ptr->jmpbuf, 1);
656
  longjmp (png_ptr->jmpbuf, 1);
657
#else
658
  png_longjmp (png_ptr, 1);
659
#endif
656
}
660
}
657
661
658
/*
662
/*
Lines 696-702 load_image (const gchar *filename, Link Here
696
  pp = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
700
  pp = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
697
  info = png_create_info_struct (pp);
701
  info = png_create_info_struct (pp);
698
702
699
  if (setjmp (pp->jmpbuf))
703
  if (setjmp (png_jmpbuf(pp)))
700
    {
704
    {
701
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
705
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
702
                   _("Error while reading '%s'. File corrupted?"),
706
                   _("Error while reading '%s'. File corrupted?"),
Lines 737-753 load_image (const gchar *filename, Link Here
737
   * Latest attempt, this should be my best yet :)
741
   * Latest attempt, this should be my best yet :)
738
   */
742
   */
739
743
740
  if (info->bit_depth == 16)
744
  if (png_get_bit_depth(pp, info) == 16)
741
    {
745
    {
742
      png_set_strip_16 (pp);
746
      png_set_strip_16 (pp);
743
    }
747
    }
744
748
745
  if (info->color_type == PNG_COLOR_TYPE_GRAY && info->bit_depth < 8)
749
  if (png_get_color_type(pp, info) == PNG_COLOR_TYPE_GRAY &&
750
      png_get_bit_depth(pp, info) < 8)
746
    {
751
    {
747
      png_set_expand (pp);
752
      png_set_expand (pp);
748
    }
753
    }
749
754
750
  if (info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8)
755
  if (png_get_color_type(pp, info) == PNG_COLOR_TYPE_PALETTE &&
756
      png_get_bit_depth(pp, info) < 8)
751
    {
757
    {
752
      png_set_packing (pp);
758
      png_set_packing (pp);
753
    }
759
    }
Lines 756-763 load_image (const gchar *filename, Link Here
756
   * Expand G+tRNS to GA, RGB+tRNS to RGBA
762
   * Expand G+tRNS to GA, RGB+tRNS to RGBA
757
   */
763
   */
758
764
759
  if (info->color_type != PNG_COLOR_TYPE_PALETTE &&
765
  if (png_get_color_type(pp, info) != PNG_COLOR_TYPE_PALETTE &&
760
      (info->valid & PNG_INFO_tRNS))
766
      png_get_valid(pp, info, PNG_INFO_tRNS) != 0)
761
    {
767
    {
762
      png_set_expand (pp);
768
      png_set_expand (pp);
763
    }
769
    }
Lines 774-780 load_image (const gchar *filename, Link Here
774
   */
780
   */
775
781
776
  if (png_get_valid (pp, info, PNG_INFO_tRNS) &&
782
  if (png_get_valid (pp, info, PNG_INFO_tRNS) &&
777
      info->color_type == PNG_COLOR_TYPE_PALETTE)
783
      png_get_color_type(pp, info) == PNG_COLOR_TYPE_PALETTE)
778
    {
784
    {
779
      png_get_tRNS (pp, info, &alpha_ptr, &num, NULL);
785
      png_get_tRNS (pp, info, &alpha_ptr, &num, NULL);
780
      /* Copy the existing alpha values from the tRNS chunk */
786
      /* Copy the existing alpha values from the tRNS chunk */
Lines 796-802 load_image (const gchar *filename, Link Here
796
802
797
  png_read_update_info (pp, info);
803
  png_read_update_info (pp, info);
798
804
799
  switch (info->color_type)
805
  switch (png_get_color_type(pp, info))
800
    {
806
    {
801
    case PNG_COLOR_TYPE_RGB:           /* RGB */
807
    case PNG_COLOR_TYPE_RGB:           /* RGB */
802
      bpp = 3;
808
      bpp = 3;
Lines 835-841 load_image (const gchar *filename, Link Here
835
      return -1;
841
      return -1;
836
    }
842
    }
837
843
838
  image = gimp_image_new (info->width, info->height, image_type);
844
  image = gimp_image_new (png_get_image_width(pp, info),
845
                          png_get_image_height(pp, info),
846
                          image_type);
839
  if (image == -1)
847
  if (image == -1)
840
    {
848
    {
841
      g_set_error (error, 0, 0,
849
      g_set_error (error, 0, 0,
Lines 848-854 load_image (const gchar *filename, Link Here
848
   * Create the "background" layer to hold the image...
856
   * Create the "background" layer to hold the image...
849
   */
857
   */
850
858
851
  layer = gimp_layer_new (image, _("Background"), info->width, info->height,
859
  layer = gimp_layer_new (image, _("Background"),
860
                          png_get_image_width(pp, info),
861
                          png_get_image_height(pp, info),
852
                          layer_type, 100, GIMP_NORMAL_MODE);
862
                          layer_type, 100, GIMP_NORMAL_MODE);
853
  gimp_image_insert_layer (image, layer, -1, 0);
863
  gimp_image_insert_layer (image, layer, -1, 0);
854
864
Lines 882-888 load_image (const gchar *filename, Link Here
882
892
883
      gimp_layer_set_offsets (layer, offset_x, offset_y);
893
      gimp_layer_set_offsets (layer, offset_x, offset_y);
884
894
885
      if ((abs (offset_x) > info->width) || (abs (offset_y) > info->height))
895
      if ((abs (offset_x) > png_get_image_width(pp, info)) ||
896
          (abs (offset_y) > png_get_image_height(pp, info)))
886
        {
897
        {
887
          if (interactive)
898
          if (interactive)
888
            g_message (_("The PNG file specifies an offset that caused "
899
            g_message (_("The PNG file specifies an offset that caused "
Lines 937-959 load_image (const gchar *filename, Link Here
937
948
938
  empty = 0; /* by default assume no full transparent palette entries */
949
  empty = 0; /* by default assume no full transparent palette entries */
939
950
940
  if (info->color_type & PNG_COLOR_MASK_PALETTE)
951
  if (png_get_color_type(pp, info) & PNG_COLOR_MASK_PALETTE)
941
    {
952
    {
953
      png_colorp palette;
954
      int num_palette;
955
      png_get_PLTE(pp, info, &palette, &num_palette);
956
942
      if (png_get_valid (pp, info, PNG_INFO_tRNS))
957
      if (png_get_valid (pp, info, PNG_INFO_tRNS))
943
        {
958
        {
944
          for (empty = 0; empty < 256 && alpha[empty] == 0; ++empty)
959
          for (empty = 0; empty < 256 && alpha[empty] == 0; ++empty)
945
            /* Calculates number of fully transparent "empty" entries */;
960
            /* Calculates number of fully transparent "empty" entries */;
946
961
947
          /*  keep at least one entry  */
962
          /*  keep at least one entry  */
948
          empty = MIN (empty, info->num_palette - 1);
963
          empty = MIN (empty, num_palette - 1);
949
964
950
          gimp_image_set_colormap (image, (guchar *) (info->palette + empty),
965
          gimp_image_set_colormap (image, (guchar *) (palette + empty),
951
                                   info->num_palette - empty);
966
                                   num_palette - empty);
952
        }
967
        }
953
      else
968
      else
954
        {
969
        {
955
          gimp_image_set_colormap (image, (guchar *) info->palette,
970
          gimp_image_set_colormap (image, (guchar *) palette,
956
                                   info->num_palette);
971
                                   num_palette);
957
        }
972
        }
958
    }
973
    }
959
974
Lines 971-988 load_image (const gchar *filename, Link Here
971
   */
986
   */
972
987
973
  tile_height = gimp_tile_height ();
988
  tile_height = gimp_tile_height ();
974
  pixel = g_new0 (guchar, tile_height * info->width * bpp);
989
  pixel = g_new0 (guchar, tile_height * png_get_image_width(pp, info) * bpp);
975
  pixels = g_new (guchar *, tile_height);
990
  pixels = g_new (guchar *, tile_height);
976
991
977
  for (i = 0; i < tile_height; i++)
992
  for (i = 0; i < tile_height; i++)
978
    pixels[i] = pixel + info->width * info->channels * i;
993
    pixels[i] = pixel + (png_get_image_width(pp, info) *
994
                         png_get_channels(pp, info) *
995
                         i);
979
996
980
  /* Install our own error handler to handle incomplete PNG files better */
997
  /* Install our own error handler to handle incomplete PNG files better */
981
  error_data.drawable    = drawable;
998
  error_data.drawable    = drawable;
982
  error_data.pixel       = pixel;
999
  error_data.pixel       = pixel;
983
  error_data.tile_height = tile_height;
1000
  error_data.tile_height = tile_height;
984
  error_data.width       = info->width;
1001
  error_data.width       = png_get_image_width(pp, info);
985
  error_data.height      = info->height;
1002
  error_data.height      = png_get_image_height(pp, info);
986
  error_data.bpp         = bpp;
1003
  error_data.bpp         = bpp;
987
  error_data.pixel_rgn   = &pixel_rgn;
1004
  error_data.pixel_rgn   = &pixel_rgn;
988
1005
Lines 995-1004 load_image (const gchar *filename, Link Here
995
       */
1012
       */
996
1013
997
      for (begin = 0, end = tile_height;
1014
      for (begin = 0, end = tile_height;
998
           begin < info->height; begin += tile_height, end += tile_height)
1015
           begin < png_get_image_height(pp, info);
1016
           begin += tile_height, end += tile_height)
999
        {
1017
        {
1000
          if (end > info->height)
1018
          if (end > png_get_image_height(pp, info))
1001
            end = info->height;
1019
            end = png_get_image_height(pp, info);
1002
1020
1003
          num = end - begin;
1021
          num = end - begin;
1004
1022
Lines 1015-1024 load_image (const gchar *filename, Link Here
1015
          gimp_pixel_rgn_set_rect (&pixel_rgn, pixel, 0, begin,
1033
          gimp_pixel_rgn_set_rect (&pixel_rgn, pixel, 0, begin,
1016
                                   drawable->width, num);
1034
                                   drawable->width, num);
1017
1035
1018
          memset (pixel, 0, tile_height * info->width * bpp);
1036
          memset (pixel, 0, tile_height * png_get_image_width(pp, info) * bpp);
1019
1037
1020
          gimp_progress_update (((gdouble) pass +
1038
          gimp_progress_update (((gdouble) pass +
1021
                                 (gdouble) end / (gdouble) info->height) /
1039
                                 (gdouble) end /
1040
                                 (gdouble) png_get_image_height(pp, info)) /
1022
                                (gdouble) num_passes);
1041
                                (gdouble) num_passes);
1023
        }
1042
        }
1024
    }
1043
    }
Lines 1071-1077 load_image (const gchar *filename, Link Here
1071
1090
1072
  {
1091
  {
1073
    png_uint_32 proflen;
1092
    png_uint_32 proflen;
1074
    png_charp   profname, profile;
1093
    png_charp   profname;
1094
    png_bytep   profile;
1075
    int         profcomp;
1095
    int         profcomp;
1076
1096
1077
    if (png_get_iCCP (pp, info, &profname, &profcomp, &profile, &proflen))
1097
    if (png_get_iCCP (pp, info, &profname, &profcomp, &profile, &proflen))
Lines 1199-1204 save_image (const gchar *filename, Link Here
1199
  guchar red, green, blue;      /* Used for palette background */
1219
  guchar red, green, blue;      /* Used for palette background */
1200
  time_t cutime;                /* Time since epoch */
1220
  time_t cutime;                /* Time since epoch */
1201
  struct tm *gmt;               /* GMT broken down */
1221
  struct tm *gmt;               /* GMT broken down */
1222
  int color_type;               /* type of colors in image */
1223
  int bit_depth;                /* width of colors in bit */
1202
1224
1203
  guchar remap[256];            /* Re-mapping for the palette */
1225
  guchar remap[256];            /* Re-mapping for the palette */
1204
1226
Lines 1207-1213 save_image (const gchar *filename, Link Here
1207
  if (pngvals.comment)
1229
  if (pngvals.comment)
1208
    {
1230
    {
1209
      GimpParasite *parasite;
1231
      GimpParasite *parasite;
1232
#ifndef PNG_iTXt_SUPPORTED
1210
      gsize text_length = 0;
1233
      gsize text_length = 0;
1234
#endif
1211
1235
1212
      parasite = gimp_image_parasite_find (orig_image_ID, "gimp-comment");
1236
      parasite = gimp_image_parasite_find (orig_image_ID, "gimp-comment");
1213
      if (parasite)
1237
      if (parasite)
Lines 1248-1254 save_image (const gchar *filename, Link Here
1248
  pp = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1272
  pp = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
1249
  info = png_create_info_struct (pp);
1273
  info = png_create_info_struct (pp);
1250
1274
1251
  if (setjmp (pp->jmpbuf))
1275
  if (setjmp (png_jmpbuf(pp)))
1252
    {
1276
    {
1253
      g_set_error (error, 0, 0,
1277
      g_set_error (error, 0, 0,
1254
                   _("Error while saving '%s'. Could not save image."),
1278
                   _("Error while saving '%s'. Could not save image."),
Lines 1290-1300 save_image (const gchar *filename, Link Here
1290
1314
1291
  png_set_compression_level (pp, pngvals.compression_level);
1315
  png_set_compression_level (pp, pngvals.compression_level);
1292
1316
1293
  info->width          = drawable->width;
1294
  info->height         = drawable->height;
1295
  info->bit_depth      = 8;
1296
  info->interlace_type = pngvals.interlaced;
1297
1298
  /*
1317
  /*
1299
   * Initialise remap[]
1318
   * Initialise remap[]
1300
   */
1319
   */
Lines 1308-1344 save_image (const gchar *filename, Link Here
1308
  switch (type)
1327
  switch (type)
1309
    {
1328
    {
1310
    case GIMP_RGB_IMAGE:
1329
    case GIMP_RGB_IMAGE:
1311
      info->color_type = PNG_COLOR_TYPE_RGB;
1330
      color_type = PNG_COLOR_TYPE_RGB;
1312
      bpp = 3;
1331
      bpp = 3;
1313
      break;
1332
      break;
1314
1333
1315
    case GIMP_RGBA_IMAGE:
1334
    case GIMP_RGBA_IMAGE:
1316
      info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1335
      color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1317
      bpp = 4;
1336
      bpp = 4;
1318
      break;
1337
      break;
1319
1338
1320
    case GIMP_GRAY_IMAGE:
1339
    case GIMP_GRAY_IMAGE:
1321
      info->color_type = PNG_COLOR_TYPE_GRAY;
1340
      color_type = PNG_COLOR_TYPE_GRAY;
1322
      bpp = 1;
1341
      bpp = 1;
1323
      break;
1342
      break;
1324
1343
1325
    case GIMP_GRAYA_IMAGE:
1344
    case GIMP_GRAYA_IMAGE:
1326
      info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
1345
      color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
1327
      bpp = 2;
1346
      bpp = 2;
1328
      break;
1347
      break;
1329
1348
1330
    case GIMP_INDEXED_IMAGE:
1349
    case GIMP_INDEXED_IMAGE:
1331
      bpp = 1;
1350
      bpp = 1;
1332
      info->color_type = PNG_COLOR_TYPE_PALETTE;
1351
      color_type = PNG_COLOR_TYPE_PALETTE;
1333
      info->valid |= PNG_INFO_PLTE;
1352
      png_set_PLTE(pp, info,
1334
      info->palette =
1353
                   (png_colorp) gimp_image_get_colormap (image_ID, &num_colors),
1335
        (png_colorp) gimp_image_get_colormap (image_ID, &num_colors);
1354
                   num_colors);
1336
      info->num_palette = num_colors;
1337
      break;
1355
      break;
1338
1356
1339
    case GIMP_INDEXEDA_IMAGE:
1357
    case GIMP_INDEXEDA_IMAGE:
1340
      bpp = 2;
1358
      bpp = 2;
1341
      info->color_type = PNG_COLOR_TYPE_PALETTE;
1359
      color_type = PNG_COLOR_TYPE_PALETTE;
1342
      /* fix up transparency */
1360
      /* fix up transparency */
1343
      respin_cmap (pp, info, remap, image_ID, drawable);
1361
      respin_cmap (pp, info, remap, image_ID, drawable);
1344
      break;
1362
      break;
Lines 1352-1368 save_image (const gchar *filename, Link Here
1352
   * Fix bit depths for (possibly) smaller colormap images
1370
   * Fix bit depths for (possibly) smaller colormap images
1353
   */
1371
   */
1354
1372
1355
  if (info->valid & PNG_INFO_PLTE)
1373
  bit_depth = 8;
1374
1375
  if (png_get_valid(pp, info, PNG_INFO_PLTE))
1356
    {
1376
    {
1357
      if (info->num_palette <= 2)
1377
      png_colorp palette;
1358
        info->bit_depth = 1;
1378
      int num_palette;
1359
      else if (info->num_palette <= 4)
1379
      png_get_PLTE(pp, info, &palette, &num_palette);
1360
        info->bit_depth = 2;
1380
      
1361
      else if (info->num_palette <= 16)
1381
      if (num_palette <= 2)
1362
        info->bit_depth = 4;
1382
        bit_depth = 1;
1383
      else if (num_palette <= 4)
1384
        bit_depth = 2;
1385
      else if (num_palette <= 16)
1386
        bit_depth = 4;
1363
      /* otherwise the default is fine */
1387
      /* otherwise the default is fine */
1364
    }
1388
    }
1365
1389
1390
  png_set_IHDR(pp, info,
1391
               drawable->width, drawable->height, bit_depth, color_type,
1392
               pngvals.interlaced ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
1393
               PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1394
1366
  /* All this stuff is optional extras, if the user is aiming for smallest
1395
  /* All this stuff is optional extras, if the user is aiming for smallest
1367
     possible file size she can turn them all off */
1396
     possible file size she can turn them all off */
1368
1397
Lines 1476-1482 save_image (const gchar *filename, Link Here
1476
   * Convert unpacked pixels to packed if necessary
1505
   * Convert unpacked pixels to packed if necessary
1477
   */
1506
   */
1478
1507
1479
  if (info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8)
1508
  if (png_get_color_type(pp, info) ==
1509
      PNG_COLOR_TYPE_PALETTE && png_get_bit_depth(pp, info) < 8)
1480
    png_set_packing (pp);
1510
    png_set_packing (pp);
1481
1511
1482
  /*
1512
  /*
Lines 1528-1534 save_image (const gchar *filename, Link Here
1528
1558
1529
          /* If we're dealing with a paletted image with
1559
          /* If we're dealing with a paletted image with
1530
           * transparency set, write out the remapped palette */
1560
           * transparency set, write out the remapped palette */
1531
          if (info->valid & PNG_INFO_tRNS)
1561
          if (png_get_valid(pp, info, PNG_INFO_tRNS))
1532
            {
1562
            {
1533
              guchar inverse_remap[256];
1563
              guchar inverse_remap[256];
1534
1564
Lines 1548-1554 save_image (const gchar *filename, Link Here
1548
            }
1578
            }
1549
          /* Otherwise if we have a paletted image and transparency
1579
          /* Otherwise if we have a paletted image and transparency
1550
           * couldn't be set, we ignore the alpha channel */
1580
           * couldn't be set, we ignore the alpha channel */
1551
          else if (info->valid & PNG_INFO_PLTE && bpp == 2)
1581
          else if (png_get_valid(pp, info, PNG_INFO_PLTE) && bpp == 2)
1552
            {
1582
            {
1553
              for (i = 0; i < num; ++i)
1583
              for (i = 0; i < num; ++i)
1554
                {
1584
                {
Lines 1563-1569 save_image (const gchar *filename, Link Here
1563
          png_write_rows (pp, pixels, num);
1593
          png_write_rows (pp, pixels, num);
1564
1594
1565
          gimp_progress_update (((double) pass + (double) end /
1595
          gimp_progress_update (((double) pass + (double) end /
1566
                                 (double) info->height) /
1596
                                 (double) png_get_image_height(pp, info)) /
1567
                                (double) num_passes);
1597
                                (double) num_passes);
1568
        }
1598
        }
1569
    }
1599
    }
1570
- 

Return to bug 354985