Listing 1

<!--- histogram struct name = "hist" --->
<cfquery datasource="#DSN#">
INSERT INTO colors (
photoid,
redmean,
redsd,
greenmean,
greensd,
bluemean,
bluesd,
bin
)
VALUES (
<cfqueryparam value="#photoid#" cfsqltype="cf_sql_char" />,
<cfqueryparam value="#hist.r.mean#" cfsqltype="cf_sql_double" />,
<cfqueryparam value="#hist.r.standarddeviation#" cfsqltype="cf_sql_double" />,
<cfqueryparam value="#hist.g.mean#" cfsqltype="cf_sql_double" />,
<cfqueryparam value="#hist.g.standarddeviation#" cfsqltype="cf_sql_double" />,
<cfqueryparam value="#hist.b.mean#" cfsqltype="cf_sql_double" />,
<cfqueryparam value="#hist.b.standarddeviation#" cfsqltype="cf_sql_double" />,
<cfqueryparam value="#bin#" cfsqltype="cf_sql_numeric" />
)
</cfquery>

Listing 2

<!--- cfquery of cfdirectory here --->
<cfoutput query="photos">
 <cfscript>
  //size of sub images (WARNING this number will be squared so 4 = 16 subimages)
  size = 4;
  //max size in pixels of subimage (saves processing time)
  maxSize = 50;
  //Jpeg Codec
  jpegCodec = createObject("java", "com.sun.image.codec.jpeg.JPEGCodec");
  //file open
  fileInputStream = createObject("java", "java.io.FileInputStream").init("#directory#\#photos.photofilename#");
  //decodes the JPEG
  decoder = jpegCodec.createJPEGDecoder(fileInputStream);
  //give us an image to scale
  image = decoder.decodeAsBufferedImage();
  imageHeight = image.getHeight();
  imageWidth = image.getWidth();
 </cfscript>
 <cfset bin = 0 />
 <cfloop from="1" to="#size#" index="i">
  <cfloop from="1" to="#size#" index="ii">
   <cfset bin = bin + 1 />
   <cfscript>
    //get subimage
    x = imageWidth/size*(ii-1);
    y = imageHeight/size*(i-1);
    subImage = image.getSubimage(JavaCast("int",x),JavaCast("int",y),
JavaCast("int",imageWidth/size),JavaCast("int",imageHeight/size));
    
    subImageHeight = subImage.getHeight();
    subImageWidth = subImage.getWidth();
    sizeRatio = subImageWidth/subImageHeight;
    
    //figure out if the photo is portrait or landscape make the larger equal to the max thumb size given in agruments
    if (subImageWidth gte subImageHeight) {
     subImageWidth = maxSize;
     subImageHeight = round(maxSize/sizeRatio);
    } else {
     subImageWidth = round(maxSize * sizeRatio);
     subImageHeight = maxSize;
    };
    
    //create a scaled image
    scaledImage = subImage.getScaledInstance(JavaCast("int", subImageWidth), JavaCast("int", subImageHeight), subImage.SCALE_SMOOTH);
    BufferedImage = createObject("java", "java.awt.image.BufferedImage").init(JavaCast("int", subImageWidth), JavaCast("int", subImageHeight), subImage.TYPE_INT_RGB);
    //draw the image into the buffered image
    graphics = BufferedImage.createGraphics();
    graphics.drawImage(scaledImage, 0, 0, Javacast("null", ""));
     
    //get histogram
    imageHistogram.setBufferedImage(BufferedImage);
    hist = imageHistogram.getColorHistogram();
   </cfscript>
 
   <cfquery datasource="#DSN#">
   INSERT INTO colors (
   photoid,
   redmean,
   redsd,
   greenmean,
   greensd,
   bluemean,
   bluesd,
   bin
   )
   VALUES (
   <cfqueryparam value="#photos.photoid#" cfsqltype="cf_sql_char" />,
   <cfqueryparam value="#hist.r.mean#" cfsqltype="cf_sql_double" />,
   <cfqueryparam value="#hist.r.standarddeviation#" cfsqltype="cf_sql_double" />,
   <cfqueryparam value="#hist.g.mean#" cfsqltype="cf_sql_double" />,
   <cfqueryparam value="#hist.g.standarddeviation#" cfsqltype="cf_sql_double" />,
   <cfqueryparam value="#hist.b.mean#" cfsqltype="cf_sql_double" />,
   <cfqueryparam value="#hist.b.standarddeviation#" cfsqltype="cf_sql_double" />,
   <cfqueryparam value="#bin#" cfsqltype="cf_sql_numeric" />
   )
   </cfquery>
  </cfloop>
 </cfloop>
</cfoutput>

Listing 3

<!--- set range of colors to fall in based on mean --->
<cfset sd = 10 />
<!--- query for colors expecting red,green,blue 0-255 from form--->
<cfquery datasource="#datasource#" name="search">
SELECT Count(colors.photoid) AS CountOfphotoid,cfcphotoblogphotos.photoid ,
 cfcphotoblogphotos.photoTitle,cfcphotoblogphotos.photoThumbFileName, avg(colors.redmean) as redmean,
  avg(colors.bluemean) as bluemean, avg(colors.greenmean) as greenmean
FROM cfcphotoblogphotos RIGHT JOIN colors ON cfcphotoblogphotos.photoID = colors.photoid
WHERE 1=1
AND colors.redmean between <cfqueryparam value="#form.red-sd#" cfsqltype="cf_sql_numeric" />
and <cfqueryparam value="#form.red+sd#" cfsqltype="cf_sql_numeric" />
AND colors.greenmean between <cfqueryparam value="#form.green-sd#" cfsqltype="cf_sql_numeric" />
and <cfqueryparam value="#form.green+sd#" cfsqltype="cf_sql_numeric" />
AND colors.bluemean between <cfqueryparam value="#form.blue-sd#" cfsqltype="cf_sql_numeric" />
and <cfqueryparam value="#form.blue+sd#" cfsqltype="cf_sql_numeric" />
GROUP BY colors.photoid, cfcphotoblogphotos.photoTitle
ORDER BY Count(colors.photoid) DESC;
</cfquery>