Exemple #1
0
void print_image_colors(const Image* const i, const int chars, FILE* f) {

	int x, y;
	int xstart, xend, xincr;

	for ( y=0;  y < i->height; ++y ) {

		if ( use_border ) fprintf(f, "|");

		xstart = 0;
		xend   = i->width;
		xincr  = 1;

		if ( flipx ) {
			xstart = i->width - 1;
			xend = -1;
			xincr = -1;
		}

		for ( x=xstart; x != xend; x += xincr ) {

			float Y = i->pixel[x + (flipy? i->height - y - 1 : y ) * i->width];
			float Y_inv = 1.0f - Y;
			float R = i->red  [x + (flipy? i->height - y - 1 : y ) * i->width];
			float G = i->green[x + (flipy? i->height - y - 1 : y ) * i->width];
			float B = i->blue [x + (flipy? i->height - y - 1 : y ) * i->width];

			const int pos = ROUND((float)chars * (!invert? Y_inv : Y));
			char ch = ascii_palette[pos];

			const float min = 1.0f / 255.0f;

			if ( !html ) {
				const float t = 0.1f; // threshold
				const float i = 1.0f - t;

				int colr = 0;
				int highl = 0;

				// ANSI highlite, only use in grayscale
			        if ( Y>=0.95f && R<min && G<min && B<min ) highl = 1; // ANSI highlite

				if ( !convert_grayscale ) {
				     if ( R-t>G && R-t>B )            colr = 31; // red
				else if ( G-t>R && G-t>B )            colr = 32; // green
				else if ( R-t>B && G-t>B && R+G>i )   colr = 33; // yellow
				else if ( B-t>R && B-t>G && Y<0.95f ) colr = 34; // blue
				else if ( R-t>G && B-t>G && R+B>i )   colr = 35; // magenta
				else if ( G-t>R && B-t>R && B+G>i )   colr = 36; // cyan
				else if ( R+G+B>=3.0f*Y )             colr = 37; // white
				} else {
					if ( Y>=0.7f ) { highl=1; colr = 37; }
				}

				if ( !colr ) {
					if ( !highl ) fprintf(f, "%c", ch);
					else          fprintf(f, "%c[1m%c%c[0m", 27, ch, 27);
				} else {
					if ( colorfill ) colr += 10;          // set to ANSI background color
					fprintf(f, "%c[%dm%c", 27, colr, ch); // ANSI color
					fprintf(f, "%c[0m", 27);              // ANSI reset
				}

			} else {  // HTML output
			
				// either --grayscale is specified (convert_grayscale)
				// or we can see that the image is inherently a grayscale image	
				if ( convert_grayscale || (R<min && G<min && B<min && Y>min) ) {
					// Grayscale image
					if ( colorfill )
						print_html_char(f, ch,
							ROUND(255.0f*Y*0.5f), ROUND(255.0f*Y*0.5f), ROUND(255.0f*Y*0.5f),
							ROUND(255.0f*Y),      ROUND(255.0f*Y),      ROUND(255.0f*Y));
					else
						print_html_char(f, ch,
							ROUND(255.0f*Y), ROUND(255.0f*Y), ROUND(255.0f*Y),
							255, 255, 255);
				} else {
					if ( colorfill )
						print_html_char(f, ch,
							ROUND(255.0f*Y*R), ROUND(255.0f*Y*G), ROUND(255.0f*Y*B),
							ROUND(255.0f*R),   ROUND(255.0f*G),   ROUND(255.0f*B));
					else
						print_html_char(f, ch,
							ROUND(255.0f*R), ROUND(255.0f*G), ROUND(255.0f*B),
							255, 255, 255);
				}
			}
		}

		if ( use_border )
			fputc('|', f);

		if ( html )
			print_html_newline(f);
		else
			fputc('\n', f);
	}
}
Exemple #2
0
static gboolean
write_filter_html_doc (FILE *out, top_level_decl_t *decl)
{
    arg_decl_t *arg;
    int num_args;
    int arg_indent;

    g_assert(decl->type == TOP_LEVEL_FILTER);

    fprintf(out, "<p><a name=\"filter_%s\"></a><font size=\"+1\"><tt>filter <b>%s</b> (",
	    decl->name, decl->name);

    num_args = 0;
    for (arg = decl->v.filter.args; arg != NULL; arg = arg->next)
	++num_args;

    arg_indent = strlen("filter") + strlen(decl->name) + 3;

    for (arg = decl->v.filter.args; arg != NULL; arg = arg->next)
    {
	static struct { int num; const char *name; } types[] =
	    { { ARG_TYPE_INT, "int" },
	      { ARG_TYPE_FLOAT, "float" },
	      { ARG_TYPE_COLOR, "color" },
	      { ARG_TYPE_GRADIENT, "gradient" },
	      { ARG_TYPE_CURVE, "curve" },
	      { ARG_TYPE_IMAGE, "image" },
	      { 0, NULL } };

	int i;

	for (i = 0; types[i].name != NULL; ++i)
	    if (types[i].num == arg->type)
		break;
	g_assert(types[i].name != NULL);

	fprintf(out, "%s <b>%s</b>", types[i].name, arg->name);

	switch (arg->type)
	{
	    case ARG_TYPE_INT :
		if (arg->v.integer.have_limits)
		    fprintf(out, ": %d - %d", arg->v.integer.min, arg->v.integer.max);
		break;

	    case ARG_TYPE_FLOAT :
		if (arg->v.floating.have_limits)
		    fprintf(out, ": %g - %g", arg->v.floating.min, arg->v.floating.max);
		break;

	    default :
		break;
	}

	if (arg->next != NULL)
	{
	    if (num_args > 2)
	    {
		int i;

		fprintf(out, ",\n<br>");

		for (i = 0; i < arg_indent; ++i)
		    fprintf(out, "&nbsp;");
	    }
	    else
		fprintf(out, ", ");
	}
    }

    fprintf(out, ")</font>\n");

    if (decl->docstring != NULL)
    {
	char *p;
	gboolean bold_mode = FALSE;
	gboolean newline = FALSE;

	fprintf(out, "<blockquote>");
	for (p = decl->docstring; *p != '\0'; ++p)
	{
	    if (*p == '\n')
	    {
		if (newline)
		{
		    fprintf(out, "\n<p>");
		    continue;
		}
		newline = TRUE;
	    }
	    else
		newline = FALSE;

	    if (*p == '@')
	    {
		fprintf(out, "<b>");
		bold_mode = TRUE;
	    }
	    else
	    {
		if (bold_mode && !is_ident_char(*p))
		{
		    fprintf(out, "</b>");
		    bold_mode = FALSE;
		}

		if (!print_html_char(out, *p))
		    return FALSE;
	    }
	}
	fprintf(out, "</blockquote>\n");
    }

    return TRUE;
}