1 /*	png.h
2 	Copyright (C) 2004-2019 Mark Tyler and Dmitry Groshev
3 
4 	This file is part of mtPaint.
5 
6 	mtPaint is free software; you can redistribute it and/or modify
7 	it under the terms of the GNU General Public License as published by
8 	the Free Software Foundation; either version 3 of the License, or
9 	(at your option) any later version.
10 
11 	mtPaint is distributed in the hope that it will be useful,
12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 	GNU General Public License for more details.
15 
16 	You should have received a copy of the GNU General Public License
17 	along with mtPaint in the file COPYING.
18 */
19 
20 //	Loading/Saving errors
21 
22 #define WRONG_FORMAT -10
23 #define TOO_BIG -11
24 #define EXPLODE_FAILED -12
25 
26 #define FILE_LIB_ERROR  0xBAD01
27 #define FILE_MEM_ERROR  0xBAD02
28 #define FILE_HAS_FRAMES 0xBAD03
29 #define FILE_HAS_ANIM   0xBAD04
30 #define FILE_TOO_LONG   0xBAD05
31 #define FILE_EXP_BREAK  0xBAD06
32 
33 /* File types */
34 enum {
35 	FT_NONE = 0,
36 	FT_PNG,
37 	FT_JPEG,
38 	FT_JP2,
39 	FT_J2K,
40 	FT_TIFF,
41 	FT_GIF,
42 	FT_BMP,
43 	FT_XPM,
44 	FT_XBM,
45 	FT_LSS,
46 	FT_TGA,
47 	FT_PCX,
48 	FT_PBM,
49 	FT_PGM,
50 	FT_PPM,
51 	FT_PAM,
52 	FT_GPL,
53 	FT_TXT,
54 	FT_PAL,
55 	FT_ACT,
56 	FT_LAYERS1,
57 	FT_LAYERS2,
58 	FT_PIXMAP,
59 	FT_SVG,
60 	FT_PMM,
61 	FT_WEBP,
62 	FT_LBM,
63 	NUM_FTYPES
64 };
65 
66 #define FTM_FTYPE   0x00FF /* File type mask */
67 #define FTM_EXTEND  0x0100 /* Allow extended format */
68 #define FTM_UNDO    0x0200 /* Allow undoing */
69 #define FTM_FRAMES  0x0400 /* Allow frameset use */
70 
71 /* Primary features supported by file formats */
72 #define FF_BW      0x0001 /* Black and white */
73 #define FF_16      0x0002 /* 16 colors */
74 #define FF_256     0x0004 /* 256 colors */
75 #define FF_IDX     0x0007 /* Indexed image */
76 #define FF_RGB     0x0008 /* Truecolor */
77 #define FF_IMAGE   0x000F /* Image of any kind */
78 #define FF_ANIM    0x0010 /* Animation */
79 #define FF_ALPHAI  0x0020 /* Alpha channel for indexed images */
80 #define FF_ALPHAR  0x0040 /* Alpha channel for RGB images */
81 #define FF_ALPHA   0x0060 /* Alpha channel for all images */
82 #define FF_MULTI   0x0080 /* Multiple channels */
83 #define FF_LAYER   0x0100 /* Layered images */
84 #define FF_PALETTE 0x0200 /* Palette file (not image) */
85 #define FF_RMEM    0x0400 /* Can be read from memory */
86 #define FF_WMEM    0x0800 /* Can be written to memory */
87 #define FF_MEM     0x0C00 /* Both of the above */
88 #define FF_NOSAVE  0x1000 /* Can be read but not written */
89 #define FF_SCALE   0x2000 /* Freely scalable (vector format) */
90 
91 /* Configurable features of file formats */
92 #define XF_TRANS   0x0001 /* Indexed transparency */
93 #define XF_SPOT    0x0002 /* "Hot spot" */
94 #define XF_COMPJ   0x0004 /* JPEG compression */
95 #define XF_COMPZ   0x0008 /* PNG zlib compression */
96 #define XF_COMPR   0x0010 /* RLE compression */
97 #define XF_COMPJ2  0x0020 /* JPEG2000 compression */
98 #define XF_COMPZT  0x0040 /* TIFF deflate compression */
99 #define XF_COMPLZ  0x0080 /* TIFF LZMA2 compression */
100 #define XF_COMPWT  0x0100 /* TIFF WebP compression */
101 #define XF_COMPZS  0x0200 /* TIFF ZSTD compression */
102 #define XF_COMPT   0x0400 /* TIFF selectable compression */
103 #define XF_COMPV8  0x0800 /* WebP V8 compression */
104 #define XF_COMPV8L 0x1000 /* WebP V8L compression */
105 #define XF_COMPW   0x2000 /* WebP selectable compression */
106 #define XF_COMPRL  0x4000 /* LBM RLE compression */
107 #define XF_PBM     0x8000 /* "Planar" LBM, aka PBM */
108 
109 #define FF_SAVE_MASK (mem_img_bpp == 3 ? FF_RGB : mem_cols > 16 ? FF_256 : \
110 	mem_cols > 2 ? FF_16 | FF_256 : FF_IDX)
111 #define FF_SAVE_MASK_FOR(X) ((X).bpp == 3 ? FF_RGB : (X).colors > 16 ? FF_256 : \
112 	(X).colors > 2 ? FF_16 | FF_256 : FF_IDX)
113 
114 /* Animation loading modes */
115 #define ANM_PAGE    0 /* No animation (pages as written) */
116 #define ANM_RAW     1 /* Raw frames (as written) */
117 #define ANM_COMP    2 /* Composited frames (as displayed) */
118 #define ANM_NOZERO  3 /* Composited frames with nonzero delays (as seen) */
119 
120 #define LONGEST_EXT 5
121 
122 typedef struct {
123 	char *name, *ext, *ext2;
124 	unsigned int flags, xflags;
125 } fformat;
126 
127 extern fformat file_formats[];
128 
129 #define TIFF_MAX_TYPES 11 /* Enough for NULL-terminated list of them all */
130 
131 typedef struct {
132 	char *name;
133 	int id;
134 	unsigned int flags, xflags;
135 	int pflag;
136 } tiff_format;
137 
138 extern tiff_format tiff_formats[];
139 
140 int tiff_lzma, tiff_zstd; /* LZMA2 & ZSTD compression supported */
141 
142 extern char *webp_presets[];
143 
144 /* All-in-one transport container for save/load */
145 typedef struct {
146 	/* Configuration data */
147 	int mode, ftype;
148 	int xpm_trans;
149 	int hot_x, hot_y;
150 	int req_w, req_h; // Size request for scalable formats
151 	int jpeg_quality;
152 	int png_compression;
153 	int lzma_preset;
154 	int zstd_level;
155 	int tiff_type;
156 	int tga_RLE;
157 	int jp2_rate;
158 	int webp_preset, webp_quality, webp_compression;
159 	int lbm_pack, lbm_pbm;
160 	int gif_delay;
161 	int rgb_trans;
162 	int silent;
163 	/* Image data */
164 	chanlist img;
165 	png_color *pal;
166 	int width, height, bpp, colors;
167 	int x, y;
168 	/* Extra data */
169 	int icc_size;
170 	char *icc;
171 } ls_settings;
172 
173 int silence_limit, jpeg_quality, png_compression;
174 int tga_RLE, tga_565, tga_defdir, jp2_rate;
175 int lzma_preset, zstd_level, tiff_predictor, tiff_rtype, tiff_itype, tiff_btype;
176 int webp_preset, webp_quality, webp_compression;
177 int lbm_mask, lbm_untrans, lbm_pack, lbm_pbm;
178 int apply_icc;
179 
180 int file_type_by_ext(char *name, guint32 mask);
181 
182 int save_image(char *file_name, ls_settings *settings);
183 int save_mem_image(unsigned char **buf, int *len, ls_settings *settings);
184 
185 int load_image(char *file_name, int mode, int ftype);
186 int load_mem_image(unsigned char *buf, int len, int mode, int ftype);
187 int load_image_scale(char *file_name, int mode, int ftype, int w, int h);
188 
189 // !!! The only allowed mode for now is FS_LAYER_LOAD
190 int load_frameset(frameset *frames, int ani_mode, char *file_name, int mode,
191 	int ftype);
192 int explode_frames(char *dest_path, int ani_mode, char *file_name, int ftype,
193 	int desttype);
194 
195 int export_undo(char *file_name, ls_settings *settings);
196 int export_ascii ( char *file_name );
197 
198 int detect_file_format(char *name, int need_palette);
199 #define detect_image_format(X) detect_file_format(X, FALSE)
200 #define detect_palette_format(X) detect_file_format(X, TRUE)
201 
202 int valid_file(char *filename);		// Can this file be opened for reading?
203 
204 /* Compression levels range for ZSTD */
205 #define ZSTD_MIN 1
206 #define ZSTD_MAX 22
207 
208 #ifdef U_TIFF
209 void init_tiff_formats();	// Check what libtiff can handle
210 #endif
211