Lines 555-603
TRACEUSER( "Jonathan", _T("PNG write: Width = %d Height = %d\n"),Width,Height);
Link Here
|
555 |
//png_set_compression_window_bits(png_ptr, 15); |
555 |
//png_set_compression_window_bits(png_ptr, 15); |
556 |
//png_set_compression_method(png_ptr, 8); |
556 |
//png_set_compression_method(png_ptr, 8); |
557 |
|
557 |
|
558 |
info_ptr->valid = 0; // - this describes which optional chunks to write to the |
558 |
// - this describes which optional chunks to write to the |
559 |
// file. Note that if you are writing a |
559 |
// file. Note that if you are writing a |
560 |
// PNG_COLOR_TYPE_PALETTE file, the PLTE chunk is not |
560 |
// PNG_COLOR_TYPE_PALETTE file, the PLTE chunk is not |
561 |
// optional, but must still be marked for writing. To |
561 |
// optional, but must still be marked for writing. To |
562 |
// mark chunks for writing, OR valid with the |
562 |
// mark chunks for writing, OR valid with the |
563 |
// appropriate PNG_INFO_<chunk name> define. |
563 |
// appropriate PNG_INFO_<chunk name> define. |
564 |
// Set the file information here |
564 |
// Set the file information here |
565 |
info_ptr->width = Width; // - holds the width of the file |
565 |
|
566 |
info_ptr->height = Height; // - holds the height of the file |
566 |
png_uint_32 _width, _height; |
|
|
567 |
int _bit_depth, _color_type, _interlace_type, _unit_type; |
568 |
|
569 |
_width = Width; |
570 |
_height = Height; |
567 |
|
571 |
|
568 |
// resolution of image |
572 |
// resolution of image |
569 |
info_ptr->valid |= PNG_INFO_pHYs; |
573 |
_unit_type = 1; // meter |
570 |
info_ptr->x_pixels_per_unit = pInfo->biXPelsPerMeter; |
574 |
|
571 |
info_ptr->y_pixels_per_unit = pInfo->biYPelsPerMeter; |
575 |
TRACEUSER( "Jonathan", _T("PNG write: X,y dpi = %d %d\n"), pInfo->biXPelsPerMeter, pInfo->biYPelsPerMeter); |
572 |
info_ptr->phys_unit_type = 1; // meter |
|
|
573 |
TRACEUSER( "Jonathan", _T("PNG write: X,y dpi = %d %d\n"),info_ptr->x_pixels_per_unit, info_ptr->y_pixels_per_unit); |
574 |
if (InterlaceState) |
576 |
if (InterlaceState) |
575 |
info_ptr->interlace_type = 1; // - currently 0 for none, 1 for interlaced |
577 |
_interlace_type = 1; // - currently 0 for none, 1 for interlaced |
576 |
else |
578 |
else |
577 |
info_ptr->interlace_type = 0; // - currently 0 for none, 1 for interlaced |
579 |
_interlace_type = 0; // - currently 0 for none, 1 for interlaced |
578 |
|
580 |
|
579 |
BitsPerPixel = pInfo->biBitCount; |
581 |
BitsPerPixel = pInfo->biBitCount; |
580 |
TRACEUSER( "Jonathan", _T("PNG write: Bitdepth = %d\n"),BitsPerPixel); |
582 |
TRACEUSER( "Jonathan", _T("PNG write: Bitdepth = %d\n"),BitsPerPixel); |
581 |
info_ptr->palette = NULL; |
583 |
png_colorp _palette = NULL; |
582 |
info_ptr->num_palette = 0; |
584 |
int _num_palette = 0; |
583 |
//info_ptr->trans_values = 0; // - transparent pixel for non-paletted images |
585 |
//info_ptr->trans_values = 0; // - transparent pixel for non-paletted images |
584 |
info_ptr->trans = NULL; // - array of transparent entries for paletted images |
586 |
png_bytep _trans_alpha = 0; |
585 |
info_ptr->num_trans = 0; // - number of transparent entries |
587 |
int _num_trans = 0; |
586 |
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour); |
588 |
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour); |
587 |
if ( BitsPerPixel <= 8 ) |
589 |
if ( BitsPerPixel <= 8 ) |
588 |
{ |
590 |
{ |
589 |
info_ptr->bit_depth = BitsPerPixel; // - holds the bit depth of one of the image channels |
591 |
_bit_depth = BitsPerPixel; |
590 |
info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; // - describes the channels and what they mean |
592 |
_color_type = PNG_COLOR_TYPE_PALETTE; // - describes the channels and what they mean |
591 |
// see the PNG_COLOR_TYPE_ defines for more information |
593 |
// see the PNG_COLOR_TYPE_ defines for more information |
592 |
// set the palette if there is one |
594 |
// set the palette if there is one |
593 |
info_ptr->valid |= PNG_INFO_PLTE; |
|
|
594 |
INT32 PaletteEntries = pInfo->biClrUsed; |
595 |
INT32 PaletteEntries = pInfo->biClrUsed; |
595 |
info_ptr->palette = (png_color_struct *)CCMalloc(PaletteEntries * sizeof (png_color)); |
596 |
_palette = (png_color_struct *)CCMalloc(PaletteEntries * sizeof (png_color)); |
596 |
if (info_ptr->palette == NULL) |
597 |
if (_palette == NULL) |
597 |
File->GotError( _R(IDS_OUT_OF_MEMORY) ); |
598 |
File->GotError( _R(IDS_OUT_OF_MEMORY) ); |
598 |
|
599 |
|
599 |
info_ptr->num_palette = PaletteEntries; |
600 |
_num_palette = PaletteEntries; |
600 |
png_color_struct * pPNGPalette = info_ptr->palette; |
601 |
png_color_struct * pPNGPalette = _palette; |
|
|
602 |
|
603 |
png_set_IHDR (png_ptr, info_ptr, _width, _height, _bit_depth, _color_type, |
604 |
_interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
605 |
|
601 |
// ... set palette colors ... |
606 |
// ... set palette colors ... |
602 |
if (pQuadPalette && PaletteEntries > 0) |
607 |
if (pQuadPalette && PaletteEntries > 0) |
603 |
{ |
608 |
{ |
Lines 625-631
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour);
Link Here
|
625 |
} |
630 |
} |
626 |
else |
631 |
else |
627 |
File->GotError(_R(IDS_PNG_ERR_WRITE_PALETTE)); |
632 |
File->GotError(_R(IDS_PNG_ERR_WRITE_PALETTE)); |
628 |
|
633 |
|
|
|
634 |
png_set_PLTE(png_ptr, info_ptr, _palette, _num_palette); |
635 |
/* |
636 |
* Although documentation says otherwise, I can free palette |
637 |
* memory here since libpng-1.0.9beta7... |
638 |
*/ |
639 |
CCFree(_palette); |
640 |
_palette = NULL; |
641 |
|
629 |
// Now check to see if transparency is present or not |
642 |
// Now check to see if transparency is present or not |
630 |
if (TransparentColour >= 0 && TransparentColour <= PaletteEntries ) |
643 |
if (TransparentColour >= 0 && TransparentColour <= PaletteEntries ) |
631 |
{ |
644 |
{ |
Lines 634-646
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour);
Link Here
|
634 |
// We will only create as many as we require, i.e. up to the transparent colour entry |
647 |
// We will only create as many as we require, i.e. up to the transparent colour entry |
635 |
// rather a full palettes worth |
648 |
// rather a full palettes worth |
636 |
INT32 NumEntries = TransparentColour + 1; |
649 |
INT32 NumEntries = TransparentColour + 1; |
637 |
info_ptr->trans = (png_byte *)CCMalloc(NumEntries * sizeof (png_byte)); |
650 |
_trans_alpha = (png_byte *)CCMalloc(NumEntries * sizeof (png_byte)); |
638 |
if (info_ptr->trans) |
651 |
if (_trans_alpha) |
639 |
{ |
652 |
{ |
640 |
// Set the number of transparent entries |
653 |
// Set the number of transparent entries |
641 |
info_ptr->num_trans = NumEntries; |
654 |
_num_trans = NumEntries; |
642 |
png_byte * pTransEntry = info_ptr->trans; |
655 |
png_byte * pTransEntry = _trans_alpha; |
643 |
info_ptr->valid |= PNG_INFO_tRNS; |
|
|
644 |
for (INT32 i = 0; i < TransparentColour; i++) |
656 |
for (INT32 i = 0; i < TransparentColour; i++) |
645 |
{ |
657 |
{ |
646 |
*pTransEntry = 255; // set it fully opaque |
658 |
*pTransEntry = 255; // set it fully opaque |
Lines 648-661
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour);
Link Here
|
648 |
} |
660 |
} |
649 |
// We should now be at the transparent entry so set it fully transparent |
661 |
// We should now be at the transparent entry so set it fully transparent |
650 |
*pTransEntry = 0; |
662 |
*pTransEntry = 0; |
|
|
663 |
|
664 |
png_set_tRNS(png_ptr, info_ptr, _trans_alpha, _num_trans, NULL); |
665 |
CCFree(_trans_alpha); |
666 |
_trans_alpha = NULL; |
651 |
} |
667 |
} |
|
|
668 |
|
652 |
} |
669 |
} |
653 |
} |
670 |
} |
654 |
else if (BitsPerPixel == 24) |
671 |
else if (BitsPerPixel == 24) |
655 |
{ |
672 |
{ |
656 |
// We must be 24 bpp |
673 |
// We must be 24 bpp |
657 |
info_ptr->bit_depth = BitsPerPixel/3; // - holds the bit depth of one of the image channels |
674 |
_bit_depth = BitsPerPixel/3; |
658 |
info_ptr->color_type = PNG_COLOR_TYPE_RGB; // - describes the channels and what they mean |
675 |
_color_type = PNG_COLOR_TYPE_RGB; |
|
|
676 |
|
677 |
png_set_IHDR (png_ptr, info_ptr, _width, _height, _bit_depth, _color_type, |
678 |
_interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
679 |
|
659 |
// optional significant bit chunk |
680 |
// optional significant bit chunk |
660 |
//info_ptr->valid |= PNG_INFO_sBIT; |
681 |
//info_ptr->valid |= PNG_INFO_sBIT; |
661 |
// otherwise, if we are dealing with a color image then |
682 |
// otherwise, if we are dealing with a color image then |
Lines 668-675
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour);
Link Here
|
668 |
else if (BitsPerPixel == 32) |
689 |
else if (BitsPerPixel == 32) |
669 |
{ |
690 |
{ |
670 |
// We must be a 32 bpp |
691 |
// We must be a 32 bpp |
671 |
info_ptr->bit_depth = BitsPerPixel/4; // - holds the bit depth of one of the image channels |
692 |
_bit_depth = BitsPerPixel/4; // - holds the bit depth of one of the image channels |
672 |
info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; // - describes the channels and what they mean |
693 |
_color_type = PNG_COLOR_TYPE_RGB_ALPHA; // - describes the channels and what they mean |
|
|
694 |
|
695 |
png_set_IHDR (png_ptr, info_ptr, _width, _height, _bit_depth, _color_type, |
696 |
_interlace_type, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
673 |
// optional significant bit chunk |
697 |
// optional significant bit chunk |
674 |
//info_ptr->valid |= PNG_INFO_sBIT; |
698 |
//info_ptr->valid |= PNG_INFO_sBIT; |
675 |
// otherwise, if we are dealing with a color image then |
699 |
// otherwise, if we are dealing with a color image then |
Lines 687-702
TRACEUSER( "Jonathan", _T("PNG write: TransColour = %d\n"),TransparentColour);
Link Here
|
687 |
else |
711 |
else |
688 |
ERROR2(FALSE,"OutputPNG::OutputPNGHeader Unknown bit depth"); |
712 |
ERROR2(FALSE,"OutputPNG::OutputPNGHeader Unknown bit depth"); |
689 |
|
713 |
|
690 |
TRACEUSER( "Jonathan", _T("PNG write: bit_depth = %d color_type = %d\n"),info_ptr->bit_depth,info_ptr->color_type); |
714 |
TRACEUSER( "Jonathan", _T("PNG write: bit_depth = %d color_type = %d\n"),_bit_depth,_color_type); |
691 |
|
715 |
|
|
|
716 |
png_set_pHYs(png_ptr, info_ptr, pInfo->biXPelsPerMeter, pInfo->biYPelsPerMeter, _unit_type); |
692 |
// Could use:- |
717 |
// Could use:- |
693 |
// if we are dealing with a grayscale image then |
718 |
// if we are dealing with a grayscale image then |
694 |
//info_ptr->sig_bit.gray = true_bit_depth; |
719 |
//info_ptr->sig_bit.gray = true_bit_depth; |
695 |
|
720 |
|
696 |
// gamma - the gamma the file is written at |
721 |
// gamma - the gamma the file is written at |
697 |
info_ptr->hist = NULL; // - histogram of palette |
722 |
//info_ptr->hist = NULL; // - histogram of palette |
698 |
info_ptr->text = NULL; // - text comments in the file. |
723 |
//info_ptr->text = NULL; // - text comments in the file. |
699 |
info_ptr->num_text = 0; // - number of comments |
724 |
//info_ptr->num_text = 0; // - number of comments |
700 |
|
725 |
|
701 |
// optional gamma chunk is strongly suggested if you have any guess |
726 |
// optional gamma chunk is strongly suggested if you have any guess |
702 |
// as to the correct gamma of the image |
727 |
// as to the correct gamma of the image |
Lines 708-715
TRACEUSER( "Jonathan", _T("PNG write: bit_depth = %d color_type = %d\n"),info_pt
Link Here
|
708 |
// write the file information |
733 |
// write the file information |
709 |
png_write_info(png_ptr, info_ptr); |
734 |
png_write_info(png_ptr, info_ptr); |
710 |
|
735 |
|
711 |
TRACEUSER( "Jonathan", _T("PNG write: pixel_depth %d channels %d\n"),png_ptr->pixel_depth, png_ptr->channels); |
736 |
int _channels = png_get_channels (png_ptr, info_ptr); |
712 |
TRACEUSER( "Jonathan", _T("PNG write: rowbytes %d color_type %d\n"),png_ptr->rowbytes, png_ptr->color_type); |
737 |
int _pixel_depth = _channels * _bit_depth; |
|
|
738 |
TRACEUSER( "Jonathan", _T("PNG write: pixel_depth %d channels %d\n"),_pixel_depth, _channels); |
739 |
TRACEUSER( "Jonathan", _T("PNG write: rowbytes %d color_type %d\n"),png_get_rowbytes(png_ptr,info_ptr), _color_type); |
713 |
// Set up the transformations you want. |
740 |
// Set up the transformations you want. |
714 |
// Note: that these are all optional. Only call them if you want them |
741 |
// Note: that these are all optional. Only call them if you want them |
715 |
|
742 |
|
Lines 775-795
BOOL OutputPNG::CleanUpPngStructures()
Link Here
|
775 |
// If our structures are present then clean them out |
802 |
// If our structures are present then clean them out |
776 |
if (png_ptr) |
803 |
if (png_ptr) |
777 |
{ |
804 |
{ |
778 |
if (info_ptr) |
|
|
779 |
{ |
780 |
// They do not seem to have catered for the palette and transparency structures |
781 |
if (info_ptr->palette) |
782 |
{ |
783 |
CCFree(info_ptr->palette); |
784 |
info_ptr->palette = NULL; |
785 |
} |
786 |
if (info_ptr->trans) |
787 |
{ |
788 |
CCFree(info_ptr->trans); |
789 |
info_ptr->trans = NULL; |
790 |
} |
791 |
} |
792 |
|
793 |
// clean up after the write, and free any memory allocated |
805 |
// clean up after the write, and free any memory allocated |
794 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
806 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
795 |
png_ptr = NULL; |
807 |
png_ptr = NULL; |