# HG changeset patch # User John Tsiombikas # Date 1500953373 -10800 # Node ID f5422c7609c1f4b6626f15bb8c64fc1e197f9ce1 # Parent 89fd621967398aef2789b658fbb51b51141419a1 implemented interleaved output, and *I think* I fixed the planar output, needs testing diff -r 89fd62196739 -r f5422c7609c1 src/main.c --- a/src/main.c Thu Jul 20 07:42:52 2017 +0300 +++ b/src/main.c Tue Jul 25 06:29:33 2017 +0300 @@ -9,9 +9,16 @@ static const char *modestr(int cmode); static void output_planar(FILE *fp, struct image *img); static void output_linear(FILE *fp, struct image *img); +static void output_interleaved(FILE *fp, struct image *img); static void output_palette(FILE *fp, struct image *img, int pcol_bits); -static int planar = 0; +enum img_mode { + IMG_LINEAR, + IMG_PLANAR, + IMG_INTERLEAVED +}; + +static enum img_mode mode = IMG_LINEAR; static int pcol_bits = 8; static int verbose = 0; @@ -23,17 +30,24 @@ if(argv[i][0] == '-') { if(argv[i][2] == 0) { switch(argv[i][1]) { - case 'a': - planar = 1; + case '4': pcol_bits = 4; break; + case '8': + pcol_bits = 8; + break; + case 'p': - planar = 1; + mode = IMG_PLANAR; break; case 'l': - planar = 0; + mode = IMG_LINEAR; + break; + + case 'i': + mode = IMG_INTERLEAVED; break; case 'v': @@ -43,9 +57,11 @@ case 'h': printf("usage: %s [options] ... \n", argv[0]); printf("Options:\n"); - printf(" -a: amiga mode (4bit palette colors & planar format)\n"); - printf(" -p: output image in planar format\n"); - printf(" -c: output image in linear (chunky) format\n"); + printf(" -p: output image in planar mode\n"); + printf(" -l: output image in linear mode\n"); + printf(" -i: output image in interleaved mode\n"); + printf(" -8: output palette with 8bit per color channel\n"); + printf(" -4: output palette with 4bit per color channel\n"); printf(" -v: verbose output\n"); printf(" -h: print usage and exit\n"); return 0; @@ -111,12 +127,24 @@ } } - if(planar) { + switch(mode) { + case IMG_PLANAR: if(verbose) printf(" writing planar pixels\n"); output_planar(fp, &img); - } else { + break; + + case IMG_LINEAR: if(verbose) printf(" writing linear pixels\n"); output_linear(fp, &img); + break; + + case IMG_INTERLEAVED: + if(verbose) printf(" writing interleaved pixels\n"); + output_interleaved(fp, &img); + break; + + default: + break; } fclose(fp); @@ -156,12 +184,36 @@ for(i=0; ibpp; i++) { for(y=0; yheight; y++) { for(x=0; xwidth; x++) { - acc |= (((*pptr >> i) & 1) << acc_bit); - if(--acc_bit == 0) { + int bit = img->bpp - i - 1; + acc |= ((pptr[i] >> bit) & 1) << acc_bit; + if(acc_bit-- == 0) { fputc(acc, fp); acc_bit = 7; acc = 0; - ++pptr; + pptr += img->bpp; + } + } + } + } +} + +static void output_interleaved(FILE *fp, struct image *img) +{ + int i, x, y; + unsigned char acc = 0; + int acc_bit = 7; + unsigned char *pptr = img->pixels; + + for(y=0; yheight; y++) { + for(i=0; ibpp; i++) { + for(x=0; xwidth; x++) { + int bit = img->bpp - i - 1; + acc |= ((pptr[i] >> bit) & 1) << acc_bit; + if(acc_bit-- == 0) { + fputc(acc, fp); + acc_bit = 7; + acc = 0; + pptr += img->bpp; } } }