commit 7f181eb8b673aeb78551832e20102d0021ef3ab5 Author: nedko Date: Fri Nov 25 16:12:57 2022 +0200 Initial commit of old code diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7dacfc2 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + gcc -o flac_scan main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..0a3b7fc --- /dev/null +++ b/main.c @@ -0,0 +1,189 @@ +#include +#include +#include + +#define THRESHOLD 1200 + +/* portions derived from IJG code */ + +#define readbyte(a,b) do if(((a)=getc((b))) == EOF) return 0; while (0) +#define readword(a,b) do { int cc_=0,dd_=0; \ + if((cc_=getc((b))) == EOF \ + || (dd_=getc((b))) == EOF) return 0; \ + (a) = (cc_<<8) + (dd_); \ + } while(0) + + +int scanhead (FILE * infile, int * image_width, int * image_height) { + int marker=0; + int dummy=0; + if ( getc(infile) != 0xFF || getc(infile) != 0xD8 ) + return 0; + + for (; + ;) { + + + int discarded_bytes=0; + readbyte(marker,infile); + while (marker != 0xFF) { + discarded_bytes++; + readbyte(marker,infile); + } + do readbyte(marker,infile); while (marker == 0xFF); + + if (discarded_bytes != 0) return 0; + + switch (marker) { + case 0xC0: + case 0xC1: + case 0xC2: + case 0xC3: + case 0xC5: + case 0xC6: + case 0xC7: + case 0xC9: + case 0xCA: + case 0xCB: + case 0xCD: + case 0xCE: + case 0xCF: { + readword(dummy,infile); /* usual parameter length count */ + readbyte(dummy,infile); + readword((*image_height),infile); + readword((*image_width),infile); + readbyte(dummy,infile); + + return 1; + break; + } + case 0xDA: + case 0xD9: + return 0; + default: { + int length; + + readword(length,infile); + + if (length < 2) + return 0; + length -= 2; + while (length > 0) { + readbyte(dummy, infile); + length--; + } + } + break; + } + } +} + +int scanPNG(FILE *fp, int *width, int *height) +{ + uint8_t img_buf[4]; + uint32_t w; + uint32_t h; + + // NO SEEKING IN PIPE + // fseek(fp, 16, SEEK_SET); + fread(img_buf, 1, 4, fp); + fread(img_buf, 1, 4, fp); + fread(img_buf, 1, 4, fp); + fread(img_buf, 1, 4, fp); + + // Read Width + fread(img_buf, 1, 4, fp); + w = img_buf[0]; + w <<= 8; + w += img_buf[1]; + w <<= 8; + w += img_buf[2]; + w <<= 8; + w += img_buf[3]; + + // Read Height + fread(img_buf, 1, 4, fp); + h = img_buf[0]; + h <<= 8; + h += img_buf[1]; + h <<= 8; + h += img_buf[2]; + h <<= 8; + h += img_buf[3]; + + *width = w; + *height = h; + return 0; +} + +int fpeek(FILE *stream) +{ + int c; + + if (feof(stream)) + { + return EOF; + } + c = fgetc(stream); + ungetc(c, stream); + + return c; +} + +int main(int argc, char **argv) +{ + int width; + int height; + int peek; + FILE *pp; + char cmd[2048]; + + if (argc < 2) + { + printf("Need filename\n"); + return -1; + } + + strcpy(cmd, "metaflac --export-picture-to=- \""); + strcat(cmd, argv[1]); + strcat(cmd, "\" 2>&1"); + + pp = popen(cmd, "r"); + if (NULL == pp) + { + printf("Bad PP: '%s'\n", argv[1]); + return -1; + } + + peek = fpeek(pp); + switch (peek) + { + case 0xFF: + //JPG + scanhead(pp, &width, &height); + if ((width > THRESHOLD) || (height > THRESHOLD)) + { + printf("'%s' W:%04d H:%04d\n", argv[1], width, height); + } + break; + + case 0x89: + //PNG + scanPNG(pp, &width, &height); + if ((width > THRESHOLD) || (height > THRESHOLD)) + { + printf("'%s' W:%04d H:%04d\n", argv[1], width, height); + } + break; + + case 0x2F: + break; + + default: + //NaN + printf("'%s' WeirdChamp: 0x%02X\n", argv[1], peek); + } + pclose(pp); + + return 0; +}