Malcolm's github site Baby X Resource compiler    Baby X RC banner

Using loadimage in Baby X

The Baby X resource compiler comes with a powerful loadimage() function which you will want to take for your own baby programs if you want to allow the user to specify and image to load a runtime.

The image load function

The image loader which comes with the Baby X resource compiler is powerful and easy to use. It will load most common image files.


    /*
    load an image as 32 bit rgba.
          
    Params:
           fname - the name of the file to load.
           width - return pointer for image width.
           height - return pointer for image height.
           err - return for error code.
    Returns:
           the image as a 32 bit rgba buffer, 0 on fail.
                
    */
    unsigned char *loadrgba(char *fname, int *width, int *height, int *err)
    {
      int fmt;
      unsigned char *answer = 0;

      if(err)
        *err = 0;

      fmt = getformat(fname);

      switch(fmt)
      {
      case FMT_UNKNOWN:
        if(err)
          *err = -3;
        return 0;
      case FMT_JPEG:
        answer = loadasjpeg(fname, width, height);
        break;
      case FMT_PNG:
        answer =  loadaspng(fname, width, height);
        break;
      case FMT_BMP:
        answer =  loadasbmp(fname, width, height);
        break;
      case FMT_GIF:
        answer =  loadasgif(fname, width, height);
        break;
      case FMT_TIFF:
         answer = loadastiff(fname, width, height);
         break;
      case FMT_SVG:
          answer = loadassvg(fname, width, height);
          break;
      }
      if(!answer)
        if(err)
          *err = -1;

      return answer;
    }
    

And of course I do not need to insult your intelligence by showing an example of how to use it.

Dependencies:

And the C files all have associated headers. So you need to take those files and drop them into your Baby X program, as well as loadimage.c.

lodepng.c is maintained here.
nanosvg.h nanosvrast.h here.

All other components are by Malcolm's github site

And this function is heart of the resource compiler. It's what most people will use it for.