~ubuntu-branches/ubuntu/intrepid/perl-tk/intrepid

« back to all changes in this revision

Viewing changes to PNG/libpng/pngwrite.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Tuckley
  • Date: 2008-02-15 13:56:59 UTC
  • mfrom: (1.1.3 upstream) (4.1.1 hardy)
  • Revision ID: james.westby@ubuntu.com-20080215135659-ru2oqlykuju20fav
Tags: 1:804.028-1
* New Upstream Release (Closes: #463080).
* Update to Debhelper v5.
* Build with XFT=1 (Closes: #411129).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* pngwrite.c - general routines to write a PNG file
3
3
 *
4
 
 * libpng 1.2.5 - October 3, 2002
 
4
 * Last changed in libpng 1.2.9 April 14, 2006
5
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 
 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
 
6
 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
7
7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8
8
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9
9
 */
26
26
png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
27
27
{
28
28
   png_debug(1, "in png_write_info_before_PLTE\n");
 
29
   if (png_ptr == NULL || info_ptr == NULL)
 
30
      return;
29
31
   if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
30
32
   {
31
33
   png_write_sig(png_ptr); /* write PNG signature */
32
34
#if defined(PNG_MNG_FEATURES_SUPPORTED)
33
35
   if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
34
36
   {
35
 
      png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
 
37
      png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
36
38
      png_ptr->mng_features_permitted=0;
37
39
   }
38
40
#endif
104
106
            up++)
105
107
       {
106
108
         int keep=png_handle_as_unknown(png_ptr, up->name);
107
 
         if (keep != HANDLE_CHUNK_NEVER &&
108
 
            up->location && (!(up->location & PNG_HAVE_PLTE)) &&
109
 
            ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
 
109
         if (keep != PNG_HANDLE_CHUNK_NEVER &&
 
110
            up->location && !(up->location & PNG_HAVE_PLTE) &&
 
111
            !(up->location & PNG_HAVE_IDAT) &&
 
112
            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
110
113
            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
111
114
         {
112
115
            png_write_chunk(png_ptr, up->name, up->data, up->size);
127
130
 
128
131
   png_debug(1, "in png_write_info\n");
129
132
 
 
133
   if (png_ptr == NULL || info_ptr == NULL)
 
134
      return;
 
135
 
130
136
   png_write_info_before_PLTE(png_ptr, info_ptr);
131
137
 
132
138
   if (info_ptr->valid & PNG_INFO_PLTE)
133
139
      png_write_PLTE(png_ptr, info_ptr->palette,
134
140
         (png_uint_32)info_ptr->num_palette);
135
141
   else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
136
 
      png_error(png_ptr, "Valid palette required for paletted images\n");
 
142
      png_error(png_ptr, "Valid palette required for paletted images");
137
143
 
138
144
#if defined(PNG_WRITE_tRNS_SUPPORTED)
139
145
   if (info_ptr->valid & PNG_INFO_tRNS)
182
188
          info_ptr->scal_s_width, info_ptr->scal_s_height);
183
189
#else
184
190
      png_warning(png_ptr,
185
 
          "png_write_sCAL not supported; sCAL chunk not written.\n");
 
191
          "png_write_sCAL not supported; sCAL chunk not written.");
186
192
#endif
187
193
#endif
188
194
#endif
221
227
                         info_ptr->text[i].lang_key,
222
228
                         info_ptr->text[i].text);
223
229
#else
224
 
          png_warning(png_ptr, "Unable to write international text\n");
 
230
          png_warning(png_ptr, "Unable to write international text");
225
231
#endif
226
232
          /* Mark this chunk as written */
227
233
          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
235
241
            info_ptr->text[i].text, 0,
236
242
            info_ptr->text[i].compression);
237
243
#else
238
 
         png_warning(png_ptr, "Unable to write compressed text\n");
 
244
         png_warning(png_ptr, "Unable to write compressed text");
239
245
#endif
240
246
         /* Mark this chunk as written */
241
247
         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
248
254
                         info_ptr->text[i].text,
249
255
                         0);
250
256
#else
251
 
         png_warning(png_ptr, "Unable to write uncompressed text\n");
 
257
         png_warning(png_ptr, "Unable to write uncompressed text");
252
258
#endif
253
259
         /* Mark this chunk as written */
254
260
         info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
267
273
            up++)
268
274
       {
269
275
         int keep=png_handle_as_unknown(png_ptr, up->name);
270
 
         if (keep != HANDLE_CHUNK_NEVER &&
 
276
         if (keep != PNG_HANDLE_CHUNK_NEVER &&
271
277
            up->location && (up->location & PNG_HAVE_PLTE) &&
272
278
            !(up->location & PNG_HAVE_IDAT) &&
273
 
            ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
 
279
            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
274
280
            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
275
281
         {
276
282
            png_write_chunk(png_ptr, up->name, up->data, up->size);
289
295
png_write_end(png_structp png_ptr, png_infop info_ptr)
290
296
{
291
297
   png_debug(1, "in png_write_end\n");
 
298
   if (png_ptr == NULL)
 
299
      return;
292
300
   if (!(png_ptr->mode & PNG_HAVE_IDAT))
293
301
      png_error(png_ptr, "No IDATs written into file");
294
302
 
322
330
                         info_ptr->text[i].lang_key,
323
331
                         info_ptr->text[i].text);
324
332
#else
325
 
             png_warning(png_ptr, "Unable to write international text\n");
 
333
             png_warning(png_ptr, "Unable to write international text");
326
334
#endif
327
335
             /* Mark this chunk as written */
328
336
             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
335
343
               info_ptr->text[i].text, 0,
336
344
               info_ptr->text[i].compression);
337
345
#else
338
 
            png_warning(png_ptr, "Unable to write compressed text\n");
 
346
            png_warning(png_ptr, "Unable to write compressed text");
339
347
#endif
340
348
            /* Mark this chunk as written */
341
349
            info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
347
355
            png_write_tEXt(png_ptr, info_ptr->text[i].key,
348
356
               info_ptr->text[i].text, 0);
349
357
#else
350
 
            png_warning(png_ptr, "Unable to write uncompressed text\n");
 
358
            png_warning(png_ptr, "Unable to write uncompressed text");
351
359
#endif
352
360
 
353
361
            /* Mark this chunk as written */
367
375
            up++)
368
376
       {
369
377
         int keep=png_handle_as_unknown(png_ptr, up->name);
370
 
         if (keep != HANDLE_CHUNK_NEVER &&
 
378
         if (keep != PNG_HANDLE_CHUNK_NEVER &&
371
379
            up->location && (up->location & PNG_AFTER_IDAT) &&
372
 
            ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
 
380
            ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
373
381
            (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
374
382
         {
375
383
            png_write_chunk(png_ptr, up->name, up->data, up->size);
457
465
#endif
458
466
#endif /* PNG_1_0_X */
459
467
 
 
468
   /* added at libpng-1.2.6 */
 
469
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 
470
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
 
471
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
 
472
#endif
 
473
 
460
474
#ifdef PNG_SETJMP_SUPPORTED
461
475
#ifdef USE_FAR_KEYWORD
462
476
   if (setjmp(jmpbuf))
470
484
      return (NULL);
471
485
   }
472
486
#ifdef USE_FAR_KEYWORD
473
 
   png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
 
487
   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
474
488
#endif
475
489
#endif
476
490
 
537
551
#ifdef USE_FAR_KEYWORD
538
552
   if (setjmp(jmpbuf))
539
553
      PNG_ABORT();
540
 
   png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
 
554
   png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
541
555
#else
542
556
   if (setjmp(png_ptr->jmpbuf))
543
557
      PNG_ABORT();
547
561
}
548
562
 
549
563
/* Initialize png_ptr structure, and allocate any memory needed */
 
564
#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
 
565
/* Deprecated. */
550
566
#undef png_write_init
551
567
void PNGAPI
552
568
png_write_init(png_structp png_ptr)
561
577
{
562
578
   /* We only come here via pre-1.0.12-compiled applications */
563
579
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
564
 
   if(sizeof(png_struct) > png_struct_size || sizeof(png_info) > png_info_size)
 
580
   if(png_sizeof(png_struct) > png_struct_size ||
 
581
      png_sizeof(png_info) > png_info_size)
565
582
   {
566
583
      char msg[80];
567
584
      png_ptr->warning_fn=NULL;
576
593
      png_warning(png_ptr, msg);
577
594
   }
578
595
#endif
579
 
   if(sizeof(png_struct) > png_struct_size)
 
596
   if(png_sizeof(png_struct) > png_struct_size)
580
597
     {
581
598
       png_ptr->error_fn=NULL;
582
599
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
585
602
       png_error(png_ptr,
586
603
       "The png struct allocated by the application for writing is too small.");
587
604
     }
588
 
   if(sizeof(png_info) > png_info_size)
 
605
   if(png_sizeof(png_info) > png_info_size)
589
606
     {
590
607
       png_ptr->error_fn=NULL;
591
608
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
596
613
     }
597
614
   png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
598
615
}
 
616
#endif /* PNG_1_0_X || PNG_1_2_X */
599
617
 
600
618
 
601
619
void PNGAPI
606
624
#ifdef PNG_SETJMP_SUPPORTED
607
625
   jmp_buf tmp_jmp; /* to save current jump buffer */
608
626
#endif
 
627
 
609
628
   int i = 0;
 
629
 
 
630
   if (png_ptr == NULL)
 
631
      return;
 
632
 
610
633
   do
611
634
   {
612
635
     if (user_png_ver[i] != png_libpng_ver[i])
626
649
 
627
650
#ifdef PNG_SETJMP_SUPPORTED
628
651
   /* save jump buffer and error functions */
629
 
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
 
652
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
630
653
#endif
631
654
 
632
 
   if (sizeof(png_struct) > png_struct_size)
 
655
   if (png_sizeof(png_struct) > png_struct_size)
633
656
     {
634
657
       png_destroy_struct(png_ptr);
635
658
       png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
637
660
     }
638
661
 
639
662
   /* reset all variables to 0 */
640
 
   png_memset(png_ptr, 0, sizeof (png_struct));
 
663
   png_memset(png_ptr, 0, png_sizeof (png_struct));
 
664
 
 
665
   /* added at libpng-1.2.6 */
 
666
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 
667
   png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
 
668
   png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
 
669
#endif
641
670
 
642
671
#if !defined(PNG_1_0_X)
643
672
#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
647
676
 
648
677
#ifdef PNG_SETJMP_SUPPORTED
649
678
   /* restore jump buffer */
650
 
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
 
679
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
651
680
#endif
652
681
 
653
682
   png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
677
706
   png_bytepp rp; /* row pointer */
678
707
 
679
708
   png_debug(1, "in png_write_rows\n");
 
709
 
 
710
   if (png_ptr == NULL)
 
711
      return;
 
712
 
680
713
   /* loop through the rows */
681
714
   for (i = 0, rp = row; i < num_rows; i++, rp++)
682
715
   {
694
727
   int pass, num_pass; /* pass variables */
695
728
   png_bytepp rp; /* points to current row */
696
729
 
 
730
   if (png_ptr == NULL)
 
731
      return;
 
732
 
697
733
   png_debug(1, "in png_write_image\n");
698
734
#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
699
735
   /* intialize interlace handling.  If image is not interlaced,
717
753
void PNGAPI
718
754
png_write_row(png_structp png_ptr, png_bytep row)
719
755
{
 
756
   if (png_ptr == NULL)
 
757
      return;
720
758
   png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
721
759
      png_ptr->row_number, png_ptr->pass);
 
760
 
722
761
   /* initialize transformations and other stuff if first time */
723
762
   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
724
763
   {
827
866
   png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
828
867
      png_ptr->row_info.channels);
829
868
 
830
 
   png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
831
 
      (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
 
869
   png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
 
870
      png_ptr->row_info.width);
832
871
 
833
872
   png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
834
873
   png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
892
931
png_set_flush(png_structp png_ptr, int nrows)
893
932
{
894
933
   png_debug(1, "in png_set_flush\n");
 
934
   if (png_ptr == NULL)
 
935
      return;
895
936
   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
896
937
}
897
938
 
902
943
   int wrote_IDAT;
903
944
 
904
945
   png_debug(1, "in png_write_flush\n");
 
946
   if (png_ptr == NULL)
 
947
      return;
905
948
   /* We have already written out all of the data */
906
949
   if (png_ptr->row_number >= png_ptr->num_rows)
907
950
     return;
1049
1092
 
1050
1093
#ifdef PNG_SETJMP_SUPPORTED
1051
1094
   /* reset structure */
1052
 
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
 
1095
   png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
1053
1096
#endif
1054
1097
 
1055
1098
   error_fn = png_ptr->error_fn;
1059
1102
   free_fn = png_ptr->free_fn;
1060
1103
#endif
1061
1104
 
1062
 
   png_memset(png_ptr, 0, sizeof (png_struct));
 
1105
   png_memset(png_ptr, 0, png_sizeof (png_struct));
1063
1106
 
1064
1107
   png_ptr->error_fn = error_fn;
1065
1108
   png_ptr->warning_fn = warning_fn;
1069
1112
#endif
1070
1113
 
1071
1114
#ifdef PNG_SETJMP_SUPPORTED
1072
 
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
 
1115
   png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
1073
1116
#endif
1074
1117
}
1075
1118
 
1078
1121
png_set_filter(png_structp png_ptr, int method, int filters)
1079
1122
{
1080
1123
   png_debug(1, "in png_set_filter\n");
 
1124
   if (png_ptr == NULL)
 
1125
      return;
1081
1126
#if defined(PNG_MNG_FEATURES_SUPPORTED)
1082
1127
   if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
1083
1128
      (method == PNG_INTRAPIXEL_DIFFERENCING))
1186
1231
   int i;
1187
1232
 
1188
1233
   png_debug(1, "in png_set_filter_heuristics\n");
 
1234
   if (png_ptr == NULL)
 
1235
      return;
1189
1236
   if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1190
1237
   {
1191
1238
      png_warning(png_ptr, "Unknown filter heuristic method");
1211
1258
      if (png_ptr->prev_filters == NULL)
1212
1259
      {
1213
1260
         png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1214
 
            (png_uint_32)(sizeof(png_byte) * num_weights));
 
1261
            (png_uint_32)(png_sizeof(png_byte) * num_weights));
1215
1262
 
1216
1263
         /* To make sure that the weighting starts out fairly */
1217
1264
         for (i = 0; i < num_weights; i++)
1223
1270
      if (png_ptr->filter_weights == NULL)
1224
1271
      {
1225
1272
         png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1226
 
            (png_uint_32)(sizeof(png_uint_16) * num_weights));
 
1273
            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1227
1274
 
1228
1275
         png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1229
 
            (png_uint_32)(sizeof(png_uint_16) * num_weights));
 
1276
            (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1230
1277
         for (i = 0; i < num_weights; i++)
1231
1278
         {
1232
1279
            png_ptr->inv_filter_weights[i] =
1257
1304
   if (png_ptr->filter_costs == NULL)
1258
1305
   {
1259
1306
      png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1260
 
         (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
 
1307
         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1261
1308
 
1262
1309
      png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1263
 
         (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
 
1310
         (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1264
1311
 
1265
1312
      for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1266
1313
      {
1298
1345
png_set_compression_level(png_structp png_ptr, int level)
1299
1346
{
1300
1347
   png_debug(1, "in png_set_compression_level\n");
 
1348
   if (png_ptr == NULL)
 
1349
      return;
1301
1350
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1302
1351
   png_ptr->zlib_level = level;
1303
1352
}
1306
1355
png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1307
1356
{
1308
1357
   png_debug(1, "in png_set_compression_mem_level\n");
 
1358
   if (png_ptr == NULL)
 
1359
      return;
1309
1360
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1310
1361
   png_ptr->zlib_mem_level = mem_level;
1311
1362
}
1314
1365
png_set_compression_strategy(png_structp png_ptr, int strategy)
1315
1366
{
1316
1367
   png_debug(1, "in png_set_compression_strategy\n");
 
1368
   if (png_ptr == NULL)
 
1369
      return;
1317
1370
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1318
1371
   png_ptr->zlib_strategy = strategy;
1319
1372
}
1321
1374
void PNGAPI
1322
1375
png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1323
1376
{
 
1377
   if (png_ptr == NULL)
 
1378
      return;
1324
1379
   if (window_bits > 15)
1325
1380
      png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1326
1381
   else if (window_bits < 8)
1341
1396
png_set_compression_method(png_structp png_ptr, int method)
1342
1397
{
1343
1398
   png_debug(1, "in png_set_compression_method\n");
 
1399
   if (png_ptr == NULL)
 
1400
      return;
1344
1401
   if (method != 8)
1345
1402
      png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1346
1403
   png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1350
1407
void PNGAPI
1351
1408
png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1352
1409
{
 
1410
   if (png_ptr == NULL)
 
1411
      return;
1353
1412
   png_ptr->write_row_fn = write_row_fn;
1354
1413
}
1355
1414
 
1359
1418
   write_user_transform_fn)
1360
1419
{
1361
1420
   png_debug(1, "in png_set_write_user_transform_fn\n");
 
1421
   if (png_ptr == NULL)
 
1422
      return;
1362
1423
   png_ptr->transformations |= PNG_USER_TRANSFORM;
1363
1424
   png_ptr->write_user_transform_fn = write_user_transform_fn;
1364
1425
}
1370
1431
png_write_png(png_structp png_ptr, png_infop info_ptr,
1371
1432
              int transforms, voidp params)
1372
1433
{
 
1434
   if (png_ptr == NULL || info_ptr == NULL)
 
1435
      return;
1373
1436
#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1374
1437
   /* invert the alpha channel from opacity to transparency */
1375
1438
   if (transforms & PNG_TRANSFORM_INVERT_ALPHA)