The Baby X resource compiler source files

Source file list

  • aifffile.c
  • asciitostring.c - AIFF file loader
  • babyxrcmain.c - program main function
  • bbx_utf8.c - UTF-8 utility functions
  • bdf2c.c - BDF font converter
  • bmp.c - bitmap file loader / saver
  • csv.c - comma-separated values file loader
  • dumpcsv.c - outputs csv file as C source
  • gif.c - GIF file loader / saver
  • jpeg.c - JPEG file loader
  • loadasutf8.c - loads text files as UTF-8
  • loadcursor.c - Microsoft CUR file loader
  • loadimage.c - high level image loader
  • loadmp3.c - loads MP3 files as raw audio
  • loadtiff.c - TIFF file loader
  • lodepng.c - PNG file loader / saver
  • minimp3.c - MP3 decoder
  • options.c - commandline option parser
  • rbtree.c - red black tree
  • resize.c - image resizing functions
  • savejpeg.c - JPEG file saver
  • text_encoding_detect.c - detects encoding of a text file
  • ttf2c.c - true type font converter
  • wavfile.c - WAV audio file loader / saver
  • xmlparser.c - XML file loader
  • nanosvg.h - SVG file parser
  • nanosvgrast.h - SVG file rasterizer
  • freetype/ - code to manipulate truetype fonts
  • samplerate/ - code to resample audio
  • aifffile.c

    Copyright: Malcolm McLean

    Code to load an AIFF file. It loads 16 bit uncompresed AIFF files only. However it does support big-endian AIFF files, which are technically AIFC files.

    Public functions

    
            short *loadaiff(const char *fname, long *samplerate, int *Nchannels, long
                *Nsamples);
            short *floadaiff(FILE *fp, long *samplerate, int *Nchannels, long
                *Nsamples);
            

    loadaiff - loads an AIFF file in 16-but itnerleaved PPM format.

    floadaiff - the same fucntion but taking an open stream as an argument.


    asciitostring.c

    Copyright: Malcolm McLean

    Converts an ASCII string to an escaped C string suitable for using in C source code.

    Public functions

    
            char *fslurp(FILE *fp);
            int validcstring(const char *str);
            char *texttostring(const char *str);
            

    fslurp Probably shouldn't be in here. Loads a text file from a stream.

    validcstring Test if a string is a valid escaped C literal.

    texttostring Escapes text and returns a C string literal enclosed in quotes. If the string is long it will be broken up into separate quoted sections separated by newlines.


    babyxrcmain.c

    Copyright: Malcolm McLean

    The main function of the Baby X resource compiler.


    bbx_utf8.c

    Copyright: Malcolm McLean

    Utility functions for manipulating UTF-8. Taken from Baby X.

    Public functions

    
            int bbx_isutf8z(const char *str);
            int bbx_utf8_skip(const char *utf8);
            int bbx_utf8_getch(const char *utf8);
            int bbx_utf8_putch(char *out, int ch);
            int bbx_utf8_charwidth(int ch);
            int bbx_utf8_Nchars(const char *utf8);
            

    bbx_isutf8z - test if a string is valid UTF-8.

    bbx_utf8_skip - get the number of bytes to skip to move to the next Unicode code point.

    bbx_utf8_getch - get the Unicode code point at the front of the UTF-8 buffer.

    bbx_utf8_putch - write a code point to a UTF-8 buffer. Returns the number of bytes written.

    bbx_utf8_charwidth - get the number of UTF-8 bytes needed to represent a Unicode code point.

    bbx_utf8_Nchars - get the number of Unicode code points in a UTF-8 nul-terminated buffer.

    See using UTF-8.


    bdf2c.c

    Copyright: Malcolm McLean

    This code loads a .bdf font and converts it to a bitmap_font structure.

    
            void ReadBdf(FILE * bdf, FILE * out, int header, const char *name);
            

    ReadBdf - a misnomer. It reads a BDF font from a stream, and writes C source to out. Pass header = true to just write the C header, and name for the name of the bitmap_font structure.


    bmp.c

    Copyright: Malcolm McLean

    Code to load and save Microsoft bitmap .bmp files. It won't support absolutely everything (no compressed bitmaps or ones with odd channel masks). However it should load most bitmaps encountered in practice. There are also functions to save.

    Public functions

    
            int bmpgetinfo(char *fname, int *width, int *height);
            unsigned char *loadbmp(char *fname, int *width, int *height);
            unsigned char *loadbmp8bit(char *fname, int *width, int *height,
                unsigned char *pal);
            unsigned char *loadbmp4bit(char *fname, int *width, int *height,
                unsigned char *pal);
            int savebmp(char *fname, unsigned char *rgb, int width, int height);
            int savebmp8bit(char *fname, unsigned char *data, int width, int height,
                unsigned char *pal);
            int savebmp4bit(char *fname, unsigned char *data, int width, int height,
                unsigned char *pal);
            int savebmp1bit(char *fname, unsigned char *data, int width, int height,
                unsigned char *pal);
            

    bmpgetinfo - query a bitmap file for its type. It returns the dimensions, and the bit depth (so 2, 4, 8, 16, 24, or 32). Note that 32 bit bitmaps do not have an alpha channel.

    loadbmp - the function you will normally use. Loads any bitmap as 24 bit RGB data.

    loadbmp8bit - load a bitmap with its palette. pal should be a buffer of 768 bytes for 256 RGB triplets. The bitmap must have depth of 8 bits.

    loadbmp4bit - load a 4 bit bitmap with its palette. pal shoule be a buffer of 48 bytes for 16 RGB triplets. The bitmap must have a depth of 4 bits. It returns the raster data packed in nybles.

    savebmp - save a 24 bit RGB image as a bitmap.

    savebmp8bit - the raster data represents indexes into the palette. which is 256 24-bit RGB entries.

    savebmp4bit - save a colour-indexed image as a 4 bit bitmap. Note that each byte should be in the rnage 0-15 and represent an idex into the 16 entry palette. The function does not accept raser data packed into nybles.

    savebmp1bit - save a binary (1-bit) bitmap. Each byte in the raster represents one pixel and must be 1 or 0. The actual colours are determiend by the two-entry palette. This one hasn't been well-tested, and we need a corresponding save routine. Then it can go inthe binaryimage library.'


    csv.c

    Copyright: Malcolm McLean

    Code to load a comma-separated value file into memory. It attempts to automativally detect the presence of a header. It supports missing values and intricate string escapes, but not non-comma separators. It also determines the type of data in a column.

    Public functions

    
                #define CSV_NULL 0       /* null data */
                #define CSV_REAL 1       /* floating-point data */
                #define CSV_STRING 2     /* string data */
                #define CSV_BOOL 3       /* boolean data */
    
                CSV *loadcsv(const char *fname);
                void killcsv(CSV *csv);
                void csv_getsize(CSV *csv, int *width, int *height);
                int csv_hasdata(CSV *csv, int col, int row);
                double csv_get(CSV *csv, int col, int row);
                const char *csv_getstr(CSV *csv, int col, int row);
                int csv_hasheader(CSV *csv);
                const char *csv_column(CSV *csv, int col, int *type);
            

    loadcsv - loads a CSV object from a file. Returns NULL on fail. It needs a rewrite to improve error handling.

    killcsv - CSV object destructor.

    csv_getsize - get the dimensions of the CDV dataframe in rows (height) and columns (width).

    csv_hasdata - you need to call this before every access to check that the CSV file cell is not empty (missing data).

    csv_get - get numerical data from the CSV object. Note that integers will be returned as doubles.

    csv_getstr - get string data from the CDV file. The string is owned by the CSV object and shouldn't be hung on to.

    csv_hasheader - check is the first row of the CSV data is a header.

    csv_column - query the CSV object for the type of data in a column. Which will be either CSV_REAL or CSV_STRING, except for degenerate csv files with completely empty columns.

    See using CSV.


    dumpcsv.c

    Copyright: Malcolm McLean

    Code to convert a CSV object into C source. If the CSV file is a matrix then the data is written out as a two-dimensional array. Otheerwise it is written out as a dataframe, an array of C structs. The struct field names are determined from the CSV file header.

    Public functions

    
            int dumpcsv(FILE *fpout, int headerfile, const char *name, CSV *csv);
            

    dumpcsv - convert a CSV object to C source code. headerfile means write out a C header file instead of a .c source file (the word "header" would have ben confusing). It should be considered experimental.


    gif.c

    Copyright: Malcolm McLean

    Code to load or save a .GIF image. It depends on rbtree.c and rbtree.h.

    
                unsigned char *loadgif(char *fname, int *width, int *height,
                    unsigned char *pal, int *transparent);
                int savegif(char *fname, unsigned char *data, int width, int height,
                    unsigned char *pal, int palsize, int transparent, int important,
                    int interlaced);
            

    loadgif - load a non-animated GIF file. pal should point to 768 byters for 256 RGB triplets. transparent gives the index of the transparent colour (-1 if none, which is quite common).

    savegif - save a non-animated GIF file. This one looks frightening, because of little-used features of GIF files we support. pal points to palsize RGB triplets. transparent passes in the transparent color (use -1 for no transparency). important and interlaced are obsolete and can be zero.


    jpeg.c

    Copyright: Malcolm McLean

    Code to load a JPEG image as a 24-bit rgb array.

    Public functions

    
            unsigned char *loadjpeg(const char *path, int *width, int *height);
            

    loadjpeg - load a JPEG.


    loadasutf8.c

    Copyright: Malcolm McLean

    Code to load practically any common text format file as clean UTF-8 without a BOM. It relies on automatic detection of text format type.

    Public functions

    
            char *loadasutf8(const char *filename, int *err);
            

    loadasutf8 - load text as UTF-8. It's quite straightforwards except for the text format detection code on which it depends. It returns NULL and err is set to non-zero on failure. To do, improve error reporting.


    loadcursor.c

    Copyright: Malcolm McLean

    Code to load a Microsoft .cur (cursor) file.

    Public functions

    
            typedef struct
            {
                int width;
                int height;
                unsigned char *rgba;
                int hotx;
                int hoty;
            } BBX_CURSOR;
    
            BBX_CURSOR *loadcursor(const char *filename, int *err);
            

    loadcursor - load a .cur file. A .cur file is basically just a bitmap with a hotpoint attached. The function hasn't been well-tested.


    loadimage.c

    Copyright: Malcolm McLean

    High-level image loading function. Should load practically anything as 32 bit RGBA, including SVG files.

    Public functions

    
            unsigned char *loadrgba(char *fname, int *width, int *height, int *err);
            unsigned char *loadassvgwithsize(char *fname, int width, int height);
            

    loadrgba - load a raster image (.bmp, .gif, .jpeg, .png, .tiff) as a 32 bit RGBA. We try to keep to the convention that err -1 means out of memory, -2 means failure to open file or Io error, and -3 means a parse error. However it's unlikely that this is completely consistent. This is the crown jewel function.

    loadsvg - load an SVG as a 32 bit RTGBA raster. Since SVGs are scaleable, they can't have the same interface as the raster graphics, and you have to pass in the width and height that you want.


    loadmp3.c

    Copyright: Malcolm McLean

    A thin layer on top of the minimp3.c audio decoder to load an MP3 file into memory as raw uncompressed audio 16-bit PCM samples.

    Public functions

    
            short *loadmp3(const char *fname, long *samplerate, int
                           *Nchannels, long *Nsamples);
            short *mp3streamdecompress(unsigned char *mp3_stream, int stream_size,
               long *samplerate, int *Nchannels, long *Nsamples);
            

    loadmp3 - loads an MP3 file using the same interface as for WAV and AIFF files.

    mp3streamdecompress - a function to decompress and entire MP3 image in one go. It's useful for audio processing, less useful for playback.


    loadtiff.c

    Copyright: Malcolm McLean

    TIFF file loader. TIFF is one of those formats which has been allowed to get out of control. However this function will load the vast majority of TIFFs.

    Public functions

    
            unsigned char *floadtiff(FILE *fp, int *width, int *height);
            

    floadtiff - load a TIFF froman open stream. TIFFs were designed to work with non-seekable streams, hence the interface. TIFF is a very complicated umbrella format and it will probably fail on some input.


    lodepng.c

    Copyright: (c) 2005-2012 Lode Vandevenne

    PNG (Portable network graphic) file loader and saver.


    minimp3.c

    Copyright: (c) 2001, 2002 Fabrice Bellard,
    Copyright: (c) 2007 Martin J. Fiedler

    MP3 audio decoder. This is almost in a single file (there is a trivial dependency on libc.h), and is a great achievement.


    options.c

    Copyright: Malcolm McLean

    Commandline options parser.

    Public functions

    
            OPTIONS *options(int argc, char **argv, char *flags);
            void killoptions(OPTIONS *opt);
            int opt_get(OPTIONS *opt, char *name, char *fmt, ...);
            int opt_error(OPTIONS *opt, FILE *fp);
            int opt_Nargs(OPTIONS *opt);
            char *opt_arg(OPTIONS *opt, int index);
            

    options - create an OPTIONS object from argc and argv. It takes local copies, so everything is from thsi point self-contained. flags is a list of sigle character flags the options parser will accept. If you don't support single character option flags, pass NULL rather than the empty string.

    killoptions - the OPTIONS object destructor.

    opt_get - query the OPTIONS object for an option. You can pass several aliases for an option at once, eg, "-help -h --help /help /h". If the option is a flag, fmt should be null. If it takes arguments, it's similar to scanf() - pass %d for an integer or %f for a double. %s should always take a length field (e.g %256s) to avoid buffer overruns. Intitialise the arguments to defaults and they will not be overwritten if the option is not present. Note that opt_get() is used both to extract options, and to tell the options parser what the allowed options are. So you must query for every option you support.

    opt_error - query the options parser for user errors. Pass a stream to receive error text (so, usually, pass stderr), or NULL to suppress. It returns non-zero if there is an error. The main errors are unrecognised options, or options with the wrong arguments. So setting up the opt_get calls beforehand is vital. Note that it won't sanity check arguments for you, you have to do that in caller.

    opt_Nargs - after all the calls to opt_get have been completed, the remaining arguments in argv are non-option arguments, usually filenames. This function counts them.

    opt_arg - call to retrieve non-option arguments. The arguments are allocated with malloc() and so should be freed by caller.

    See using the commandline options parser.


    rbtree.c

    Copyright: Malcolm McLean

    Code to implement a red-black tree. red-black trees are balanced binary trees which do not deenrate into linked lists afte rrepeated insertions or deletions. They always remain reaonsably balanced. Which means that they can offer O(log N) searching of dynamic data.

    Public functions

    
            RBTREE *rbtree(int (*compfunc)(const void *e1, const void *e2));
            void killrbtree(RBTREE *tree);
            int rbt_add(RBTREE *tree, void *key, void *data);
            int  rbt_del(RBTREE *tree, void *key);
            void *rbt_find(RBTREE *tree, void *key);
            void *rbt_next(RBTREE *tree, void *key, void **dataret);
            void *rbt_prev(RBTREE *tree, void *key, void **dataret);
            

    rbtree - constructor for an empty RBTREE object. Since the tree is sorted, it need s aqsort-style comparison function to order the elements.

    killrbtree - RBTREE destructor. Note that it doesn't own its contents.

    rbt_add - add an entry to the RBTREE. It consists of a key / data item pair. The RBTREE doesn't own the keys or the data, so the key can be a field of the data entry.

    rbt_del - delete an entry from the tree. Returns -1 if the object is not found. Note that it won't delete either the key or the data.

    rbt_find - search for an entry by key. It returns a pointer to the data item if found, NULL if not found.

    rbt_next - for "walking" the tree. Returns the key for the next entry, and fills in the void ** for the data. Pass NULL for the first entry, and then the return for the subsequent entries, until the function returns NULL.

    rbt_prev - for "walking" the tree in a reverse direction. Returns the key for the previous entry, and fills in the void ** for the data. Pass NULL for the last entry, and then the return for the preceding entries, until the function returns NULL.

    See using the RBTREE.


    resize.c

    Copyright: Malcolm McLean

    Code to resize images.

    Public functions

    
            void resizeimage(unsigned char *dest, int dwidth, int dheight,
                unsigned char *src, int swidth, int sheight);
            

    resizeimage - resize a 32 bit RGBA image to dwidth, dheight. It doesn't allocate the buffer so you have to pass it in.


    savejpeg.c

    Copyright: Malcolm McLean

    JPEG saver. There's no facility for choosing quality, and it just uses generally accepted defaults. However you can play with the quantisation table if you want.

    Public functions

    
            int savejpeg(char *path, unsigned char *rgb, int width, int height);
            

    savejpeg - saves a JPEG. There are no options for quality. If you want to add them. you will need to adjust the quantisation table.


    text_encoding_detect.c

    Copyright: 2015 Jonathan Bennett

    Code to automatically detect text format form the binary.

    Public functions

    
            typedef enum
            {
                TEXTENC_None,                // Unknown or binary
                TEXTENC_ANSI,                // 0-255
                TEXTENC_ASCII,               // 0-127
                TEXTENC_UTF8_BOM,            // UTF8 with BOM
                TEXTENC_UTF8_NOBOM,          // UTF8 without BOM
                TEXTENC_UTF16_LE_BOM,        // UTF16 LE with BOM
                TEXTENC_UTF16_LE_NOBOM,      // UTF16 LE without BOM
                TEXTENC_UTF16_BE_BOM,        // UTF16-BE with BOM
                TEXTENC_UTF16_BE_NOBOM,      // UTF16-BE without BOM
            } TextEncoding;
    
            TextEncoding DetectTextFileEncoding(const char *filename, int *error);
    
            

    DetectTextFileEncoding - this uses very good heuristics. error isn't really functional.


    ttf2c.c

    Copyright: Malcolm McLean

    This is a thin layer on top of the freetype/ code which loads a .ttf or TrueType font, and saves it out as a struct bitmap_font.

    Public functions

    
            int dumpttf(char *fname, int header, char *name, int points, FILE *fp);
            

    dumpttf - write out a TrueType font as C source for a bitmap_font structure. You need to pass in the size you want in points. fp points to the output stream. header is set to just output a C header.


    wavfile.c

    Copyright: Malcolm McLean

    Microsoft .wav file loader and saver. WAV files are relatively simple and clean.

    Public functions

    
            short *loadwav(const char *fname, long *samplerate, int *Nchannels, long
                *Nsamples);
            int savewav(const char *fname, const short *pcm, long samplerate, int
                Nchannels, long Nsamples);
            short *floadwav(FILE *fp, long *samplerate, int *Nchannels, long
                *Nsamples);
            int fsavewav(FILE *fp, const short *pcm, long samplerate, int Nchnanels,
                long Nsamples);
            

    loadwav - load a WAV file.

    savewav - save a WAV file.

    floadwav - load a WAV file from an open stream.

    fsavewav - save a WAV file to an open stream.


    xmlparser.c

    Copyright: Malcolm McLean

    Code to parse an XML file. Whilst it is adequate for parsing Baby X resource compiler input files, it is not good enough to stand up to use as a general XML file parser.

    Public functions

    
            typedef struct xmlattribute
            {
                char *name;                /* attribute name */
                char *value;               /* attribute value (without quotes) */
                struct xmlattribute *next; /* next pointer in linked list */
            } XMLATTRIBUTE;
    
            typedef struct xmlnode
            {
                char *tag;                 /* tag to identify data type */
                XMLATTRIBUTE *attributes;  /* attributes */
                char *data;                /* data as ascii */
                int position;              /* position of the node within parent's
                                                data string */
                struct xmlnode *next;      /* sibling node */
                struct xmlnode *child;     /* first child node */
            } XMLNODE;
    
            typedef struct
            {
                XMLNODE *root;             /* the root node */
            } XMLDOC;
    
    
            XMLDOC *loadxmldoc(char *fname, int *err);
            XMLDOC *floadxmldoc(FILE *fp, int *err);
            void killxmldoc(XMLDOC *doc);
    
            XMLNODE *xml_getroot(XMLDOC *doc);
            const char *xml_gettag(XMLNODE *node);
            const char *xml_getdata(XMLNODE *node);
            const char *xml_getattribute(XMLNODE *node, const char *attr);
            int xml_Nchildren(XMLNODE *node);
            int xml_Nchildrenwithtag(XMLNODE *node, const char *tag);
            XMLNODE *xml_getchild(XMLNODE *node, const char *tag, int index);
            XMLNODE **xml_getdescendants(XMLNODE *node, const char *tag, int *N);
            

    loadxmldoc - load an XML document.

    floadxmldoc - load an XML document from an open stream.

    killxmldoc - XMLDOC destructor.

    xml_getroot - get the root node of the XML document. All other nodes are descendants.

    xml_gettag - get the element name associated with a node.

    xml_getdata - get the data (the text between the open and close tags) associated with a node. Note currrently this function doesn't handle properly nodes with both children and embedded text.

    xml_getattribute - get the named atribute data associated with a node. You have to walk the attribute list to query for unknown attributes(which should be rarely needed).

    xml_getNchildren - get the number of direct children of a node.

    xml_Nchildrenwithtag - convenience function, get the number of children with a given element name.

    xml_getchild - get the child of a node, passing in element name and index. So use in conjunction with xml_Nchildrenwithtag() to visi every child element of a given type. Note that this function starts from the beginning of the list on each call, don't use for really long XML (walk the list by hand). If you pass NULL for the element name, it finds he Nth child of any type.

    xml_getdescendants - convenience function. it gets all the descendants of a node with a certain element name, and returns them in an allocated list. Useful for over-specified XML where the data of interest is buried away in higher-level structures.

    The XML parser is being rewritten and is not advised for general use at present. It's good enough for simple XML like the Baby X resource script, but it won't stand up to the complications found in general XML.

    See using the XML parser.


    nanosgv.h

    Copyright: (c) 2013-14 Mikko Mononen

    This code is implemented as header-only file. It parses an SVG (scaleable vector graphics) file.

    Public functions

    
            NSVGimage* nsvgParseFromFile(const char* filename, const char* units, float dpi);
            void nsvgDelete(NSVGimage* image);
            

    nanosvgrast.h

    Copyright: (c) 2013-14 Mikko Mononen

    This code is implemented as a header-only file. It takes the output of nanosvg.h, and produces a raster image.

    Public functions

    
            NSVGrasterizer* nsvgCreateRasterizer(void);
            void nsvgDeleteRasterizer(NSVGrasterizer* r);
            void nsvgRasterize(NSVGrasterizer* r,
                               NSVGimage* image, float tx, float ty, float scale,
                               unsigned char* dst, int w, int h, int stride);
            

    freetype/

    Copyright: 1996-1999 by David Turner, Robert Wilhelm, and Werner Lemberg.

    This directory contains files for drawing TrueType fonts. We use it to convert the fonts to a simple raster representation which can be used by small programs.


    samplerate/

    Copyright: (C) 2002-2011 Erik de Castro Lopo

    This directory contains files for resampling audio at a given frequency (Hz).