Skip to content

Commit

Permalink
Merge branch 'feature-load-grayscaled-input' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
saitoha committed Nov 29, 2014
2 parents 64a43e1 + 982bcfa commit 83f1422
Show file tree
Hide file tree
Showing 21 changed files with 788 additions and 336 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -39,7 +39,7 @@ before_script:
script:
- "make"
- "sudo make install"
- "make test | tee log | grep succeeded || (cat log|tail -n10; exit 1)"
- "make test | tee log | grep succeeded || (cat log|tail -n100; exit 1)"

after_success:
- "make valgrind || (cat valgrind.log && exit 1)"
Expand Down
13 changes: 11 additions & 2 deletions converters/Makefile.am
Expand Up @@ -83,6 +83,9 @@ if WANT_IMG2SIXEL
$(WINE) ./img2sixel ../images/snake.tga -7 -sauto -w100 -rgaussian -qauto -dburkes -tauto
$(WINE) ./img2sixel -p300 ../images/snake.tiff -8 -scenter -h100 -qfull -rhanning -dstucki -thls
$(WINE) ./img2sixel -8 -qauto -thls -e ../images/snake.pgm
$(WINE) ./img2sixel -8 ../images/snake.ppm -m ../images/map8-palette.png
$(WINE) ./img2sixel -7 ../images/snake.jpg -m ../images/map16-palette.png
$(WINE) ./img2sixel -7 ../images/snake-palette.png -w300
$(WINE) ./img2sixel -7 -w100 -h100 -bxterm16 ../images/snake.pbm
$(WINE) ./img2sixel -I ../images/snake.ppm -dstucki -thls
$(WINE) ./img2sixel -I ../images/snake-ascii.ppm -8 -dburkes
Expand All @@ -91,6 +94,12 @@ if WANT_IMG2SIXEL
$(WINE) ./img2sixel -I ../images/snake-ascii.pbm -datkinson
$(WINE) ./img2sixel -I ../images/snake-grayscale.png
$(WINE) ./img2sixel -I ../images/snake-grayscale.jpg
$(WINE) ./img2sixel ../images/snake.six -m ../images/map8.six
$(WINE) ./img2sixel ../images/snake.six -w200 -p8
$(WINE) ./img2sixel ../images/snake.six -c200x200+100+100
$(WINE) ./img2sixel -bxterm16 ../images/snake.six
$(WINE) ./img2sixel -e ../images/snake.six
$(WINE) ./img2sixel -I ../images/snake.six
$(WINE) ./img2sixel -I ../images/snake.ppm -c2000x100+40+20 -wauto -h200 -qhigh -dfs -rbilinear -trgb
$(WINE) ./img2sixel -I -v ../images/snake.bmp -w200 -hauto -c100x1000+40+20 -qlow -dnone -rhamming -thls
$(WINE) ./img2sixel -m ../images/map8.png -w200 -fauto -rwelsh ../images/egret.jpg
Expand All @@ -105,10 +114,10 @@ if WANT_IMG2SIXEL
$(WINE) ./img2sixel ../images/seq2gif.gif -ldisable -dnone -g
$(WINE) ./img2sixel ../images/seq2gif.gif -S -datkinson
if HAVE_JPEG
$(WINE) ./img2sixel ../images/snake-progressive.jpg | tee snake.sixel
$(WINE) ./img2sixel ../images/snake-progressive.jpg
endif
if HAVE_PNG
$(WINE) ./img2sixel ../images/snake.png | tee snake.sixel
$(WINE) ./img2sixel ../images/snake.png
endif
if HAVE_CURL
!$(WINE) ./img2sixel file:///test
Expand Down
13 changes: 11 additions & 2 deletions converters/Makefile.in
Expand Up @@ -1101,6 +1101,9 @@ test: all
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake.tga -7 -sauto -w100 -rgaussian -qauto -dburkes -tauto
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -p300 ../images/snake.tiff -8 -scenter -h100 -qfull -rhanning -dstucki -thls
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -8 -qauto -thls -e ../images/snake.pgm
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -8 ../images/snake.ppm -m ../images/map8-palette.png
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -7 ../images/snake.jpg -m ../images/map16-palette.png
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -7 ../images/snake-palette.png -w300
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -7 -w100 -h100 -bxterm16 ../images/snake.pbm
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake.ppm -dstucki -thls
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake-ascii.ppm -8 -dburkes
Expand All @@ -1109,6 +1112,12 @@ test: all
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake-ascii.pbm -datkinson
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake-grayscale.png
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake-grayscale.jpg
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake.six -m ../images/map8.six
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake.six -w200 -p8
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake.six -c200x200+100+100
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -bxterm16 ../images/snake.six
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -e ../images/snake.six
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake.six
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I ../images/snake.ppm -c2000x100+40+20 -wauto -h200 -qhigh -dfs -rbilinear -trgb
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -I -v ../images/snake.bmp -w200 -hauto -c100x1000+40+20 -qlow -dnone -rhamming -thls
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel -m ../images/map8.png -w200 -fauto -rwelsh ../images/egret.jpg
Expand All @@ -1122,8 +1131,8 @@ test: all
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/seq2gif.gif -ldisable -dnone -u
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/seq2gif.gif -ldisable -dnone -g
@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/seq2gif.gif -S -datkinson
@HAVE_JPEG_TRUE@@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake-progressive.jpg | tee snake.sixel
@HAVE_PNG_TRUE@@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake.png | tee snake.sixel
@HAVE_JPEG_TRUE@@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake-progressive.jpg
@HAVE_PNG_TRUE@@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel ../images/snake.png
@HAVE_CURL_TRUE@@WANT_IMG2SIXEL_TRUE@ !$(WINE) ./img2sixel file:///test
@HAVE_CURL_TRUE@@WANT_IMG2SIXEL_TRUE@ $(WINE) ./img2sixel file:///$$(pwd)/../images/snake.jpg
@WANT_IMG2SIXEL_TRUE@@WANT_SIXEL2PNG_TRUE@ ! $(WINE) ./sixel2png -i unknown.sixel
Expand Down
110 changes: 70 additions & 40 deletions converters/frompnm.c
Expand Up @@ -26,19 +26,22 @@
#include <ctype.h>
#include <assert.h>


static unsigned char *
pnm_get_line(unsigned char *p, unsigned char *e, unsigned char *line)
pnm_get_line(unsigned char *p, unsigned char *end, unsigned char *line)
{
int n;

do {
for (n = 0 ; p < e && *p >= ' '; p++) {
if (n < 255)
for (n = 0 ; p < end && *p >= ' '; p++) {
if (n < 255) {
line[n++] = *p;
}
}

if (p < e && *p == '\n')
if (p < end && *p == '\n') {
p++;
}

line[n] = '\0';

Expand All @@ -49,22 +52,31 @@ pnm_get_line(unsigned char *p, unsigned char *e, unsigned char *line)


unsigned char *
load_pnm(unsigned char *p, int len, int *psx, int *psy, int *pcomp, int *pstride)
load_pnm(unsigned char *p, int length,
int *psx, int *psy, int *pcomp,
unsigned char **ppalette, int *pncolors,
int *pixelformat)
{
int n, i, b, x, y, c[3];
int n, i, b, x, y, component[3];
int ascii, maps;
int width, height, deps;
unsigned char *result;
unsigned char *s, *e, tmp[256];
unsigned char *s, *end, tmp[256];

(void) ppalette;
(void) pncolors;
(void) pixelformat;

width = height = 0;
deps = 1;
*pcomp = 3;

e = p + len;
p = pnm_get_line(p, e, tmp);
end = p + length;
p = pnm_get_line(p, end, tmp);

if (tmp[0] != 'P')
if (tmp[0] != 'P') {
return NULL;
}

switch(tmp[1]) {
case '1':
Expand Down Expand Up @@ -95,33 +107,41 @@ load_pnm(unsigned char *p, int len, int *psx, int *psy, int *pcomp, int *pstride
return NULL;
}

p = pnm_get_line(p, e, tmp);
p = pnm_get_line(p, end, tmp);

s = tmp;
width = 0;
while (isdigit(*s) && width >= 0)
while (isdigit(*s) && width >= 0) {
width = width * 10 + (*s++ - '0');
while (*s == ' ')
}
while (*s == ' ') {
s++;
}
height = 0;
while (isdigit(*s) && height >= 0)
while (isdigit(*s) && height >= 0) {
height = height * 10 + (*s++ - '0');
while (*s != '\0')
}
while (*s != '\0') {
s++;
}

if (maps > 0) {
p = pnm_get_line(p, e, tmp);
p = pnm_get_line(p, end, tmp);
s = tmp;
deps = 0;
while (isdigit(*s) && deps >= 0)
while (isdigit(*s) && deps >= 0) {
deps = deps * 10 + (*s++ - '0');
}
}

if (width < 1 || height < 1 || deps < 1)
if (width < 1 || height < 1 || deps < 1) {
return NULL;
}

if ((result = malloc(width * height * 3 + 1)) == NULL)
result = malloc(width * height * 3 + 1);
if (result == NULL) {
return NULL;
}

memset(result, 0, width * height * 3 + 1);

Expand All @@ -131,61 +151,71 @@ load_pnm(unsigned char *p, int len, int *psx, int *psy, int *pcomp, int *pstride
for (i = 0 ; i < b ; i++) {
if (ascii) {
while (*s == '\0') {
if (p >= e)
if (p >= end) {
break;
p = pnm_get_line(p, e, tmp);
}
p = pnm_get_line(p, end, tmp);
s = tmp;
}
n = 0;
if (maps == 0) {
n = *s++ == '0';
} else {
while (isdigit(*s) && n >= 0)
while (isdigit(*s) && n >= 0) {
n = n * 10 + (*s++ - '0');
while (*s == ' ')
}
while (*s == ' ') {
s++;
}
}
} else {
if (p >= e)
if (p >= end) {
break;
}
if (maps == 0) {
n = ((*p << (x & 0x7) >> 0x7) & 1) == 0;
if ((x & 0x7) == 0x7) p++;
if ((x & 0x7) == 0x7) {
p++;
}
} else {
n = *(p++);
}
}
c[i] = n;
component[i] = n;
}
if (i < b)
if (i < b) {
break;
}

switch(maps) {
case 0: /* bitmap */
if (c[0] == 0)
c[0] = c[1] = c[2] = 0;
else
c[0] = c[1] = c[2] = 255;
if (component[0] == 0) {
component[0] = component[1] = component[2] = 0;
} else {
component[0] = component[1] = component[2] = 255;
}
break;
case 1: /* graymap */
c[0] = c[1] = c[2] = (c[0] * 255 / deps);
component[0] = component[1] = component[2] = component[0] * 255 / deps;
break;
case 2: /* pixmap */
c[0] = (c[0] * 255 / deps);
c[1] = (c[1] * 255 / deps);
c[2] = (c[2] * 255 / deps);
component[0] = (component[0] * 255 / deps);
component[1] = (component[1] * 255 / deps);
component[2] = (component[2] * 255 / deps);
break;
}

*(result + (y * width + x) * 3 + 0) = c[0];
*(result + (y * width + x) * 3 + 1) = c[1];
*(result + (y * width + x) * 3 + 2) = c[2];
*(result + (y * width + x) * 3 + 0) = component[0];
*(result + (y * width + x) * 3 + 1) = component[1];
*(result + (y * width + x) * 3 + 2) = component[2];
}
}

*psx = width;
*psy = height;
*pcomp = 3;
*pstride = *psx * *pcomp;
return result;
}

/* emacs, -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
/* vim: set expandtab ts=4 : */
/* EOF */
5 changes: 4 additions & 1 deletion converters/frompnm.h
Expand Up @@ -31,7 +31,10 @@ extern "C" {
/* image scaling api */

unsigned char *
load_pnm(unsigned char *p, int len, int *psx, int *psy, int *pcomp, int *pstride);
load_pnm(unsigned char *p, int len,
int *psx, int *psy, int *pcomp,
unsigned char **ppalette, int *pncolors,
int pixelformat);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 83f1422

Please sign in to comment.