void Flcc_ValueBox::generate() { int X = 0, Y = 0, W = w(), H = h(); box()->inset(X,Y,W,H); Fl_Image *im = new Fl_Image(W, H, 32); uint32 *dst = (uint32*)im->data(); uint32 rgb; int skip = (im->pitch() - W * im->bytespp()) >> 2; for(int y = 0; y < H; y++) { float Yf = 255*(1.0-float(y)/H); fl_rgb888_from_rgb(rgb, uchar(tr*Yf+.5f), uchar(tg*Yf+.5f), uchar(tb*Yf+.5f)); for(int x = 0; x < W; x++) { *dst++ = rgb; } dst += skip; } if(bg) delete bg; bg = im; }
void Flcc_HueBox::generate() { int X = 0, Y = 0, W = w(), H = h(); box()->inset(X,Y,W,H); #ifdef UPDATE_HUE_BOX const float V = ((Fl_Color_Chooser*)(parent()))->v(); #else const float V = 1.0f; #endif Fl_Image *im = new Fl_Image(W, H, 32); uint32 *dst = (uint32 *)im->data(); int skip = (im->pitch() - W * im->bytespp()) >> 2; register float r,g,b; for(int y = 0; y < H; y++) { float Yf = (float)y / H; for (int x = 0; x < W; x++) { float Xf = (float)x / W; float H,S; tohs(Xf, Yf, H, S); Fl_Color_Chooser::hsv2rgb(H,S,V,r,g,b); fl_rgb888_from_rgb(*dst++, uchar(255*r+.5f), uchar(255*g+.5f), uchar(255*b+.5f)); } dst += skip; } if(bg) delete bg; bg = im; }
Fl_Image *make_bg(int w, int h) { #ifdef GENERATE_16BIT Fl_Image *ret = new Fl_Image(w, h, 16, 0, true, 0x0000F000, 0x00000F00, 0x000000F0, 0x0000000F); #else Fl_Image *ret = new Fl_Image(w, h, 32, 0, true, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF); #endif printf("BG Format:\n Rmask: 0x%08x\n Gmask: 0x%08x\n Bmask: 0x%08x\n", ret->format()->Rmask, ret->format()->Gmask, ret->format()->Bmask); // pitch = WORD alignment bits per line int pitch=Fl_Renderer::calc_pitch(ret->format()->bytespp, w); int skip = pitch - w * ret->format()->bytespp; uint8 *p = ret->data(); uint8 r,g,b; for (int y = 0; y < h; y++) { double Y = double(y)/(h-1); for (int x = 0; x < w; x++) { double X = double(x)/(w-1); r = uchar(255*((1-X)*(1-Y))); // red in upper-left g = uchar(255*((1-X)*Y)); // green in lower-left b = uchar(255*(X*Y)); // blue in lower-right fl_assemble_rgb(p, ret->format()->bytespp, ret->format(), r, g, b); p+=ret->format()->bytespp; } p+=skip; } return ret; }
/* ============================================================================= =============================================================================== */ int C3DSceneMgr::createUITexture(string strFileName, int &width, int &height, int &txWidth) { Fl_Image * img = openTextureFile(strFileName); if (img == NULL) { return -1; } width = img->w(); height = img->h(); // need to add an alpha channel to properly overlay on existing textures const unsigned char *pData = NULL; if (img->d() == 3) pData = makeRGBA((const unsigned char *) img->data()[0], width, height); else pData = (const unsigned char *) img->data()[0]; GLuint textureID; glGenTextures(1, &textureID); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glBindTexture(GL_TEXTURE_2D, textureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); int txHeight = height; txWidth = (width <= 64) ? 64 : (width <= 128) ? 128 : width; const unsigned char *pDataNew = NULL; if (width != txWidth) { pDataNew = padOut(pData, width, height, txWidth); txHeight = txWidth; } glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, txWidth, txHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pDataNew ? pDataNew : pData); if (pDataNew) delete[] pDataNew; if (img->d() == 3) delete[] pData; delete img; return textureID; }
Fl_Image *Fl_Image_Filter::apply_to_new(Fl_Image *image, Fl_Rect *rect, Fl_Image_Filter *filter, float val1, float val2, float val3) { Fl_Rect r(0,0,image->width(),image->height()); if(!rect) { rect = &r; } Fl_Image *ret = new Fl_Image(*image); uint8 *data = ret->data(); if(!filter->execute(&data, *rect, ret->pitch(), ret->format(), val1, val2, val3)) { delete ret; return 0; } return ret; }
Fl_Image *Fl_Image::scale(int W, int H) { Fl_Image *ret = new Fl_Image(W, H, bitspp()); ret->format()->copy(format()); Fl_Rect olds(0,0,width(),height()); Fl_Rect news(0,0,W,H); bool success = Fl_Renderer::stretch(m_data, bytespp(), pitch(), &olds, ret->data(), bytespp(), ret->pitch(), &news); if(!success) { delete ret; ret = 0; } return ret; }
/* ============================================================================= =============================================================================== */ int C3DSceneMgr::createTexture(string strFileName, int &width, int &height, int iTxMode) { if (strFileName == "") // Return from the function if no file name was passed in return -1; if (!fileexists(strFileName)) return -1; // Load the image and store the data int textureID = -1; if (getExt(strFileName) == ".dds") { DDS_IMAGE_DATA *pDDSImageData = NULL; if ((pDDSImageData = loadDDSTextureFile(strFileName)) != NULL) { textureID = createTextureDDS(pDDSImageData); height = pDDSImageData->sizeY; width = pDDSImageData->sizeX; } else // case where worldwind wraps jpegs in dds files { Fl_RGB_Image *img = new Fl_JPEG_Image(strFileName.c_str()); if (img->d() == 0) return -1; width = img->w(); height = img->h(); const unsigned char *pData = (const unsigned char *) img->data()[0]; textureID = createTexture(pData, width, height, GL_RGB, GL_RGB, iTxMode); delete img; } } else { Fl_Image * img = openTextureFile(strFileName); if (img == NULL) return -1; width = img->w(); height = img->h(); const unsigned char *pData = (const unsigned char *) img->data()[0]; textureID = createTexture(pData, width, height, img->d() == 4 ? GL_RGBA : GL_RGB, GL_RGBA, iTxMode); delete img; } return textureID; }
void C3DSceneMgr::createNewTileTexture(string strFileName) { Fl_Image * img = openTextureFile(strFileName); if (img == NULL) return; int width = img->w(); int height = img->h(); const unsigned char *pOld = (const unsigned char *) img->data()[0]; //image processing on water unsigned char *pData = NULL; if (!mem_alloc(pData, width * height * 3)) return; unsigned char *p = pData; for (int i = 0; i < width * height; i++, p+=3, pOld+=3) { p[0] = pOld[0]; //red p[1] = pOld[1]; //green if (pOld[2] < 220 && pOld[2] > pOld[1]*5/4 && pOld[2] > pOld[0]*5/4) p[2] = pOld[2] + (220-pOld[2])/4; else p[2] = pOld[2]; //blue } string strnew = strFileName; setExt(strnew, ".png"); saveImage(strnew, pData, width, height); int txID = createTexture(pData, width, height, GL_RGB, GL_RGB, TX_CLAMP_EDGE); delete pData; delete img; setExt(strFileName, ".dds"); saveDDSTextureFile(strFileName, txID); deleteTexture(txID); }
virtual Bitmap *Load(IStream *stream) { SPADES_MARK_FUNCTION(); // read all std::string data = stream->ReadAllBytes(); // copy to buffer Fl_Image *img = LoadFltkImage(data); SPAssert(img); SPAssert(img->count() >= 1); const unsigned char* inPixels = (const unsigned char *)img->data()[0]; int depth = img->d(); int width = img->w(); int height = img->h(); int pitch = width * depth + img->ld(); Handle<Bitmap> bmp; try { bmp = new Bitmap(width, height); } catch(...) { delete img; throw; } try { unsigned char *outPixels = (unsigned char *)bmp->GetPixels(); if(pitch == width * 4 && depth == 4) { // if the format matches the requirement of Bitmap, // just use it memcpy(outPixels, inPixels, pitch * height); } else { // convert const unsigned char* line; for(int y = 0; y < height; y++) { line = inPixels; for(int x = 0; x < width; x++) { uint8_t r, g, b, a; switch(depth) { case 1: r = g = b = *(line++); a = 255; break; case 2: r = g = b = *(line++); a = *(line++); break; case 3: r = *(line++); g = *(line++); b = *(line++); a = 255; break; case 4: r = *(line++); g = *(line++); b = *(line++); a = *(line++); break; default: SPAssert(false); } *(outPixels++) = r; *(outPixels++) = g; *(outPixels++) = b; *(outPixels++) = a; } inPixels += pitch; } } delete img; return bmp.Unmanage(); } catch(...) { delete img; throw; } }
WPaper *make_image(Fl_Color bg_color, Fl_Image *im, int w, int h, int mode, uchar opacity=255) { // secret box render function from Fl_Image :) extern uint8 *render_box(int w, int h, int bitspp, uint color, Fl_Colormap *pal, uint8 *buffer); Fl_PixelFormat *fmt = Fl_Renderer::system_format(); WPaper *bg_image = new WPaper(w, h, Fl_Renderer::system_format()); int iw=im->width(), ih=im->height(); int ix=0, iy=0; int xoff=0, yoff=0; Fl_Image *newim = im; switch(mode) { //CENTER case 0: { ix=(w/2)-(iw/2); iy=(h/2)-(ih/2); if(ix<0) xoff=-ix; if(iy<0) yoff=-iy; if(ix<0) ix=0; if(iy<0) iy=0; } break; //STRECH case 1: { ix=0, iy=0, iw=w, ih=h; if(w!=im->width()||h!=im->height()) { newim = im->scale(w,h); } } break; //STRETCH ASPECT case 2: { int pbw = w, pbh = h; iw = pbw; ih = iw * im->height() / im->width(); if(ih > pbh) { ih = pbh; iw = ih * im->width() / im->height(); } ix=(w/2)-(iw/2), iy=(h/2)-(ih/2); if(ix<0) ix=0; if(iy<0) iy=0; if(iw>w) iw=w; if(ih>h) ih=h; if(iw!=im->width()||ih!=im->height()) { newim = im->scale(iw,ih); } } break; } // This could be an option, opacity newim->format()->alpha = opacity; if( (iw<w || ih<h) || newim->format()->alpha!=255) { // If image doesnt fill the whole screen, or opacity < 255 // fill image first with bg color. render_box(w, h, fmt->bitspp, bg_color, fmt->palette, bg_image->data()); } if(iw>w) iw=w; if(ih>h) ih=h; Fl_Rect r(xoff, yoff, iw, ih); Fl_Rect r2(ix,iy, iw, ih); if(newim->format()->alpha>0) { // Blit image data to our bg_image bg_image->check_map(newim->format()); Fl_Renderer::alpha_blit(newim->data(), &r, newim->format(), newim->pitch(), bg_image->data(), &r2, bg_image->format(), bg_image->pitch(), 0); } if(newim!=im) delete newim; return bg_image; }
Fl_Image *make_ball(int radius) { uint8 trans, alphamask; int range, addition; int xdist, ydist; uint16 x, y; uint16 skip; uint32 pixel; Fl_Image *light; #ifdef GENERATE_16BIT uint16 *buf; /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */ /* Note: this isn't any faster than a 32 bit alpha surface */ alphamask = 0x0000000F; light = new Fl_Image(2*radius, 2*radius, 16, 0, true, 0x0000F000, 0x00000F00, 0x000000F0, alphamask); #else uint32 *buf; /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */ alphamask = 0x000000FF; light = new Fl_Image(2*radius, 2*radius, 32, 0, true, 0xFF000000, 0x00FF0000, 0x0000FF00, alphamask); #endif /* Fill with a light yellow-orange color */ skip = light->pitch()-(light->width()*light->format()->bytespp); #ifdef GENERATE_16BIT buf = (uint16 *)light->data(); #else buf = (uint32 *)light->data(); #endif /* Get a tranparent pixel value - we'll add alpha later */ pixel = light->format()->map_rgba(0xFF, 0xDD, 0x88, 0); for ( y=0; y<light->height(); ++y ) { for ( x=0; x<light->width(); ++x ) { *buf++ = pixel; } buf += skip; /* Almost always 0, but just in case... */ } /* Calculate alpha values for the surface. */ #ifdef GENERATE_16BIT buf = (uint16 *)light->data(); #else buf = (uint32 *)light->data(); #endif for ( y=0; y<light->height(); ++y ) { for ( x=0; x<light->width(); ++x ) { /* Slow distance formula (from center of light) */ xdist = x-(light->width()/2); ydist = y-(light->height()/2); range = (int)sqrt(xdist*xdist+ydist*ydist); /* Scale distance to range of transparency (0-255) */ if ( range > radius ) { trans = alphamask; } else { /* Increasing transparency with distance */ trans = (uint8)((range*alphamask)/radius); /* Lights are very transparent */ addition = (alphamask+1)/16; if ( (int)trans+addition > alphamask ) { trans = alphamask; } else { trans += addition; } } /* We set the alpha component as the right N bits */ *buf++ |= (255-trans); } buf += skip; /* Almost always 0, but just in case... */ } return light; }