~ubuntu-branches/ubuntu/vivid/libicns/vivid

« back to all changes in this revision

Viewing changes to src/icns_png.c

  • Committer: Package Import Robot
  • Author(s): Paul Wise, Mathew Eis, Paul Wise
  • Date: 2012-06-14 10:16:49 UTC
  • mfrom: (1.1.3)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: package-import@ubuntu.com-20120614101649-7e9vuqyd686l0h3z
[ Mathew Eis ]
* New upstream release
  - No longer crashes on ARM/SPARC (Closes: #612698)
  - Understands 'odrp' type (Closes: #614031)
  - Fails with non-zero return code (Closes: #614028)
  - Fixes FTBFS with newer libpng (Closes: #635952)
* debian/control
  - Bump to 3.9.3 Standards-Version (no change needed).

[ Paul Wise ]
* Include changes from Debian NMU
* Wrap and sort various files using wrap-and-sort -s
* Compile against libjpeg-dev instead of the old version (Closes: #643316)
* Compile against libpng-dev instead of the old version (Closes: #662403)
* Adjust the watch file to only look for release versions
* Switch to debhelper compat 9 and dh tiny rules
* Drop autotools_dev and use dh-autoreconf
* Switch to dpkg-source v3
* Use multi-arch paths and fields
* Add myself to uploaders
* Add Vcs-* headers
* Also install the API docs, drop from lib package
* Switch to Debian copyright format 1.0 for the source Debian copyright file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
File:       icns_png.c
 
3
Copyright (C) 2001-2012 Mathew Eis <mathew@eisbox.net>
 
4
 
 
5
This library is free software; you can redistribute it and/or
 
6
modify it under the terms of the GNU Lesser General Public
 
7
License as published by the Free Software Foundation; either
 
8
version 2.1 of the License, or (at your option) any later version.
 
9
 
 
10
This library is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
Lesser General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU Lesser General Public
 
16
License along with this library; if not, write to the
 
17
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 
18
Boston, MA 02110-1301, USA.
 
19
*/
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <stdint.h>
 
24
#include <string.h>
 
25
 
 
26
#include "icns.h"
 
27
#include "icns_internals.h"
 
28
 
 
29
#include <png.h>
 
30
 
 
31
typedef struct icns_png_io_ref {
 
32
        void*   data;
 
33
        size_t  size;
 
34
        size_t  offset;
 
35
} icns_png_io_ref;
 
36
 
 
37
static void icns_png_read_memory(png_structp png_ptr, png_bytep data, png_size_t length) {
 
38
        icns_png_io_ref* _ref = (icns_png_io_ref*) png_get_io_ptr( png_ptr );
 
39
        memcpy( data, (char*)_ref->data + _ref->offset, length );
 
40
        _ref->offset += length;
 
41
}
 
42
 
 
43
int icns_png_to_image(icns_size_t dataSize, icns_byte_t *dataPtr, icns_image_t *imageOut)
 
44
{
 
45
        int error = ICNS_STATUS_OK;
 
46
        png_structp png_ptr = NULL;
 
47
        png_infop info_ptr = NULL;
 
48
        png_uint_32 w;
 
49
        png_uint_32 h;
 
50
        png_bytep *rows;
 
51
        int bit_depth;
 
52
        int32_t color_type;
 
53
        int row;
 
54
        int rowsize;
 
55
        
 
56
        
 
57
        if(dataPtr == NULL)
 
58
        {
 
59
                icns_print_err("icns_png_to_image: JP2 data is NULL!\n");
 
60
                return ICNS_STATUS_NULL_PARAM;
 
61
        }
 
62
        
 
63
        if(imageOut == NULL)
 
64
        {
 
65
                icns_print_err("icns_png_to_image: Image out is NULL!\n");
 
66
                return ICNS_STATUS_NULL_PARAM;
 
67
        }
 
68
        
 
69
        if(dataSize == 0)
 
70
        {
 
71
                icns_print_err("icns_png_to_image: Invalid data size! (%d)\n",dataSize);
 
72
                return ICNS_STATUS_INVALID_DATA;
 
73
        }
 
74
        
 
75
        #ifdef ICNS_DEBUG
 
76
        printf("Decoding PNG image...\n");
 
77
        #endif
 
78
 
 
79
        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
 
80
 
 
81
        if(png_ptr == NULL) {
 
82
                return ICNS_STATUS_NO_MEMORY;
 
83
        }
 
84
 
 
85
        info_ptr = png_create_info_struct(png_ptr);
 
86
 
 
87
        if(info_ptr == NULL) {
 
88
           png_destroy_read_struct(&png_ptr, NULL, NULL);
 
89
           return ICNS_STATUS_NO_MEMORY;
 
90
        }
 
91
 
 
92
        if (setjmp(png_jmpbuf(png_ptr)))
 
93
        {
 
94
                png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 
95
                return ICNS_STATUS_INVALID_DATA;
 
96
        }
 
97
 
 
98
        // set libpng to read from memory
 
99
        icns_png_io_ref io_data = { dataPtr, dataSize, 0 };
 
100
        png_set_read_fn(png_ptr, (void *)&io_data, &icns_png_read_memory); 
 
101
        
 
102
        png_read_info(png_ptr, info_ptr);
 
103
        png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, NULL, NULL, NULL);
 
104
 
 
105
        switch (color_type)
 
106
        {
 
107
                case PNG_COLOR_TYPE_RGB:
 
108
                        if (bit_depth == 16) {
 
109
                                png_set_strip_16(png_ptr);
 
110
                                bit_depth = 8;
 
111
                        }
 
112
 
 
113
                        png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
 
114
                        break;
 
115
 
 
116
                case PNG_COLOR_TYPE_RGB_ALPHA:
 
117
                        if (bit_depth == 16) {
 
118
                                png_set_strip_16(png_ptr);
 
119
                                bit_depth = 8;
 
120
                        }
 
121
 
 
122
                        break;
 
123
        }
 
124
        
 
125
        png_read_update_info(png_ptr, info_ptr);
 
126
        
 
127
        rowsize = png_get_rowbytes(png_ptr, info_ptr);
 
128
        rows = malloc (sizeof(png_bytep) * h);
 
129
 
 
130
        imageOut->imageWidth = w;
 
131
        imageOut->imageHeight = h;
 
132
        imageOut->imageChannels = 4;
 
133
        imageOut->imagePixelDepth = 8;
 
134
        imageOut->imageDataSize = w * h * 4;
 
135
        imageOut->imageData = malloc( rowsize * h + 8 );
 
136
 
 
137
        if(imageOut->imageData == NULL) {
 
138
                png_destroy_read_struct(&png_ptr, NULL, NULL);
 
139
                free(rows);
 
140
                return ICNS_STATUS_NO_MEMORY;
 
141
        }
 
142
        
 
143
        rows[0] = imageOut->imageData;
 
144
        for (row = 1; row < h; row++) {
 
145
                rows[row] = rows[row-1] + rowsize;
 
146
        }
 
147
 
 
148
        png_read_image(png_ptr, rows);
 
149
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
 
150
 
 
151
        free(rows);
 
152
 
 
153
        #ifdef ICNS_DEBUG
 
154
        if(error == ICNS_STATUS_OK) {
 
155
                printf("  decode result:\n");
 
156
                printf("  width is: %d\n",imageOut->imageWidth);
 
157
                printf("  height is: %d\n",imageOut->imageHeight);
 
158
                printf("  channels are: %d\n",imageOut->imageChannels);
 
159
                printf("  pixel depth is: %d\n",imageOut->imagePixelDepth);
 
160
                printf("  data size is: %d\n",(int)imageOut->imageDataSize);
 
161
        } else {
 
162
                printf("  decode failed.\n");
 
163
        }
 
164
        #endif
 
165
        
 
166
        return error;
 
167
}
 
168
 
 
169
 
 
170
int icns_image_to_png(icns_image_t *image, icns_size_t *dataSizeOut, icns_byte_t **dataPtrOut)
 
171
{
 
172
        int error = ICNS_STATUS_OK;
 
173
        
 
174
        if(image == NULL)
 
175
        {
 
176
                icns_print_err("icns_image_to_png: Image is NULL!\n");
 
177
                return ICNS_STATUS_NULL_PARAM;
 
178
        }
 
179
        
 
180
        if(dataSizeOut == NULL)
 
181
        {
 
182
                icns_print_err("icns_image_to_png: Data size NULL!\n");
 
183
                return ICNS_STATUS_NULL_PARAM;
 
184
        }
 
185
        
 
186
        if(dataPtrOut == NULL)
 
187
        {
 
188
                icns_print_err("icns_image_to_png: Data ref is NULL!\n");
 
189
                return ICNS_STATUS_NULL_PARAM;
 
190
        }
 
191
        
 
192
        #ifdef ICNS_DEBUG
 
193
        printf("Encoding PNG image...\n");
 
194
        #endif
 
195
        
 
196
        // TODO:
 
197
        // Verify that size is > 256x256 and 32-bit
 
198
        // Then convert an icns_image to PNG data
 
199
        return error;
 
200
}
 
201