<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: image code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:38:04 GMT</pubDate>
    <description>DZone Snippets: image code</description>
    <item>
      <title>Simple Resize PIL Image with python</title>
      <link>http://snippets.dzone.com/posts/show/5779</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def resize(im,percent):&lt;br /&gt;    """ retaille suivant un pourcentage 'percent' """&lt;br /&gt;    w,h = im.size&lt;br /&gt;    return im.resize(((percent*w)/100,(percent*h)/100))&lt;br /&gt;&lt;br /&gt;def resize2(im,pixels):&lt;br /&gt;    """ retaille le cot&#233; le plus long en 'pixels' &lt;br /&gt;        (pour tenir dans une frame de pixels x pixels)&lt;br /&gt;    """&lt;br /&gt;    (wx,wy) = im.size&lt;br /&gt;    rx=1.0*wx/pixels&lt;br /&gt;    ry=1.0*wy/pixels&lt;br /&gt;    if rx&gt;ry:&lt;br /&gt;        rr=rx&lt;br /&gt;    else:&lt;br /&gt;        rr=ry&lt;br /&gt;&lt;br /&gt;    return im.resize((int(wx/rr), int(wy/rr)))&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 Jul 2008 09:48:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5779</guid>
      <author>manatlan (manatlan)</author>
    </item>
    <item>
      <title>Generate a graph using Gruff</title>
      <link>http://snippets.dzone.com/posts/show/5168</link>
      <description>This Ruby code produced a graph using gruff.  The output shows a &lt;a href="http://twitxr.com/image/6822/"&gt;line graph&lt;/a&gt; [twitxr.com] for the different fruits. Source code origin: &lt;a href="http://nubyonrails.com/pages/gruff"&gt;Gruff Update With Bar Graphs | Ruby on Rails for Newbies&lt;/a&gt; [rubyonrails.com]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'gruff'&lt;br /&gt;&lt;br /&gt;g = Gruff::Line.new&lt;br /&gt;g.title = "My Graph" &lt;br /&gt;&lt;br /&gt;g.data("Apples", [1, 2, 3, 4, 4, 3])&lt;br /&gt;g.data("Oranges", [4, 8, 7, 9, 8, 9])&lt;br /&gt;g.data("Watermelon", [2, 3, 1, 5, 6, 8])&lt;br /&gt;g.data("Peaches", [9, 9, 10, 8, 7, 9])&lt;br /&gt;&lt;br /&gt;g.labels = {0 =&gt; '2003', 2 =&gt; '2004', 4 =&gt; '2005'}&lt;br /&gt;&lt;br /&gt;g.write('my_fruity_graph.png')&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note: I executed the code within an irb session on my Gentoo box. With Gentoo, &lt;a href="http://gentoo-portage.com/dev-ruby/gruff"&gt;Gruff was installed&lt;/a&gt; [gentoo-portage.com] using the command emerge -va gruff. I tried installing it on Ubuntu but ran into some difficulty, even with help from the article &lt;a href="http://snippets.dzone.com/posts/show/4140"&gt;install rmagick ubuntu&lt;/a&gt; [dzone.com].&lt;br /&gt;&lt;br /&gt;*udpate 21:48 24-Feb*&lt;br /&gt;&lt;br /&gt;The following code does exactly as the same code above, however it uses XML to separate the data from the process, making it easier and more efficient to build graphs.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;# file: xml2gruff.rb&lt;br /&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;require 'gruff'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;class Xml2Gruff&lt;br /&gt;  &lt;br /&gt;  def initialize(filename)&lt;br /&gt;    file = File.new(filename, 'r')&lt;br /&gt;    doc = Document.new(file)&lt;br /&gt;    # get the title&lt;br /&gt;    @title = doc.root.elements['summary/title'].text&lt;br /&gt;    &lt;br /&gt;    @record = Hash.new&lt;br /&gt;    # get each record&lt;br /&gt;    doc.root.elements.each('records/item') {|item|&lt;br /&gt;      avalues = Array.new&lt;br /&gt;      item.elements.each('values/value') { |value| avalues &lt;&lt; value.text.to_i }&lt;br /&gt;      @record[item.elements['label'].text] = avalues&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    # get the summary labels&lt;br /&gt;    @labels = Hash.new  &lt;br /&gt;    doc.root.elements.each('summary/scale/label') {|l| @labels[l.elements['value'].text.to_i] = l.elements['title'].text} &lt;br /&gt;    &lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def save_line_graph(filename)&lt;br /&gt;&lt;br /&gt;    g = Gruff::Line.new&lt;br /&gt;    g.title = @title &lt;br /&gt;    @record.each {|label, data| g.data(label, data) }&lt;br /&gt;    g.labels = @labels&lt;br /&gt;    g.write(filename)&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if __FILE__ == $0&lt;br /&gt; x2g = Xml2Gruff.new('my_fruit.xml')&lt;br /&gt; x2g.save_line_graph('my_fruit2.png')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: my_fruit.xml&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;graph&gt;&lt;br /&gt;  &lt;summary&gt;&lt;br /&gt;    &lt;title&gt;My Graph&lt;/title&gt;&lt;br /&gt;    &lt;scale&gt;&lt;br /&gt;      &lt;label&gt;&lt;title&gt;2003&lt;/title&gt;&lt;value&gt;0&lt;/value&gt;&lt;/label&gt;&lt;br /&gt;      &lt;label&gt;&lt;title&gt;2004&lt;/title&gt;&lt;value&gt;2&lt;/value&gt;&lt;/label&gt;&lt;br /&gt;      &lt;label&gt;&lt;title&gt;2005&lt;/title&gt;&lt;value&gt;4&lt;/value&gt;&lt;/label&gt;&lt;br /&gt;    &lt;/scale&gt;&lt;br /&gt;  &lt;/summary&gt;&lt;br /&gt;  &lt;records&gt;&lt;br /&gt;    &lt;item&gt;&lt;br /&gt;      &lt;label&gt;Apples&lt;/label&gt;&lt;br /&gt;      &lt;values&gt;&lt;value&gt;1&lt;/value&gt;&lt;value&gt;2&lt;/value&gt;&lt;value&gt;3&lt;/value&gt;&lt;value&gt;4&lt;/value&gt;&lt;value&gt;4&lt;/value&gt;&lt;value&gt;3&lt;/value&gt;&lt;/values&gt;&lt;br /&gt;    &lt;/item&gt;&lt;br /&gt;    &lt;item&gt;&lt;br /&gt;      &lt;label&gt;Oranges&lt;/label&gt;&lt;br /&gt;      &lt;values&gt;&lt;value&gt;4&lt;/value&gt;&lt;value&gt;8&lt;/value&gt;&lt;value&gt;7&lt;/value&gt;&lt;value&gt;9&lt;/value&gt;&lt;value&gt;8&lt;/value&gt;&lt;value&gt;9&lt;/value&gt;&lt;/values&gt;&lt;br /&gt;    &lt;/item&gt;&lt;br /&gt;    &lt;item&gt;&lt;br /&gt;      &lt;label&gt;Watermelon&lt;/label&gt;&lt;br /&gt;      &lt;values&gt;&lt;value&gt;2&lt;/value&gt;&lt;value&gt;3&lt;/value&gt;&lt;value&gt;1&lt;/value&gt;&lt;value&gt;5&lt;/value&gt;&lt;value&gt;6&lt;/value&gt;&lt;value&gt;8&lt;/value&gt;&lt;/values&gt;&lt;br /&gt;    &lt;/item&gt;&lt;br /&gt;    &lt;item&gt;&lt;br /&gt;      &lt;label&gt;Peaches&lt;/label&gt;&lt;br /&gt;      &lt;values&gt;&lt;value&gt;9&lt;/value&gt;&lt;value&gt;9&lt;/value&gt;&lt;value&gt;10&lt;/value&gt;&lt;value&gt;8&lt;/value&gt;&lt;value&gt;7&lt;/value&gt;&lt;value&gt;9&lt;/value&gt;&lt;/values&gt;&lt;br /&gt;    &lt;/item&gt;&lt;br /&gt;  &lt;/records&gt;&lt;br /&gt;&lt;/graph&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: &lt;a href="http://gruff.rubyforge.org/"&gt;gruff's gruff-0.2.9 Documentation&lt;/a&gt; [rubyforge.org]&lt;br /&gt;&lt;a href="http://www.google.com/url?sa=t&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Ftopfunky.com%2Fclients%2Fblog%2F2006%2Fcanada_on_rails_gruff.pdf&amp;ei=2urBR-elJYe6-ALryoX2DA&amp;usg=AFQjCNEc0leNwpB7ai1MeHjDO3eD1T_kiQ&amp;sig2=SgqtwiMT6yb1-zOUqOl8pw"&gt;gruff graphs for ruby by geoffrey grosenbach&lt;/a&gt; [topfunky.com]</description>
      <pubDate>Sun, 24 Feb 2008 18:52:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5168</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height</title>
      <link>http://snippets.dzone.com/posts/show/4336</link>
      <description>// This allows us to resize the image. It prevents skewed images and &lt;br /&gt;// also vertically long images caused by trying to maintain the aspect &lt;br /&gt;// ratio on images who's height is larger than their width&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)&lt;br /&gt;{&lt;br /&gt;	System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);&lt;br /&gt;&lt;br /&gt;	// Prevent using images internal thumbnail&lt;br /&gt;	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);&lt;br /&gt;	FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);&lt;br /&gt;&lt;br /&gt;	if (OnlyResizeIfWider)&lt;br /&gt;	{&lt;br /&gt;		if (FullsizeImage.Width &lt;= NewWidth)&lt;br /&gt;		{&lt;br /&gt;			NewWidth = FullsizeImage.Width;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;&lt;br /&gt;	if (NewHeight &gt; MaxHeight)&lt;br /&gt;	{&lt;br /&gt;		// Resize with height instead&lt;br /&gt;		NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;&lt;br /&gt;		NewHeight = MaxHeight;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);&lt;br /&gt;&lt;br /&gt;	// Clear handle to original file so that we can overwrite it if necessary&lt;br /&gt;	FullsizeImage.Dispose();&lt;br /&gt;&lt;br /&gt;	// Save resized picture&lt;br /&gt;	NewImage.Save(NewFile);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jul 2007 22:55:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4336</guid>
      <author>cornerblue (CornerBLUE, Inc.)</author>
    </item>
    <item>
      <title>Set image opacity</title>
      <link>http://snippets.dzone.com/posts/show/3895</link>
      <description>&lt;code&gt;&lt;br /&gt;        public static Image SetOpacity(Image original, float opacity)&lt;br /&gt;        {&lt;br /&gt;            Bitmap temp = new Bitmap(original.Width, original.Height);&lt;br /&gt;            Graphics g = Graphics.FromImage(temp);&lt;br /&gt;            ColorMatrix cm = new ColorMatrix();&lt;br /&gt;            cm.Matrix33 = opacity;&lt;br /&gt;&lt;br /&gt;            ImageAttributes ia = new ImageAttributes();&lt;br /&gt;            ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);&lt;br /&gt;            g.DrawImage(original, new Rectangle(0, 0, temp.Width, temp.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, ia);&lt;br /&gt;            g.Dispose();&lt;br /&gt;&lt;br /&gt;            return temp;&lt;br /&gt;        } &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 24 Apr 2007 22:46:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3895</guid>
      <author>mstampar (Miroslav Stampar)</author>
    </item>
    <item>
      <title>Avatar Resizer</title>
      <link>http://snippets.dzone.com/posts/show/3860</link>
      <description>I go on a lot of Bulletin Board, every has its own limits of size for the avatars, instead of the resize manually I created a script which does it for me with RMagick&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;require "RMagick"&lt;br /&gt;$SIZES = [80 , 100 , 110 , 128]&lt;br /&gt;&lt;br /&gt;if !ARGV[0]&lt;br /&gt;  puts "Usage: mk_avatars.rb SourceAvatarPath"&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;image = Magick::Image.read(ARGV[0]).first&lt;br /&gt;$SIZES.each do |sz|&lt;br /&gt;  puts "Generating Avatar : #{sz}"&lt;br /&gt;  out = image.thumbnail(sz,sz)&lt;br /&gt;  file = "out_#{sz}.#{image.format}"&lt;br /&gt;  out.write(file)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Apr 2007 17:13:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3860</guid>
      <author>kedare (Mathieu Poussin)</author>
    </item>
    <item>
      <title>thumbnail with gd</title>
      <link>http://snippets.dzone.com/posts/show/3843</link>
      <description>Function that makes a thumbnail of a imagen and keeps it's proportion. $the fourth parameter ($fill) makes that, if the image is smaller than the size we want, the function expands it. It shows the image directly to the browser, but can be easily modificed to save the imagen on a directory.&lt;br /&gt;&lt;br /&gt;Funcion que hace una miniatura de una imagen manteniendo su proporcion individual. El cuarto parametro ($fill) hace que, si la imagen es mas peque&#241;a del tama&#241;o deseado, la expande. Muestra la imagen directamente en el navegador, pero puede ser facilmente modificada para guardarla en un directorio.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;function thumb($img, $w, $h, $fill = true) {&lt;br /&gt;	if (!extension_loaded('gd') &amp;&amp; !extension_loaded('gd2')) {&lt;br /&gt;		trigger_error("No dispones de la libreria GD para generar la imagen.", E_USER_WARNING);&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	$imgInfo = getimagesize($img);&lt;br /&gt;	switch ($imgInfo[2]) {&lt;br /&gt;		case 1: $im = imagecreatefromgif($img); break;&lt;br /&gt;		case 2: $im = imagecreatefromjpeg($img);  break;&lt;br /&gt;		case 3: $im = imagecreatefrompng($img); break;&lt;br /&gt;		default:  trigger_error('Tipo de imagen no reconocido.', E_USER_WARNING);  break;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	if ($imgInfo[0] &lt;= $w &amp;&amp; $imgInfo[1] &lt;= $h &amp;&amp; !$fill) {&lt;br /&gt;		$nHeight = $imgInfo[1];&lt;br /&gt;		$nWidth = $imgInfo[0];&lt;br /&gt;	}else{&lt;br /&gt;		if ($w/$imgInfo[0] &lt; $h/$imgInfo[1]) {&lt;br /&gt;			$nWidth = $w;&lt;br /&gt;			$nHeight = $imgInfo[1]*($w/$imgInfo[0]);&lt;br /&gt;		}else{&lt;br /&gt;			$nWidth = $imgInfo[0]*($h/$imgInfo[1]);&lt;br /&gt;			$nHeight = $h;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;  &lt;br /&gt;	$nWidth = round($nWidth);&lt;br /&gt;	$nHeight = round($nHeight);&lt;br /&gt;&lt;br /&gt;	$newImg = imagecreatetruecolor($nWidth, $nHeight);&lt;br /&gt;&lt;br /&gt;	imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);&lt;br /&gt;&lt;br /&gt;	header("Content-type: ". $imgInfo['mime']);&lt;br /&gt;&lt;br /&gt;	switch ($imgInfo[2]) {&lt;br /&gt;		case 1: imagegif($newImg); break;&lt;br /&gt;		case 2: imagejpeg($newImg);  break;&lt;br /&gt;		case 3: imagepng($newImg); break;&lt;br /&gt;		default:  trigger_error('Imposible mostrar la imagen.', E_USER_WARNING);  break;&lt;br /&gt;	}&lt;br /&gt;  &lt;br /&gt;	imagedestroy($newImg);&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Usage/Uso:&lt;br /&gt;thumb("image.png", 200, 200);</description>
      <pubDate>Fri, 20 Apr 2007 00:30:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3843</guid>
      <author>oso96_2000 (oso96_2000)</author>
    </item>
    <item>
      <title>php image from database using PEAR::DB</title>
      <link>http://snippets.dzone.com/posts/show/3730</link>
      <description>This fetches an image from a MySQL database (I know, a database may not be the best place to keep images). The image is sent to the web client as an image/jpeg.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;require_once 'DB.php';&lt;br /&gt;&lt;br /&gt;define('DB_SERVER', 'localhost');&lt;br /&gt;define('DB_USER', 'php');&lt;br /&gt;define('DB_PASS', '');&lt;br /&gt;define('DB_DATABASE', 'images');&lt;br /&gt;define('DSN', 'mysql://'.DB_USER.':'.DB_PASS.'@'.DB_SERVER.'/'.DB_DATABASE);&lt;br /&gt;&lt;br /&gt;global $db;&lt;br /&gt;$db = DB::connect(DSN);&lt;br /&gt;&lt;br /&gt;if (PEAR::isError($db)) {&lt;br /&gt;    die($db-&gt;getMessage());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (isset($_GET['id'])) {&lt;br /&gt;  header('Content-Type: image/jpeg');&lt;br /&gt;  $id = $_GET['id'];&lt;br /&gt;  $sql = "select image_data from images where id = ?";&lt;br /&gt;  $result =&amp; $db-&gt;query($sql, $id);&lt;br /&gt;&lt;br /&gt;  if (PEAR::isError($result)) {&lt;br /&gt;    die($result-&gt;getMessage());&lt;br /&gt;  } else {&lt;br /&gt;    if ($result-&gt;fetchInto($row)) {&lt;br /&gt;      echo $row[0];&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;} else {&lt;br /&gt;  echo file_get_contents('broken.png');&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Mar 2007 08:16:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3730</guid>
      <author>mikewilsonuk (Mike Wilson)</author>
    </item>
    <item>
      <title>Extensions to CImg</title>
      <link>http://snippets.dzone.com/posts/show/3323</link>
      <description>Extensions to CImg, also for interoperability with Fl_Image&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;#include "CImg.h"&lt;br /&gt;#include &lt;Fl/Fl_Image.H&gt;&lt;br /&gt;&lt;br /&gt;using namespace cimg_library;&lt;br /&gt;&lt;br /&gt;/* Generates a w*h*3 CImg&lt;unsigned char&gt; image from the given rgba data and background colour. */&lt;br /&gt;CImg&lt;unsigned char&gt; cimg_from_rgba(unsigned char const* rgba,const unsigned int dimw,const unsigned int dimh, &lt;br /&gt;		const unsigned char back_r, const unsigned char back_g, const unsigned char back_b) {&lt;br /&gt;	CImg&lt;unsigned char&gt; res(dimw,dimh,1,3);&lt;br /&gt;	unsigned char *pR = res.ptr(0,0,0,0), *pG = res.ptr(0,0,0,1), *pB=res.ptr(0,0,0,2);&lt;br /&gt;	const unsigned char *ptrs = rgba;&lt;br /&gt;	for (unsigned int off=res.width*res.height; off&gt;0; --off) {&lt;br /&gt;		*(pR++) = (unsigned char)((ptrs[0] * ptrs[3] + back_r * (255 - ptrs[3])) &gt;&gt; 8);&lt;br /&gt;		*(pG++) = (unsigned char)((ptrs[1] * ptrs[3] + back_g * (255 - ptrs[3])) &gt;&gt; 8);&lt;br /&gt;		*(pB++) = (unsigned char)((ptrs[2] * ptrs[3] + back_b * (255 - ptrs[3])) &gt;&gt; 8);&lt;br /&gt;		ptrs += 4;&lt;br /&gt;	}&lt;br /&gt;	return res;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Generates a w*h*3 CImg&lt;unsigned char&gt; image from the given rgb data. */&lt;br /&gt;CImg&lt;unsigned char&gt; cimg_from_rgb(unsigned char const* rgb,const unsigned int dimw,const unsigned int dimh) {&lt;br /&gt;	CImg&lt;unsigned char&gt; res(dimw,dimh,1,3);&lt;br /&gt;	unsigned char *pR = res.ptr(0,0,0,0), *pG = res.ptr(0,0,0,1), *pB=res.ptr(0,0,0,2);&lt;br /&gt;	const unsigned char *ptrs = rgb;&lt;br /&gt;	for (unsigned int off=res.width*res.height; off&gt;0; --off) {&lt;br /&gt;		*(pR++) = (unsigned char)*(ptrs++);&lt;br /&gt;		*(pG++) = (unsigned char)*(ptrs++);&lt;br /&gt;		*(pB++) = (unsigned char)*(ptrs++);&lt;br /&gt;	}&lt;br /&gt;	return res;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Generates a FLTK RGB Image from the given CImg&lt;unsigned char&gt; image. */&lt;br /&gt;Fl_RGB_Image* image_from_cimg(CImg&lt;unsigned char&gt; const &amp; cimg) {&lt;br /&gt;	const unsigned int wh = cimg.dimx() * cimg.dimy();&lt;br /&gt;	unsigned char *buffer = new unsigned char[3*wh], *nbuffer=buffer;&lt;br /&gt;	const unsigned char &lt;br /&gt;		*ptr1 = cimg.ptr(0,0,0,0),&lt;br /&gt;		*ptr2 = cimg.dim&gt;1?cimg.ptr(0,0,0,1):ptr1,&lt;br /&gt;		*ptr3 = cimg.dim&gt;2?cimg.ptr(0,0,0,2):ptr1;&lt;br /&gt;	for (unsigned int k=0; k&lt;wh; k++) {&lt;br /&gt;		*(nbuffer++) = (unsigned char)(*(ptr1++));&lt;br /&gt;		*(nbuffer++) = (unsigned char)(*(ptr2++));&lt;br /&gt;		*(nbuffer++) = (unsigned char)(*(ptr3++));&lt;br /&gt;	}&lt;br /&gt;	return new Fl_RGB_Image(buffer, cimg.dimx(), cimg.dimy());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Returns a resized image to a maximum of the given dimensions, keeping its aspect ratio. */&lt;br /&gt;template&lt;typename T&gt; CImg&lt;T&gt; get_resize_keep_aspect_ratio(CImg&lt;T&gt; const &amp; cimg, const unsigned int w, const unsigned int h) {&lt;br /&gt;	if (cimg.dimx() * h &gt; cimg.dimy() * w) { // cimg.dimx() / cimg.dimy() &gt; w / h&lt;br /&gt;		return cimg.get_resize(w, cimg.dimy() * w / cimg.dimx(), -100, -100, 5);&lt;br /&gt;	} else {&lt;br /&gt;		return cimg.get_resize(cimg.dimx() * h / cimg.dimy(), h, -100, -100, 5);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Returns a resized image to a maximum of the given dimensions, keeping its aspect ratio,&lt;br /&gt; * only if the dimensions given are smaller than the original dimensions. */&lt;br /&gt;template&lt;typename T&gt; CImg&lt;T&gt; get_resize_if_smaller_keep_aspect_ratio(CImg&lt;T&gt; const &amp; cimg, const unsigned int w, const unsigned int h) {&lt;br /&gt;	if (w &lt; cimg.dimx() || h &lt; cimg.dimy())&lt;br /&gt;		return get_resize_keep_aspect_ratio(cimg, w, h);&lt;br /&gt;	else&lt;br /&gt;		return CImg&lt;T&gt;(cimg);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 18 Jan 2007 10:04:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3323</guid>
      <author>yondalf (Jonathan Tan)</author>
    </item>
    <item>
      <title>J2ME - Rescale Image</title>
      <link>http://snippets.dzone.com/posts/show/3257</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private Image rescaleImage(Image image, int width, int height)&lt;br /&gt;	{&lt;br /&gt;		int sourceWidth = image.getWidth();&lt;br /&gt;		int sourceHeight = image.getHeight();&lt;br /&gt;	&lt;br /&gt;		Image newImage = Image.createImage(width, height);&lt;br /&gt;		Graphics g = newImage.getGraphics();&lt;br /&gt;		&lt;br /&gt;		for(int y=0; y&lt;height; y++)&lt;br /&gt;		{&lt;br /&gt;			for(int x=0; x&lt;width; x++)&lt;br /&gt;			{&lt;br /&gt;				g.setClip(x, y, 1, 1);&lt;br /&gt;				int dx = x * sourceWidth / width;&lt;br /&gt;				int dy = y * sourceHeight / height;&lt;br /&gt;				g.drawImage(image, x-dx, y-dy, Graphics.LEFT | Graphics.TOP);&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		return Image.createImage(newImage);&lt;br /&gt;	}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 09 Jan 2007 02:08:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3257</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>www.webscriptexpert.com - Java - Image based tool tips</title>
      <link>http://snippets.dzone.com/posts/show/3124</link>
      <description>// This script creates 'tool tips' for images. Use as much text as you want. Formatting is controlled through the use of style sheets. The text will resize, according to the screen width and placement of the cursor.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- TWO STEPS TO INSTALL IMAGE TOOL TIPS:&lt;br /&gt;&lt;br /&gt;1. Copy the coding into the HEAD of your HTML document&lt;br /&gt;2. Add the last code into the BODY of your HTML document --&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- STEP ONE: Paste this code into the HEAD of your HTML document --&gt;&lt;br /&gt;&lt;br /&gt;&lt;HEAD&gt;&lt;br /&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;#toolTipBox {&lt;br /&gt;display: none;&lt;br /&gt;padding: 5;&lt;br /&gt;font-size: 12px;&lt;br /&gt;border: black solid 1px;&lt;br /&gt;font-family: verdana;&lt;br /&gt;position: absolute;&lt;br /&gt;background-color: #ffd038;&lt;br /&gt;color: 000000;&lt;br /&gt;}&lt;br /&gt;--&gt;&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;Created by: Saul Salvatierra :: http://myarea.com.sapo.pt&lt;br /&gt;with help from Ultimater :: http://ultimiacian.tripod.com */&lt;br /&gt;&lt;br /&gt;var theObj="";&lt;br /&gt;&lt;br /&gt;function toolTip(text,me) {&lt;br /&gt;theObj=me;&lt;br /&gt;theObj.onmousemove=updatePos;&lt;br /&gt;document.getElementById('toolTipBox').innerHTML=text;&lt;br /&gt;document.getElementById('toolTipBox').style.display="block";&lt;br /&gt;window.onscroll=updatePos;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function updatePos() {&lt;br /&gt;var ev=arguments[0]?arguments[0]:event;&lt;br /&gt;var x=ev.clientX;&lt;br /&gt;var y=ev.clientY;&lt;br /&gt;diffX=24;&lt;br /&gt;diffY=0;&lt;br /&gt;document.getElementById('toolTipBox').style.top = y-2+diffY+document.body.scrollTop+ "px";&lt;br /&gt;document.getElementById('toolTipBox').style.left = x-2+diffX+document.body.scrollLeft+"px";&lt;br /&gt;theObj.onmouseout=hideMe;&lt;br /&gt;}&lt;br /&gt;function hideMe() {&lt;br /&gt;document.getElementById('toolTipBox').style.display="none";&lt;br /&gt;}&lt;br /&gt;--&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/HEAD&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document --&gt;&lt;br /&gt;&lt;br /&gt;&lt;BODY&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="center"&gt;&lt;br /&gt;&lt;span id="toolTipBox" width="200"&gt;&lt;/span&gt;&lt;br /&gt;&lt;img src="yourImage.jpg" width="237" height="197" border="0" onmouseover="toolTip('Place your tool tip here',this)"&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;center&gt;&lt;br /&gt;&lt;font face="arial, helvetica" size"-2"&gt;Free JavaScripts provided&lt;br&gt;&lt;br /&gt;by &lt;a href="http://www.webscriptexpert.com"&gt;Web Script Expert &lt;/a&gt;&lt;/font&gt;&lt;br /&gt;&lt;/center&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 11 Dec 2006 03:55:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3124</guid>
      <author>webscriptexpert (r)</author>
    </item>
  </channel>
</rss>
