1/*
2File:       icns.h
3Copyright (C) 2001-2012 Mathew Eis <mathew@eisbox.net>
4Copyright (C) 2002 Chenxiao Zhao <chenxiao.zhao@gmail.com>
5
6With the exception of the limited portions mentiond, this library
7is free software; you can redistribute it and/or modify it under
8the terms of the GNU Lesser General Public License as published
9by the Free Software Foundation; either version 2.1 of the License,
10or (at your option) any later version.
11
12This library is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public
18License along with this library; if not, write to the
19Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA.
21*/
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <stdint.h>
26#include <string.h>
27
28#include "icns.h"
29
30#include "config.h"
31
32#ifndef _ICNS_INTERNALS_H_
33#define	_ICNS_INTERNALS_H_
34
35/*  Compile-time variables   */
36/*  These should really be set from the Makefile */
37
38// Enable debugging messages?
39// #define	ICNS_DEBUG	1
40
41// Enable supprt for 256x256, 512x512 and 1024x1024 icons
42// Use either Jasper or OpenJPEG, but not both
43// #define	ICNS_JASPER
44// #define	ICNS_OPENJPEG
45
46/* Make sure we're not using both libraries */
47#if defined(ICNS_JASPER) && defined(ICNS_OPENJPEG)
48	#error "Must use either Jasper or OpenJPEG, but not both!"
49#endif
50
51/*  Include Jasper headers   */
52#ifdef ICNS_JASPER
53#include <jasper.h>
54#endif
55
56/*  Include OpenJPEG headers   */
57#ifdef ICNS_OPENJPEG
58#include <openjpeg.h>
59#endif
60
61// We do not want to expose any of the internal stuff
62#pragma GCC visibility push(hidden)
63
64/* icns structures */
65
66typedef struct icns_rgba_t
67{
68	icns_byte_t	 r;
69	icns_byte_t	 g;
70	icns_byte_t	 b;
71	icns_byte_t	 a;
72} icns_rgba_t;
73
74typedef struct icns_argb_t
75{
76	icns_byte_t	 a;
77	icns_byte_t	 r;
78	icns_byte_t	 g;
79	icns_byte_t	 b;
80} icns_argb_t;
81
82typedef struct icns_rgb_t
83{
84	icns_byte_t	 r;
85	icns_byte_t	 g;
86	icns_byte_t	 b;
87} icns_rgb_t;
88
89/* icns constants */
90
91
92typedef enum icns_rsrc_endian_t
93{
94	ICNS_BE_RSRC = 0,
95	ICNS_LE_RSRC = 1
96} icns_rsrc_endian_t;
97
98#define	ICNS_BYTE_BITS	                  8
99
100#define ICNS_APPLE_SINGLE_MAGIC           0x00051600
101#define	ICNS_APPLE_DOUBLE_MAGIC           0x00051607
102
103#define	ICNS_APPLE_ENC_DATA               1
104#define	ICNS_APPLE_ENC_RSRC               2
105
106/* icns macros */
107
108/*
109These functions swap the position of the alpha channel
110*/
111static inline icns_rgba_t ICNS_ARGB_TO_RGBA(icns_argb_t pxin) {
112	icns_rgba_t pxout;
113	pxout.r = pxin.r;
114	pxout.g = pxin.g;
115	pxout.b = pxin.b;
116	pxout.a = pxin.a;
117	return pxout;
118}
119
120static inline icns_argb_t ICNS_RGBA_TO_ARGB(icns_rgba_t pxin) {
121	icns_argb_t pxout;
122	pxout.r = pxin.r;
123	pxout.g = pxin.g;
124	pxout.b = pxin.b;
125	pxout.a = pxin.a;
126	return pxout;
127}
128
129/*
130These macros will work on systems that support unaligned
131accesses, as well as those that don't support it.
132Unfortunately, gcc doesn't support unaligned access well
133with memcpy on some architectures due to a combination of
134memcpy being inlined during the optimization process and
135memory alignment. So, we try to work around this here.
136*/
137
138// If the autotools didn't tell us, try and make a good guess
139#ifndef HAVE_UNALIGNED_MEMCPY
140 #if defined(__GNUC__) && (defined(__arm__) || defined(__thumb__) || defined(__sparc__))
141  #define HAVE_UNALIGNED_MEMCPY 0
142 #else
143  #define HAVE_UNALIGNED_MEMCPY 1
144 #endif
145#endif
146
147// Set up the macros
148#if HAVE_UNALIGNED_MEMCPY == 0
149 __attribute__ ((noinline)) void *icns_memcpy( void *dst, const void *src, size_t num );
150 #define ICNS_READ_UNALIGNED(val, addr, size)        icns_memcpy(&(val), (addr), size)
151 #define ICNS_WRITE_UNALIGNED(addr, val, size)       icns_memcpy((addr), &(val), size)
152#else
153 #define ICNS_READ_UNALIGNED(val, addr, size)        memcpy(&(val), (addr), size)
154 #define ICNS_WRITE_UNALIGNED(addr, val, size)       memcpy((addr), &(val), size)
155#endif
156
157/* global variables */
158extern icns_bool_t gShouldPrintErrors;
159
160/* icns function prototypes */
161
162// icns_debug.c
163void bin_print_byte(int x);
164void bin_print_int(int x);
165
166// icns_element.c
167int icns_new_element_from_image_or_mask(icns_image_t *imageIn,icns_type_t iconType,icns_bool_t isMask,icns_element_t **iconElementOut);
168int icns_update_element_with_image_or_mask(icns_image_t *imageIn,icns_bool_t isMask,icns_element_t **iconElement);
169
170// icns_io.c
171int icns_parse_family_data(icns_size_t dataSize,icns_byte_t *data,icns_family_t **iconFamilyOut);
172int icns_find_family_in_mac_resource(icns_size_t resDataSize, icns_byte_t *resData, icns_rsrc_endian_t fileEndian, icns_family_t **dataOut);
173int icns_read_macbinary_resource_fork(icns_size_t dataSize,icns_byte_t *dataPtr,icns_type_t *dataTypeOut, icns_type_t *dataCreatorOut,icns_size_t *parsedResSizeOut,icns_byte_t **parsedResDataOut);
174int icns_read_apple_encoded_resource_fork(icns_size_t dataSize,icns_byte_t *dataPtr,icns_type_t *dataTypeOut, icns_type_t *dataCreatorOut,icns_size_t *parsedResSizeOut,icns_byte_t **parsedResDataOut);
175icns_bool_t icns_icns_header_check(icns_size_t dataSize,icns_byte_t *dataPtr);
176icns_bool_t icns_rsrc_header_check(icns_size_t dataSize,icns_byte_t *dataPtr,icns_rsrc_endian_t fileEndian);
177icns_bool_t icns_macbinary_header_check(icns_size_t dataSize,icns_byte_t *dataPtr);
178icns_bool_t icns_apple_encoded_header_check(icns_size_t dataSize,icns_byte_t *dataPtr);
179
180// icns_png.c
181int icns_image_to_png(icns_image_t *image, icns_size_t *dataSizeOut, icns_byte_t **dataPtrOut);
182int icns_png_to_image(icns_size_t dataSize, icns_byte_t *dataPtr, icns_image_t *imageOut);
183
184// icns_jp2.c
185#ifdef ICNS_JASPER
186int icns_jas_jp2_to_image(icns_size_t dataSize, icns_byte_t *dataPtr, icns_image_t *imageOut);
187int icns_jas_image_to_jp2(icns_image_t *image, icns_size_t *dataSizeOut, icns_byte_t **dataPtrOut);
188#endif
189#ifdef ICNS_OPENJPEG
190int icns_opj_jp2_to_image(icns_size_t dataSize, icns_byte_t *dataPtr, icns_image_t *imageOut);
191int icns_opj_jp2_dec(icns_size_t dataSize, icns_byte_t *dataPtr, opj_image_t **imageOut);
192int icns_opj_to_image(opj_image_t *image, icns_image_t *outIcon);
193int icns_opj_image_to_jp2(icns_image_t *image, icns_size_t *dataSizeOut, icns_byte_t **dataPtrOut);
194void icns_opj_error_callback(const char *msg, void *client_data);
195void icns_opj_warning_callback(const char *msg, void *client_data);
196void icns_opj_info_callback(const char *msg, void *client_data);
197#endif
198void icns_place_jp2_cdef(icns_byte_t *dataPtr, icns_size_t dataSize);
199
200// icns_utils.c
201icns_uint32_t icns_get_element_order(icns_type_t iconType);
202void icns_print_err(const char *template, ...);
203
204// Stop hiding symbols
205#pragma GCC visibility pop
206
207#endif /* _ICNS_INTERNALS_H_ */
208