Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.advocacy > #688454
| From | Farley Flud <ff@linux.rocks> |
|---|---|
| Subject | Re: Fuck This Shit! Let's Have A Programming Challenge. |
| Newsgroups | comp.os.linux.advocacy |
| References | <1831517ff78e51cc$64967$5317$802601b3@news.usenetexpress.com> |
| Message-Id | <pan$86275$50fbbc3c$ec6e5a08$484f5a5f@linux.rocks> |
| Date | 2025-03-30 17:36 +0000 |
| Organization | UsenetExpress - www.usenetexpress.com |
On Sat, 29 Mar 2025 16:02:20 +0000, Farley Flud wrote:
> Programming challenge. A big prize ($$$$) awaits the winner.
>
> Write a program, using only GNU/Linux languages/tools,
> to output the histogram data of an RGB (color) image file.
>
Time's up. Fucking deadbeat losers.
What follows is a masterpiece of C code that fulfills all the
requirements. The code is supremely efficient. Aside from
parallelism it cannot get any better.
Note that the code exploits the image I/O routines of the
Netpbm libraries. But the requirements state that any/all
GNU/Linux tools can be utilized.
All RGB images must first be converted to PNM format which
is a trivial matter.
Feast thine jaundiced, bloodshot eyeballs on ultimate C code.
=========================================================
/*
Reads PPM file and outputs histogram data to an ASCII file for
plotting with gnuplot or matlab.
compile: gcc CFLAGS -o histodata histodata.c -lm -lnetpbm
usage: histodata file.ppm > file.hst
Data columns are:
bin(0-maxval), luminosity(bt709), luminosity(bt601), red, green, blue
*/
#define _GNU_SOURCE
#include <netpbm/ppm.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int col, cols, row, rows, format;
unsigned int i, lum709, lum601, *hist;
double bt709r, bt709g, bt709b, bt601r, bt601g, bt601b;
pixval maxval, maxvalp;
pixel *pixelrow;
FILE *fd;
// conversion constants
bt709r=0.2126; bt709g=0.7152; bt709b=0.0722;
bt601r=0.2989; bt601g=0.5866; bt601b=0.1145;
// open to read file or stdin
if (argc > 1) {
if(!strcmp(argv[1],"-")) {
fd = stdin;
} else {
fd = fopen(argv[1],"r");
if (NULL == fd) {
fprintf(stderr, "Unable to open '%s': %s\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
}
} else {
fd = stdin;
}
ppm_readppminit(fd, &cols, &rows, &maxval, &format);
pixelrow=ppm_allocrow(cols);
maxvalp=maxval+1; // inc maxval for proper array indexing
hist = (unsigned int*)calloc((maxvalp)*5, sizeof(unsigned int));
for (row=0; row<rows; row++)
{
ppm_readppmrow(fd, pixelrow, cols, maxval, format); // Leaves pointer at next row
for(col=0; col<cols; col++)
{
lum709=(unsigned int)round(bt709r*(double)pixelrow[col].r+bt709g*(double)pixelrow[col].g+bt709b*(double)pixelrow[col].b);
lum601=(unsigned int)round(bt601r*(double)pixelrow[col].r+bt601g*(double)pixelrow[col].g+bt601b*(double)pixelrow[col].b);
++*(hist+lum709);
++*(hist+maxvalp+lum601);
++*(hist+2*maxvalp+pixelrow[col].r);
++*(hist+3*maxvalp+pixelrow[col].g);
++*(hist+4*maxvalp+pixelrow[col].b);
}
}
ppm_freerow(pixelrow);
fclose(fd);
fprintf(stdout,"%s\n%s %u\n%s\n","% Histogram data -- comma separated","% Total points:",rows*cols,"% bin lum709 lum601 red green blue");
for (i = 0; i <=maxval; i++)
{ fprintf(stdout,"%u,%u,%u,%u,%u,%u\n", i,*(hist+i),*(hist+maxvalp+i),*(hist+2*maxvalp+i),*(hist+3*maxvalp+i),*(hist+4*maxvalp+i)); }
free(hist);
exit(0);
}
--
Systemd: solving all the problems that you never knew you had.
Back to comp.os.linux.advocacy | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fuck This Shit! Let's Have A Programming Challenge. Farley Flud <fflud@gnu.rocks> - 2025-03-29 16:02 +0000
Re: Fuck This Shit! Let's Have A Programming Challenge. Farley Flud <ff@linux.rocks> - 2025-03-29 20:02 +0000
Re: Fuck This Shit! Let's Have A Programming Challenge. Farley Flud <ff@linux.rocks> - 2025-03-30 17:36 +0000
Re: Fuck This Shit! Let's Have A Programming Challenge. Stéphane CARPENTIER <sc@fiat-linux.fr> - 2025-03-30 21:53 +0000
Re: Fuck This Shit! Let's Have A Programming Challenge. vallor <vallor@cultnix.org> - 2025-03-31 01:31 +0000
Re: Fuck This Shit! Let's Have A Programming Challenge. candycanearter07 <candycanearter07@candycanearter07.nomail.afraid> - 2025-04-09 19:10 +0000
Re: Fuck This Shit! Let's Have A Programming Challenge. Stéphane CARPENTIER <sc@fiat-linux.fr> - 2025-04-12 09:11 +0000
csiph-web