// compile ... // gcc -std=gnu99 -Wall image.c -o image `pkg-config --cflags --libs gtk+-3.0` // on thales@mathematik.uni-ulm.de #include #include #include #include #include #include bool string_to_int(char* s, int* val) { char* endptr; errno = 0; /* reset errno */ int i = strtol(s, &endptr, 10); if (endptr == s) return false; /* conversion failed */ if (*endptr) return false; /* unexpected characters behind the integer */ if (errno) return false; /* out of range */ *val = i; return true; } bool string_to_double(char* s, double* val) { char* endptr; errno = 0; /* reset errno */ double x = strtod(s, &endptr); if (endptr == s) return false; /* conversion failed */ if (*endptr) return false; /* unexpected characters behind the double */ if (errno) return false; /* out of range */ *val = x; return true; } int main(int argc, char** argv) { g_type_init(); // parameters int width = 100; int height = 100; // read command line arguments // ... // check arguments // ... // create image buffer: GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height); // more info: http://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-The-GdkPixbuf-Structure.html int n_channels = gdk_pixbuf_get_n_channels (pixbuf); // == 3 because is_alpha(pixbuf) == FALSE // rowstride: number of bytes between the start of a row and the start of the next row // --> also einfach Laenge einer Zeile; das habe ich in der Uebung falsch erklaert: Entschuldigung! int rowstride = gdk_pixbuf_get_rowstride (pixbuf); guchar* pixels = gdk_pixbuf_get_pixels (pixbuf); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { // get pixel p guchar* p = pixels + y * rowstride + x * n_channels; // set RGB p[0] = 0; // red p[1] = 255; // green p[2] = 0; // blue } } // save image gdk_pixbuf_save (pixbuf, "image.jpg", "jpeg", NULL, "quality", "100", NULL); }