DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Extensions To CImg

Snippets Manager user avatar by
Snippets Manager
·
Jan. 18, 07 · · Code Snippet
Like (0)
Save
Tweet
627 Views

Join the DZone community and get the full member experience.

Join For Free
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);
}

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Determine if Microservices Architecture Is Right for Your Business?
  • Transactions vs. Analytics in Apache Kafka
  • Kafka Fail-Over Using Quarkus Reactive Messaging
  • Top Soft Skills to Identify a Great Software Engineer

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo