How to resolve the algorithm Greyscale bars/Display step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Greyscale bars/Display step by step in the C programming language

Table of Contents

Problem Statement

The task is to display a series of vertical greyscale bars (contrast bars) with a sufficient number of bars to span the entire width of the display. For the top quarter of the display, the left hand bar should be black, and we then incrementally step through six shades of grey until we have a white bar on the right hand side of the display. (This gives a total of 8 bars) For the second quarter down, we start with white and step down through 14 shades of gray, getting darker until we have black on the right hand side of the display. (This gives a total of 16 bars). Halfway down the display, we start with black, and produce 32 bars, ending in white, and for the last quarter, we start with white and step through 62 shades of grey, before finally arriving at black in the bottom right hand corner, producing a total of 64 bars for the bottom quarter.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greyscale bars/Display step by step in the C programming language

This program draws a greyscale plot using the GTK+ library in C. Here's a detailed explanation of the code:

  1. Header Files: The program includes the header file <gtk/gtk.h> for using the GTK+ library.

  2. Function gsplot:

    • This function takes a Cairo context (cairo_t *cr), x coordinate (int x), y coordinate (int y), and a scaling factor (double s) as parameters.
    • Inside the function:
      • It sets the source color to grayscale using cairo_set_source_rgb(cr, s, s, s).
      • It moves the cursor to the given coordinates (x+0.5, y) using cairo_move_to(cr, x+0.5, y).
      • It draws a vertical line with a length of 1 using cairo_rel_line_to(cr, 0, 1) and then strokes it using cairo_stroke(cr).
  3. Function expose_event:

    • This function handles the "expose" event, which occurs when the widget becomes visible or needs to be redrawn.
    • Inside the function:
      • It iterates through rows r and columns c to draw greyscale lines.
      • It sets the drawing parameters and initializes the Cairo context.
      • It iteratively calls gsplot to draw vertical lines at specific coordinates and with appropriate scaling.
      • Finally, it destroys the Cairo context using cairo_destroy(cr).
  4. Function main:

    • This is the entry point of the program.
    • It initializes the GTK+ library by calling gtk_init(&argc, &argv).
    • It creates a new top-level window using gtk_window_new(GTK_WINDOW_TOPLEVEL).
    • It connects the "expose-event" signal to the expose_event function.
    • It sets the default size of the window to 320x200 pixels.
    • It sets the widget to be paintable using gtk_widget_set_app_paintable(window, TRUE).
    • It makes the window visible using gtk_widget_show_all(window).
    • Finally, it starts the GTK+ main loop using gtk_main(), which processes events and keeps the window open.

In summary, this program uses GTK+ and Cairo to create a widget that displays a greyscale plot. It handles the widget's expose event to draw the plot using a specific algorithm that iteratively draws vertical lines with varying grayscale shades.

Source code in the c programming language

#include <gtk/gtk.h>
/* do some greyscale plotting */
void gsplot (cairo_t *cr,int x,int y,double s) {
    cairo_set_source_rgb (cr,s,s,s);
    cairo_move_to (cr,x+0.5,y);
    cairo_rel_line_to (cr,0,1);
    cairo_stroke (cr);
}
/* make a shaded widget */
gboolean expose_event (GtkWidget *widget,GdkEventExpose *event,gpointer data) {
    int r,c,x=0;
    cairo_t *cr;
    cr = gdk_cairo_create (widget->window);
    cairo_scale (cr,5,50);
    cairo_set_line_width (cr,1);
    for (r=0;r<4;r++) {
        c = (r&1)*64-(r%2);
        do gsplot (cr,x++%64,r,c/(1<<(3-r))/(8*(1<<r)-1.0));
        while ((c+=2*!(r%2)-1)!=(!(r%2))*64-(r%2));
    } cairo_destroy (cr);
    return FALSE;
}
/* main */
int main (int argc, char *argv[]) {
    GtkWidget *window;
    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect (window, "expose-event",G_CALLBACK (expose_event), NULL);
    g_signal_connect (window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_window_set_default_size (GTK_WINDOW(window), 320, 200);
    gtk_widget_set_app_paintable (window, TRUE);
    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}


  

You may also check:How to resolve the algorithm Hello world/Line printer step by step in the J programming language
You may also check:How to resolve the algorithm Brilliant numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Padovan sequence step by step in the Julia programming language
You may also check:How to resolve the algorithm Host introspection step by step in the Lua programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the PARI/GP programming language