Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Extensions To CImg

DZone's Guide to

Extensions To CImg

·
Free Resource
Extensions to CImg, also for interoperability with Fl_Image



#include "CImg.h"
#include 
  
   

using namespace cimg_library;

/* Generates a w*h*3 CImg
   
     image from the given rgba data and background colour. */
CImg
    
      cimg_from_rgba(unsigned char const* rgba,const unsigned int dimw,const unsigned int dimh, 
		const unsigned char back_r, const unsigned char back_g, const unsigned char back_b) {
	CImg
     
       res(dimw,dimh,1,3);
	unsigned char *pR = res.ptr(0,0,0,0), *pG = res.ptr(0,0,0,1), *pB=res.ptr(0,0,0,2);
	const unsigned char *ptrs = rgba;
	for (unsigned int off=res.width*res.height; off>0; --off) {
		*(pR++) = (unsigned char)((ptrs[0] * ptrs[3] + back_r * (255 - ptrs[3])) >> 8);
		*(pG++) = (unsigned char)((ptrs[1] * ptrs[3] + back_g * (255 - ptrs[3])) >> 8);
		*(pB++) = (unsigned char)((ptrs[2] * ptrs[3] + back_b * (255 - ptrs[3])) >> 8);
		ptrs += 4;
	}
	return res;
}

/* Generates a w*h*3 CImg
      
        image from the given rgb data. */
CImg
       
         cimg_from_rgb(unsigned char const* rgb,const unsigned int dimw,const unsigned int dimh) {
	CImg
        
          res(dimw,dimh,1,3);
	unsigned char *pR = res.ptr(0,0,0,0), *pG = res.ptr(0,0,0,1), *pB=res.ptr(0,0,0,2);
	const unsigned char *ptrs = rgb;
	for (unsigned int off=res.width*res.height; off>0; --off) {
		*(pR++) = (unsigned char)*(ptrs++);
		*(pG++) = (unsigned char)*(ptrs++);
		*(pB++) = (unsigned char)*(ptrs++);
	}
	return res;
}

/* Generates a FLTK RGB Image from the given CImg
         
           image. */
Fl_RGB_Image* image_from_cimg(CImg
          
            const & cimg) {
	const unsigned int wh = cimg.dimx() * cimg.dimy();
	unsigned char *buffer = new unsigned char[3*wh], *nbuffer=buffer;
	const unsigned char 
		*ptr1 = cimg.ptr(0,0,0,0),
		*ptr2 = cimg.dim>1?cimg.ptr(0,0,0,1):ptr1,
		*ptr3 = cimg.dim>2?cimg.ptr(0,0,0,2):ptr1;
	for (unsigned int k=0; k
           
             CImg
            
              get_resize_keep_aspect_ratio(CImg
             
               const & cimg, const unsigned int w, const unsigned int h) {
	if (cimg.dimx() * h > cimg.dimy() * w) { // cimg.dimx() / cimg.dimy() > w / h
		return cimg.get_resize(w, cimg.dimy() * w / cimg.dimx(), -100, -100, 5);
	} else {
		return cimg.get_resize(cimg.dimx() * h / cimg.dimy(), h, -100, -100, 5);
	}
}

/* Returns a resized image to a maximum of the given dimensions, keeping its aspect ratio,
 * only if the dimensions given are smaller than the original dimensions. */
template
              
                CImg
               
                 get_resize_if_smaller_keep_aspect_ratio(CImg
                
                  const & cimg, const unsigned int w, const unsigned int h) {
	if (w < cimg.dimx() || h < cimg.dimy())
		return get_resize_keep_aspect_ratio(cimg, w, h);
	else
		return CImg
                 
                  (cimg);
}


                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}