//-------------------------------------------------------------------------- static unsigned int HSLtoRGB( bool bRealRgb, unsigned int H, unsigned int S, unsigned int L) { if (S == 0) { // achromatic return make_rgb(bRealRgb, L, L, L); } double h = (double)H*360/255; double s = (double)S / 255; double l = (double)L / 255; double rm1, rm2; if (l <= 0.5) rm2 = l + l * s; else rm2 = l + s - l * s; rm1 = 2.0 * l - rm2; return make_rgb(bRealRgb, to_rgb(rm1, rm2, h + 120.0), to_rgb(rm1, rm2, h), to_rgb(rm1, rm2, h - 120.0) ); }
uint32_t color::get_random_color(uint8_t min_component_value, uint8_t max_component_value) { max_component_value -= min_component_value; return make_rgb(min_component_value + rand() % max_component_value, min_component_value + rand() % max_component_value, min_component_value + rand() % max_component_value); }
static COMMAND_FUNC( do_make_rgb ) { int r,g,b,base; base=(int)HOW_MANY("base color index"); r=(int)HOW_MANY("number of red levels"); g=(int)HOW_MANY("number of green levels"); b=(int)HOW_MANY("number of blue levels"); CHECK_DPYP("do_make_rgb") make_rgb(base,r,g,b); }
Cursor::Cursor() : _surface(Ego::Graphics::SDL::createSurface(8, 8)) { uint32_t col = make_rgb(_surface, Ego::Math::Colour3b::white()); // opaque (255) white (255,255,255) uint32_t loc = make_rgb(_surface, Ego::Math::Colour3b(24, 24, 24)); // opaque (255) black-grey (24,24,24) uint32_t clr = make_rgba(_surface, Ego::Math::Colour4b(0, 0, 0, 64)); // almost transparent (64) black (0,0,0) // Simple triangle SDL_Rect rtmp; rtmp.x = 0; rtmp.y = 0; rtmp.w = 8; rtmp.h = 1; SDL_FillRect(_surface.get(), &rtmp, loc); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (x + y < 8) Ego::Graphics::SDL::putPixel(_surface, x, y, col); else Ego::Graphics::SDL::putPixel(_surface, x, y, clr); } } }
Colour Pattern_rain::get_colour() const { switch (m_mode) { case RAIN_RANDOM_COLOUR: default: return make_wheel(random(MAX_WHEEL), g_brightness.get()); case RAIN_CURRENT_HUE: return make_hue(g_hue.get()); case RAIN_SINGLE_RANDOM_COLOUR: return make_wheel(m_wheel, g_brightness.get()); case RAIN_PURE_WHITE: return make_rgb(MAX_BRIGHTNESS, MAX_BRIGHTNESS, MAX_BRIGHTNESS); } }
GUI_Cursor::GUI_Cursor() : _surface(Ego::Graphics::SDL::createSurface(8, 8)) { uint32_t col = make_rgb(_surface, 255, 255, 255); // white (255,255,255), fully opaquw uint32_t loc = make_rgb(_surface, 24, 24, 24); // black-grey (24,24,24), fully opaque uint32_t clr = make_rgba(_surface, 0, 0, 0, 64); // black (0,0,0), almost transparent (64) // Simple triangle SDL_Rect rtmp; rtmp.x = 0; rtmp.y = 0; rtmp.w = 8; rtmp.h = 1; SDL_FillRect(_surface.get(), &rtmp, loc); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (x + y < 8) Ego::Graphics::SDL::putPixel(_surface, x, y, col); else Ego::Graphics::SDL::putPixel(_surface, x, y, clr); } } }
void *stream_func(void *ptr) { unsigned char *rgb_buffer; int ready_buf; char cur_name[64]; int i; if( b_shared_mem ) { rgb_buffer = p_shm; } else { rgb_buffer = (unsigned char *)malloc(req_width*req_height*3); } for(i=0; i<total_buffers; i++) { queue_buffer(i); } for(;;) { /* get the idx of ready buffer */ for(i=0; i<total_buffers; i++) { /* Check if the thread should stop. */ if( stream_finish ) return NULL; ready_buf = dequeue_buffer(); if( b_verbose ) { printf("Buffer %d ready. Length: %uB\n", ready_buf, image_buffers[ready_buf].length); } switch( check_pixelformat() ) { case V4L2_PIX_FMT_YUYV: /* convert data to rgb */ if( b_shared_mem ) sem_down(&shm_sem); if( convert_yuv_to_rgb_buffer( (unsigned char *)(image_buffers[ready_buf].start), rgb_buffer, req_width, req_height) == 0 ) { if( b_verbose ) { printf("\tConverted to rgb.\n"); } } if( b_shared_mem ) sem_up(&shm_sem); break; default: print_pixelformat(stderr); fprintf(stderr,"\n"); return NULL; } /* make the image */ /* create the file name */ if( b_named_pipe ) sprintf(cur_name, "%s", psz_named_pipe); switch( e_outfmt ) { case FORMAT_BMP: if( b_shared_mem ) { printf("Unsupported!\n"); break; } make_bmp(rgb_buffer, cur_name, req_width, req_height); break; case FORMAT_RGB: if( b_shared_mem ) { /* The buffer is already rgb :) */ break; } make_rgb(rgb_buffer, cur_name, req_width, req_height); break; default: fprintf(stderr, "Not supported format requested!\n"); break; } queue_buffer(ready_buf); } } return NULL; }
void *capture_func(void *ptr) { unsigned char *rgb_buffer; int ready_buf; char cur_name[64]; int i; struct timeval timestamp; if( b_shared_mem ) { rgb_buffer = p_shm; } else { rgb_buffer = (unsigned char *)malloc(req_width*req_height*3); } for(;;) { /* Wait for the start condition */ pthread_mutex_lock(&cond_mutex); pthread_cond_wait(&condition, &cond_mutex); pthread_mutex_unlock(&cond_mutex); /* queue one buffer and 'refresh it' */ /* @todo Change 2 to some #define */ for(i=0; i<2; i++) { queue_buffer(i); } for(i=0; i<2 - 1; i++) { dequeue_buffer(); } /* get the idx of ready buffer */ ready_buf = dequeue_buffer(); if( b_verbose ) { printf("Buffer %d ready. Length: %uB\n", ready_buf, image_buffers[ready_buf].length); } switch( check_pixelformat() ) { case V4L2_PIX_FMT_YUYV: /* convert data to rgb */ if( convert_yuv_to_rgb_buffer( (unsigned char *)(image_buffers[ready_buf].start), rgb_buffer, req_width, req_height) == 0 ) { if( b_verbose ) { printf("\tConverted to rgb.\n"); } } break; default: print_pixelformat(stderr); fprintf(stderr,"\n"); return NULL; } timestamp = query_buffer(0); /* make the image */ /* create the file name */ if( b_named_filename ) { sprintf(cur_name, "%s%s", psz_output_dir, psz_output_filename); } else if( !b_named_pipe ) sprintf(cur_name, "%scamshot_%lu.bmp", psz_output_dir, timestamp.tv_sec); else sprintf(cur_name, "%s", psz_named_pipe); switch( e_outfmt ) { case FORMAT_BMP: make_bmp(rgb_buffer, cur_name, req_width, req_height); break; case FORMAT_RGB: make_rgb(rgb_buffer, cur_name, req_width, req_height); break; default: fprintf(stderr, "Not supported format requested!\n"); break; } } return NULL; }
main(int argc, char *argv[]) { char *file; /* data file name */ int ok, help, chann, nchan; /* cmd line options */ int type; /* cmd line options */ int showmap; /* more cmd line opts */ int probe; /* daffie probe */ int i, j, k; /* loop counters */ int nitem; /* total data, items per channel */ int xs, ys, prevxs, prevys; /* screen size */ int ix, iy; /* screen coords */ int x0; /* panned data origin */ int pan; /* pan value */ int key; /* mouse key on press */ int imgxs, imgys, *img; /* image data */ double range, dscale; /* data range/scale */ float xscale, yscale, off; /* data offset & scaling*/ float v; /* a datum */ int select[MAXCHANN], nselect; /* select channels */ char title[300] = {'\0'}; /* title bar */ char *cmapfile = NULL; /* color map file name */ COLORMAP cmap[MAX_CMAP]; /* color map from above */ int ncmap; /* # entriesin above */ int cindx[256]; /* allocated (X) color cells */ int crgb[256]; /* corresponding rgb val */ int chindx[256]; /* color index by channel*/ int plotx0, ploty0; /* base of plot area */ Pgm = argv[0]; if (streq(Pgm, "app_main")) Pgm = "wplot"; ++argv; --argc; chann = 0; nchan = 0; help = 0; ok = 1; type = 0; showmap = 0; Sample = 1; probe = 0; while (argc && *argv[0] == '-') { char *opt; opt = argv[0]+1; ++argv; --argc; if (substr(opt, "help")) ++help; else if (substr(opt, "scatter")) type |= T_SCATTER; else if (substr(opt, "line")) type |= T_LINE; else if (substr(opt, "colormapped")) type |= T_COLORMAP; else if (substr(opt, "stacked")) type |= T_STACKED; else if (substr(opt, "showmap")) showmap = 1; else if (substr(opt, "probe")) probe = 1; else if (substr(opt, "ascii")) Binary = 0; else if (substr(opt, "binary")) Binary = 1; else if (substr(opt, "bin64")) Binary = BinDouble = 1; else if (substr(opt, "autopan")) Autopan = 1; else if (substr(opt, "channel")) { char *s; int x; if (!argc || !isdigit(*argv[0])) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; continue; } s = argv[0]; ++argv; --argc; nchan = chann = 0; while (*s && isdigit(*s)) nchan = 10*nchan + *s++ - '0'; if (*s) ++s; while (*s && isdigit(*s)) chann = 10*chann + *s++ - '0'; if (!nchan) { fprintf(stderr, "%s: invalid argument to -%s\n", Pgm, opt); ok = 0; } } else if (substr(opt, "dimensions")) { int i = 0; int x; char *s; if (!argc || !isdigit(*argv[0])) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; continue; } s = argv[0]; i = 0; --s; do { x = 0; ++s; while (isdigit(*s)) x = 10*x + *s++ -'0'; Dim[i++] = x; } while (*s == '.' || *s == 'x'); ++argv; --argc; } else if (substr(opt, "pan")) { if (!argc || !isdigit(*argv[0])) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; continue; } Pan = 0.01 * atoi(argv[0]); ++argv; --argc; } else if (substr(opt, "sample")) { if (!argc || !isdigit(*argv[0])) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; continue; } Sample = atoi(argv[0]); ++argv; --argc; } else if (substr(opt, "title")) { if (!argc || !isdigit(*argv[0])) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; continue; } strcpy(title, argv[0]); ++argv; --argc; } else if (substr(opt, "range")) { if (argc < 2) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; continue; } ok &= sscanf(argv[0], " %f", &RangeMin); ok &= sscanf(argv[1], " %f", &RangeMax); if (!ok) { fprintf(stderr, "%s: invalid arguments to -%s\n", Pgm, opt); } Autorange = Rerange = 0; argv += 2; argc -= 2; } else if (substr(opt, "map")) { if (!argc) { fprintf(stderr, "%s: missing argument to -%s\n", Pgm, opt); ok = 0; } else { cmapfile = argv[0]; ++argv; --argc; } } else if (substr(opt, "double")) Double = 1; else if (streq(opt, "db")) Double = 1; else { fprintf(stderr, "%s: ignoring argument -%s\n", Pgm, opt); ok = 0; } } if (!ok || help || !argc) { usage(Pgm, help); exit(!help); } /* See if -dim was given if not -chan */ if (!nchan && Dim[1]) nchan = Dim[1]; if (nchan) Nchan = nchan; else Nchan = 1; file = argv[0]; ++argv; --argc; /* Allocate storage for the data */ Data = (float *)malloc(MAXDATA * sizeof(float)); if (!Data) { fprintf(stderr, "%s: not enough memory for %d datum\n", Pgm, MAXDATA); exit(1); } /* * Read in data file */ if (substr("daffie://", file) || substr("//", file)) probe = 1; if (!probe) readfile(file); else setprobe(file); if (Autorange) { RangeMin = DataMin; RangeMax = DataMax; } range = RangeMax - RangeMin; dscale = 1.0/range; printf("%s: read %d items (min = %f, max = %f)\n", Pgm, Ndata, DataMin, DataMax); if (Nchan > 1) printf("%s: display channel %d of %d\n", Pgm, chann, Nchan); PlotType = type; if (!title[0]) sprintf(title, "%s %s", Pgm, file); xw_title(title); /* Initialize display */ xw_size(1000, 512); xw_init(); for (i = 0; i < 8; ++i) xw_setcol(i, (i&1)?255:0, (i&2)?255:0, (i&4)?255:0); /* Initializing RGB byte ordering table */ make_rgb_ordering(); xw_color(7); #ifdef XWINDOWS /* Create back buffer */ if (Double) { int xs, ys; xw_qsize(&xs, &ys); BackBuf = xw_unused(); FrontBuf = xw_select(BackBuf); xw_pixmap(1); xw_size(xs, ys); xw_init(); for (i = 0; i < 8; ++i) xw_setcol(i, (i&1)?255:0, (i&2)?255:0, (i&4)?255:0); xw_select(FrontBuf); } #endif /* Create color map */ ncmap = make_colormap(cmapfile, cmap, MAX_CMAP); for (i = 0; i < 128; ++i) { int k, ci, rgb; j = ncmap-1 - ((float)i)/127.0 * (ncmap-1); ci = xw_alloccol(cmap[j].r, cmap[j].g, cmap[j].b); /**** printf("Allocated color cell %d for index %d (%d %d %d)\n", k, i<<1, cmap[j].r, cmap[j].g, cmap[j].b); ****/ #ifdef XWINDOWS if (Double) { xw_select(BackBuf); xw_setcol(ci, cmap[j].r, cmap[j].g, cmap[j].b); xw_select(FrontBuf); } #endif k = i<<1; cindx[k] = ci; cindx[k+1] = ci; /***** rgb = (cmap[j].r<<16) | (cmap[j].g<<8) | cmap[j].b; *****/ rgb = make_rgb(cmap[j].r, cmap[j].g, cmap[j].b); crgb[k] = rgb; crgb[k+1] = rgb; } if (Nchan) for (i = 0; i < Nchan; ++i) { j = ((float) i)/(Nchan-1) * 255.0; /**** printf("Setting channel %d to cmap entry %d, color cell %d\n", i, j, cindx[j]); ****/ chindx[i] = cindx[j]; } if (Nchan && !chann) for (i = 0; i < Nchan; ++i) select[i] = 1; else for (i = 0; i < Nchan; ++i) select[i] = (i == chann-1); if (showmap) { int xs, ys, box0, box1; float bs; xw_qsize(&xs, &ys); bs = (float)xs / (float)Nchan; for (i = 0; i < Nchan; ++i) { printf("Channel %d - color index %d\n", i, chindx[i]); xw_color(chindx[i]); box0 = i*bs; box1 = (i+1)*bs - 1; xw_move(box0, 0); xw_flbox(box1, ys-1); } printf("Click in window to continue...\n"); xw_xyin(&ix, &iy); } /* Initialize user interface */ startui(); x0 = 0; img = NULL; prevxs = prevys = 0; while (!Done) { char text[100]; int border = 4; int th, tw, ta, td; /* text metrics */ updateui(); /* Verify/allocate an in-core image buffer for the display */ xw_qsize(&xs, &ys); if (!img || imgxs != xs || imgys != ys) { if (img) free(img); img = (int *)malloc(xs*ys*sizeof(int)); if (!img) { fprintf(stderr, "Not enough memory for %dx%d image buffer\n", xs, ys); exit(4); } imgxs = xs; imgys = ys; memset(img, 0, xs*ys*sizeof(int)); } /* Redraw if window size changed */ if (xs != prevxs || ys != prevys) { prevxs = xs; prevys = ys; Redraw = 1; } /* * Set effective number of items to plot given number of * channels and sampling size */ nitem = Ndata/(Nchan*Sample); /* Do any desired panning */ pan = Pan * xs; /* pan amount */ if (PanLeft) x0 = max(x0-pan, 0); else if (PanRight) x0 = min(x0+pan, max(0,nitem-xs)); if (!KeepTrucking) PanLeft = PanRight = 0; else { unsigned long long now, elapsed; now = event_clock(); elapsed = now-Lastdraw; if (elapsed < MINREDRAW) event_sleep(NORMREDRAW-elapsed); } if (!Redraw && !KeepTrucking) { event_sleep(20); continue; } Redraw = 0; #ifdef XWINDOWS /* Use double buffer on X Windows displays */ if (Double) xw_select(BackBuf); #endif /* Compute data range if necessary */ if (Autorange && Rerange) { if (DataMin < RangeMin) { float delta = RangeMin - DataMin; while (RangeInc < delta) { if (9.0*RangeInc >= delta) while (RangeInc < delta) RangeInc += RangeInc; else RangeInc *= 10.0; } RangeMin -= RangeInc; printf("Set Range min to %f\n", RangeMin); } if (DataMax > RangeMax) { float delta = DataMax - RangeMax; while (RangeInc < delta) { if (9.0*RangeInc >= delta) while (RangeInc < delta) RangeInc += RangeInc; else RangeInc *= 10.0; } RangeMax += RangeInc; printf("Set Range max to %f\n", RangeMax); } Rerange = 0; } range = RangeMax-RangeMin; if (range > EPSILON) dscale = 1.0/range; else dscale = 1.0; off = -RangeMin; yscale = ys*dscale; /* * Redraw the display */ xw_erase(); xw_color(7); xw_move(0, 0); plotx0 = 0; ploty0 = 1; nselect = 0; for (k = 0; k < Nchan; ++k) if (select[k]) ++nselect; if (!nselect) { printf("nothing selected!!!"); break; } if (PlotType & T_STACKED) yscale /= Nchan; if (PlotType & T_COLORMAP) { if (!img) for (i = 0; i < min(xs, nitem); ++i) { float yband; int iy; yband = (float)(ys - ploty0) / (float)Nchan; ix = plotx0 + i; xw_move(ix, ploty0); for (k = 0; k < Nchan; ++k) { int ci; /****** v = Data[Nchan*(x0 + i*Sample) + k]; *****/ v = datum(Data, x0+i, k, Sample); ci = (v+off)*dscale * 255.0 + 0.5; xw_color(cindx[ci]); iy = (k+1) * yband; xw_draw(ix, iy); } } else { for (i = 0; i < min(xs, nitem/Sample); ++i) { float yband; int iy0, iy1; int *pix; yband = (float)(ys - ploty0) / (float)Nchan; ix = plotx0 + i; iy0 = ploty0; pix = img + imgxs * (imgys-1) + ix; for (k = 0; k < Nchan; ++k) { int ci, rgbcol; /******* v = Data[Nchan*(x0 + i*Sample) + k]; *******/ v = datum(Data, x0+i, k, Sample); ci = (v+off)*dscale * 255.0 + 0.5; rgbcol = crgb[ci]; iy1 = (k+1) * yband; while (iy0++ <= iy1) { *pix = rgbcol; pix -= xs; } --iy0; } } /* Fill remainder of image with black */ for (ix = i; ix < imgxs; ++ix) for (iy = ploty0; iy < imgys; ++iy) img[imgxs*iy + ix] = 0; /* Send above created image to the display */ xw_showimage(img, i, imgys, 0, imgys-1, 0); } } else for (k = 0; k < Nchan; ++k) { if (!select[k]) continue; if (nselect > 1) { /*** printf("plotting chan %d color %d\n", k, chindx[k]); ***/ xw_color(chindx[k]); } else xw_color(7); if (PlotType & T_STACKED) ploty0 = k*(float)ys/(float)Nchan; for (i = 0; i < min(xs, nitem/Sample); i += Sample) { v = Data[Nchan*(x0 + i*Sample) + k]; ix = plotx0 + i; iy = ploty0 + yscale*(v+off); if (PlotType & T_SCATTER || !i) xw_move(ix, iy); xw_draw(ix, iy); } } if (!(PlotType & (T_STACKED|T_COLORMAP))) { iy = yscale*off; xw_color(1); xw_move(plotx0, 1+iy); xw_draw(xs, 1+iy); } xw_color(7); sprintf(text, "%d", Domain_X0/Nchan + x0); xw_qtext(text, &tw, &th, &ta, &td); xw_move(border, border); xw_text(text); xw_move(border, ys - border - th); xw_text(text); sprintf(text, "%d", min(nitem, x0+xs*Sample-1)); xw_qtext(text, &tw, &th, &ta, &td); xw_move(xs-tw-border, border); xw_text(text); xw_move(xs-tw-border, ys - border - th); xw_text(text); xw_flush(); #ifdef XWINDOWS if (Double) { int xs, ys; xw_qsize(&xs, &ys); xw_select(FrontBuf); xw_copyfrom(BackBuf, 0, 0, xs, ys, 0, 0); xw_flush(); } #endif Lastdraw = event_clock(); } #ifdef XWINDOWS xw_erase(); xw_flush(); sleep(1); xw_fini(); #endif }