File indexing completed on 2025-03-09 04:09:54

0001 /* Generic functions and macros for reading XCF files
0002  *
0003  * This file was written by Henning Makholm <henning@makholm.net>
0004  * It is hereby in the public domain.
0005  * 
0006  * In jurisdictions that do not recognise grants of copyright to the
0007  * public domain: I, the author and (presumably, in those jurisdictions)
0008  * copyright holder, hereby permit anyone to distribute and use this code,
0009  * in source code or binary form, with or without modifications. This
0010  * permission is world-wide and irrevocable.
0011  *
0012  * Of course, I will not be liable for any errors or shortcomings in the
0013  * code, since I give it away without asking any compenstations.
0014  *
0015  * If you use or distribute this code, I would appreciate receiving
0016  * credit for writing it, in whichever way you find proper and customary.
0017  */
0018 
0019 #ifndef XCFTOOLS_H
0020 #define XCFTOOLS_H
0021 
0022 #include "config.h"
0023 #include "enums.h"
0024 #include <stddef.h>
0025 #include <stdio.h>
0026 
0027 #if defined(HAVE_GETTEXT) && defined(ENABLE_NLS)
0028 #include <libintl.h>
0029 #define _(s) gettext(s)
0030 void nls_init(void);
0031 #else
0032 #define _(s) (s)
0033 #define nls_init() (void)0
0034 #endif
0035 #define N_(s) (s)
0036 
0037 #if HAVE_INTTYPES_H
0038 # define __STDC_FORMAT_MACROS
0039 # include <inttypes.h>
0040 #else
0041 /* These legacy fall-backs will probably work on every system
0042  * that does not supply a inttypes.h ... */
0043 typedef unsigned char     uint8_t ;
0044 typedef unsigned long int uint32_t, uintptr_t;
0045 typedef signed char       int8_t ;
0046 typedef signed long int   int32_t ;
0047 # define PRIX32 "lX"
0048 # define PRIu32 "lu"
0049 # define PRIXPTR "lX"
0050 #endif
0051 
0052 #if __GNUC__
0053 # define __ATTRIBUTE__ __attribute__
0054 #else
0055 # define __ATTRIBUTE__(x)
0056 #endif
0057 
0058 #ifdef _WIN32
0059 #include <winsock2.h>
0060 #elif HAVE_NETINET_IN_H
0061 #include <netinet/in.h>
0062 #elif HAVE_ARPA_INET_H
0063 # include <arpa/inet.h>
0064 #elif WORDS_BIGENDIAN
0065 # define ntohl(x) (x)
0066 #else
0067 static inline uint32_t ntohl(uint32_t a) {
0068   return (a << 24) + ((a & 0xFF00) << 8) + ((a >> 8) & 0xFF00) + (a >> 24) ;
0069 }
0070 #endif
0071 
0072 #ifndef HAVE_STRCASECMP
0073 #define strcasecmp strcmp
0074 #endif
0075 
0076 /* Read a single word value from the XCF file */
0077 
0078 /* Use + instead of | because that allows LEA instructions */
0079 #define xcfBE(a) ( ((uint32_t)xcf_file[(a)  ] << 24) + \
0080                    ((uint32_t)xcf_file[(a)+1] << 16) + \
0081                    ((uint32_t)xcf_file[(a)+2] << 8 ) + \
0082                    ((uint32_t)xcf_file[(a)+3]      ) )
0083 #define xcfLE(a) ( ((uint32_t)xcf_file[(a)  ]      ) + \
0084                    ((uint32_t)xcf_file[(a)+1] << 8 ) + \
0085                    ((uint32_t)xcf_file[(a)+2] << 16) + \
0086                    ((uint32_t)xcf_file[(a)+3] << 24) )
0087 
0088 #if defined(CAN_DO_UNALIGNED_WORDS)
0089 # define xcfL(a) ntohl(*(uint32_t *)(xcf_file + (a)))
0090 #else
0091 # define xcfL(a) ((a) & 3 ? xcfBE(a) : ntohl(*(uint32_t *)(xcf_file + (a))))
0092 #endif
0093 
0094 /* ****************************************************************** */
0095 
0096 /* The following are exported from am OS-specific source file;
0097  * io-unix.c on unixish systems.
0098  */
0099 void read_or_mmap_xcf(const char* filename, const char *unzipper);
0100 void free_or_close_xcf(void);
0101 
0102 /* ****************************************************************** */
0103 /* utils.c */
0104 
0105 #define XCF_ERROR 1
0106 #define XCF_OK 0
0107 #define XCF_PTR_EMPTY 0
0108 
0109 extern const char *progname ;
0110 extern int verboseFlag ;
0111 
0112 void *xcfmalloc(size_t size);
0113 void xcffree(void*);
0114 
0115 void FatalGeneric(int status,const char* format,...)
0116      __ATTRIBUTE__((format(printf,2,3))) ;
0117 void FatalUnexpected(const char* format,...)
0118      __ATTRIBUTE__((format(printf,1,2))) ;
0119 void FatalBadXCF(const char* format,...)
0120      __ATTRIBUTE__((format(printf,1,2))) ;
0121 void FatalUnsupportedXCF(const char* format,...)
0122      __ATTRIBUTE__((format(printf,1,2))) ;
0123 
0124 void gpl_blurb(void);
0125      
0126 FILE* openout(const char*);
0127 int closeout(FILE *,const char*);
0128 
0129 struct rect {
0130   int t, b, l, r ;
0131 };
0132 
0133 #define isSubrect(A,B) \
0134   ((A).l >= (B).l && (A).r <= (B).r && (A).t >= (B).t && (A).b <= (B).b)
0135 #define disjointRects(A,B) \
0136   ((A).l >= (B).r || (A).r <= (B).l || (A).t >= (B).b || (A).b <= (B).t)
0137 
0138 /* ****************************************************************** */
0139 /* xcf-general.c */
0140 
0141 extern uint8_t *xcf_file ;
0142 extern size_t xcf_length ;
0143 extern int use_utf8 ;
0144 
0145 int xcfCheckspace(uint32_t addr,int spaceafter, const char *format,...)
0146      __ATTRIBUTE__((format(printf,3,4)));
0147 int xcfOffset(uint32_t addr,int spaceafter, uint32_t* apparent);
0148 
0149 int xcfNextprop(uint32_t *master,uint32_t *body, PropType* type);
0150 const char* xcfString(uint32_t ptr,uint32_t *after);
0151 
0152 /* These are hardcoded in the Gimp sources: */
0153 #define TILE_SHIFT 6
0154 #define TILE_WIDTH (1<<TILE_SHIFT)
0155 #define TILE_HEIGHT (1<<TILE_SHIFT)
0156 /* These definitions of TILE_LEFT and TILE_TOP work correctly for negative
0157  * numbers, but on the other hand depend on TILE_WIDTH and TILE_HEIGHT
0158  * being powers of 2. That's okay, because the tile size cannot change
0159  * anyway.
0160  */
0161 #define TILE_LEFT(x) ((x) & -TILE_WIDTH)
0162 #define TILE_TOP(y) ((y) & -TILE_HEIGHT)
0163 #define TILE_NUM(x) ((x) >> TILE_SHIFT)
0164 
0165 struct tileDimensions {
0166   struct rect c ;
0167   unsigned width, height ;
0168   unsigned tilesx, tilesy ;
0169   unsigned ntiles ;
0170 };
0171 /* computeDimensions assumes that width, height, c.l, and c.t are set */
0172 int computeDimensions(struct tileDimensions *);
0173 
0174 struct xcfTiles {
0175   const struct _convertParams *params ;
0176   uint32_t *tileptrs ;
0177   uint32_t hierarchy ;
0178 };
0179 
0180 struct xcfLayer {
0181   struct tileDimensions dim ;
0182   const char *name ;
0183   GimpLayerModeEffects mode ;
0184   GimpImageType type ;
0185   unsigned int opacity ;
0186   int isVisible, hasMask ;
0187   uint32_t propptr ;
0188   struct xcfTiles pixels ;
0189   struct xcfTiles mask ;
0190   int isGroup ;
0191   unsigned pathLength ;
0192   unsigned *path ;
0193 }; 
0194 
0195 extern struct xcfImage {
0196   int version ;
0197   unsigned width, height ;
0198   GimpImageBaseType type ;
0199   XcfCompressionType compression ;
0200   int numLayers ;
0201   struct xcfLayer *layers ;
0202   uint32_t colormapptr ;
0203 } XCF ;
0204 
0205 int getBasicXcfInfo(void);
0206 
0207 #endif /* XCFTOOLS_H */