Table of Contents

Name

libmaglo - MAKIchan Graphic (MAG) Loader Library

Synopsis


#include <maglo.h>
int MAGversion(void);
int MAGopen(MagImage *mag, const char *path);
int MAGclose(MagImage *mag);
long MAGcommentSize(MagImage *mag);
char * MAGcomment(MagImage *mag);
char * MAGmacType(MagImage *mag);
char * MAGuserName(MagImage *mag);
unsigned char MAGmacCode(MagImage *mag);
unsigned char MAGmacFlags(MagImage *mag);
unsigned char MAGscrModeCode(MagImage *mag);
int MAGscrMode200Lines(MagImage *mag);
int MAGscrModeNumColors(MagImage *mag);
int MAGpixelWidth(MagImage *mag);
int MAGscrModeIsDigital(MagImage *mag);
int MAGimageStartX(MagImage *mag);
int MAGimageStartY(MagImage *mag);
int MAGimageEndX(MagImage *mag);
int MAGimageEndY(MagImage *mag);
int MAGimageWidth(MagImage *mag);
int MAGimageHeight(MagImage *mag);
int MAGpalette(MagImage *mag, int index);
int MAGReader(MagImage *mag, MagReader *rd);
int MAGReaderDelete(MagReader *rd);
int MAGReaderNextPixel(MagReader *rd, unsigned char *buf);

Description

The libmaglo library supports reading of the MAKIchan Graphic (MAG) format image files. The format is fairly straightforward, however is documented only in Japanese so there are little implementations available.

The MAG format was used in mid-1980s to mid-1990s to store and distribute screenshots and drawings. The format is suited for pattern-based graphics, and supports only 8/16/256-color images.

Libmaglo in a Nutshell

Call MAGopen with a pointer to an already-allocated storage and the path to the MAG file.

Call functions like MAGimageWidth or MAGscrMode200Lines to get information about the image.

Call MAGReader with pointer to the MAGimage and a pointer to an already-allocated storage.

Loop calling MAGReaderNextPixel to retrieve image data.

When entire image is transferred, call MAGReaderDelete and MAGclose sequentially to free memory regions.

Examples

The following program converts a MAG file to PPM format.

static int magtoppm(const char *from,FILE *to)
{
    MagImage m;
    MagReader rd;
    unsigned char pixel[4],palette[256][3];
    unsigned int i,width,height,pxlw,count;
    int err;
    /* Open MagImage */
    if((err=MAGopen(&m,from))<0)
    {
        fprintf(stderr,"MAGopen error(code=%d)\n",err);
        return -1;
    }
    /* Get info */
    width=MAGimageWidth(&m);
    height=MAGimageHeight(&m);
    pxlw=MAGpixelWidth(&m);
    /* copy palettes */
    for(i=0; i<MAGscrModeNumColors(&m); i++)
    {
        unsigned int pal=MAGpalette(&m,i);
        palette[i][0]=pal>>16;
        palette[i][1]=(pal&0x00FF00)>>8;
        palette[i][2]=(pal&0xFF);
    }
    /* print ppm header */
    fprintf(to,"P6\n%d %d\n255\n",width,height);
    /* create MagReader */
    if((err=MAGReader(&m,&rd))!=MAGERR_NOERROR)
    {
        fprintf(stderr,"MAGReader failed(code=%d)\n",err);
        MAGclose(&m);
        return -1;
    }
    /* copy dots */
    count=width;
    while((err=MAGReaderNextPixel(&rd,pixel))==MAGERR_NOERROR)
    {
        unsigned int k,valid_dots;
        valid_dots=MIN(pxlw,count);
        for(k=0; k<valid_dots; k++)
        {
            fputc(palette[pixel[k]][0],to);
            fputc(palette[pixel[k]][1],to);
            fputc(palette[pixel[k]][2],to);
        }
        count=count-valid_dots;
        if(count<=0)
            count=width;
    }
    if(err<0 && err!=MAGERR_DATA_END)
        fprintf(stderr,"MAGReaderNextPixel returns error %d\n",err);
    /* cleanup MAGReader */
    MAGReaderDelete(&rd);
    /* cleanup MAGImage */
    MAGclose(&m);
    return 0;
}

Author

TOMARI Hisanobu <posco dot grubb at gmail.com>

License

libmaglo is distributed as a public domain software.


Table of Contents