/*-----------------------------------------------------------------------------
                This filter emulates Ben Day dots

Benjamin Henry Day 1810-89, American journalist.
He learned the printer's trade in the office of the Springfield (Mass.)
Republican and opened a printing office in New York City.

Lack of work during a financial depression led him to begin publishing (1833) 
the New York Sun.

In 1842, Day founded the monthly Brother Jonathan,
which later became the first illustrated weekly in the United States.

His son Benjamin (1838-1916) invented the Ben Day process for shading printed 
illustrations

generate a 4,800 wide image with 60 dots -- each is 80 pixels in diameter

-----------------------------------------------------------------------------*/
void ben_day_filter(TIFFIMAGEACCESS *poster)
{
 TIFFIMAGEACCESS *ben_day;
 char ben_day_filename[MAXPATH];
 unsigned long x, y, x_out, y_out;
    int i, j, x_offset, y_offset;
    int dot_radius, box_radius, dot_radius_squared, radius_squared;
    int pitch, row, radius, diameter;
 unsigned char r, g, b, r_bakg, g_bakg, b_bakg;
    double r_sum, g_sum, b_sum, scale;
    double hue, sat, lit;
    char *poster_pixel_ptr, *ben_day_pixel_ptr;

    // set the radius so that there are ~90 dots across the image
    radius = poster->image_width / 180;
    if(radius < 10) radius = 10;

    pitch = radius * sqrt(3.0);
    diameter = (2 * radius) + 1;  // the center is the zero-th pixel

    if(global_image_width == 0)
    {
        scale = 1.0;    }
    else
    {
        scale = (double)global_image_width / poster->image_width;
    }
    //printf("Ben Day dot Y pitch = %2d Y excess = %2d\n", (poster->image_width 
% diameter), (poster->image_length % diameter));
    //printf("Ben Day dot X diameter = %2d Y pitch = %2d\n", diameter, pitch);

    printf("Ben Day radius %2d ...", radius);
    if(radius < 4)
    {
        printf("image width (%d pixels) is too small for Ben Day dots\n", 
poster->image_width);
        xexit(1);
    }

    x_offset = (poster->image_width % diameter) / 2;
    //y_offset = (poster->image_length % pitch) / 2;
    y_offset = ((poster->image_length - diameter) % pitch) / 2;
    printf("Ben Day dot X excess = %2d Y excess = %2d\n", (poster->image_width 
% diameter), (poster->image_length % diameter));
    printf("Ben Day dot X offset = %2d Y offset = %2d\n", x_offset, y_offset);

 prefix_long_filename("Ben_Day", poster->filename, ben_day_filename);
    dots_per_inch = 100;    // set the dpi for Ben Day printing
 ben_day = tifio_open_write
 (
  ben_day_filename,
  poster->image_width * scale,
  poster->image_length * scale,
  CHANNELS_RGBA,   // open with an alpha channel
        BUFFERED
 );


    tifio_initialize_buffer_value(ben_day, 0);  // black and transparent

    for(y = radius + y_offset, row = 0; y < poster->image_length - radius; y += 
pitch, row++)
    {
        // inset on even/odd rows
        for(x = radius + x_offset + radius * (row%2); x < poster->image_width - 
radius; x += diameter)
        {
            // average the region
            r_sum = 0; g_sum = 0; b_sum = 0;
            for(i = -radius; i <= radius; i++)
            {
                for(j = -radius; j <= radius; j++)
                {
                    poster_pixel_ptr = PIXEL_BUFFER_PTR(poster->image_buffer, 
poster->image_width, (x + i), (y + j));
                    PTR_to_RGB(poster_pixel_ptr, r, g, b);
                    r_sum += r; g_sum += g; b_sum += b;
                }
            }

            r = r_sum / (diameter) / (diameter);
            g = g_sum / (diameter) / (diameter);
            b = b_sum / (diameter) / (diameter);

            RGBtoHSL(r, g, b, &hue, &sat, &lit);
            dot_radius = ((radius * scale) - 1) * (1.0 - lit);
            sat = 0.90; // not too saturated
            HSLtoRGB(hue, sat, lit, &r, &g, &b);

/*
            if(TRUE || lit > 0.5)
            {
                // colored dot on a white background
                dot_radius = ((radius * scale) - 1) * (1.0 - lit);
                //lit = 0.5;
                sat = 0.90; // not too saturated
                HSLtoRGB(hue, sat, lit, &r, &g, &b);
                r_bakg = 255; g_bakg = 255; b_bakg = 255;
            }
            else
            {
                // white dot on a colored background
                dot_radius = (radius) * (1.0 - lit);
                lit = 0.5;
                sat = 0.80; // not too saturated
                HSLtoRGB(hue, sat, lit, &r_bakg, &g_bakg, &b_bakg);
                r = 255; g = 255; b = 255;
            }
*/
            dot_radius_squared = dot_radius * dot_radius;
            x_out = scale * x;
            y_out = scale * y;

            // a square box of pixels is colored
            box_radius = dot_radius + 2;
            for(i = -box_radius; i <= box_radius; i++)
            {
                for(j = -box_radius; j <= box_radius; j++)
                {
                    ben_day_pixel_ptr = 
PIXEL_RGBA_BUFFER_PTR(ben_day->image_buffer, ben_day->image_width, 
ben_day->channels, (x_out + i), (y_out + j));
                    RGB_to_PTR(r, g, b, ben_day_pixel_ptr);
                }
            }

            for(i = -dot_radius; i <= dot_radius && (dot_radius != 0); i++)
            {
                for(j = -dot_radius; j <= dot_radius; j++)
                {
                    // a square box of pixels is colored
                    ben_day_pixel_ptr = 
PIXEL_RGBA_BUFFER_PTR(ben_day->image_buffer, ben_day->image_width, 
ben_day->channels, (x_out + i), (y_out + j));
                    RGB_to_PTR(r, g, b, ben_day_pixel_ptr);

                    if((i * i + j * j) <= dot_radius_squared)
                    {
                        // the dot alpha is 255 = opaque
                        ALPHA_to_PTR(255, ben_day_pixel_ptr);
                    }
                    else
                    {
                        // the background alpha is 0 = transparent
                        ALPHA_to_PTR(0, ben_day_pixel_ptr);
                    }
                }
            }
  }
 }

    map_to_palette_file(ben_day, "Ben_Day.txt");

    gaussian_alpha_sigma14_filter(ben_day);
    //white_transparent_generate_alpha_from_rgb(ben_day);

    //predictor_differencing(ben_day);
    //predictor_de_differencing(ben_day);

 sprintf(ben_day->description, "source image: %s Ben Day radius %d", 
poster->filename, radius);
 tifio_close(ben_day);
    dots_per_inch = DEFAULT_DOTS_PER_INCH;
    printf(" done\n");
}

http://www.fourteenthstreetstudio.com/web_page_Vanessa%20Ben%20Day_root.htm


_______________________________________________
NetBehaviour mailing list
[email protected]
http://www.netbehaviour.org/mailman/listinfo/netbehaviour

Reply via email to