ColdFusion Image Coordinates Are Zero-Based

Posted February 25, 2008 at 8:39 AM by Ben Nadel

Tags: ColdFusion

This might not be news to anyone else, but I never actually thought about the coordinates used in ColdFusion image manipulation functions. I am so used to everything in ColdFusion being one-based that I just assumed that ColdFusion image manipulation would work the same way. Well, as I was working on updating the ImageUtils.cfc, I realized that I was way off base (thanks to OutOfBounds errors getting thrown left and right). As it turns out, ColdFusion image objects are zero-based when it comes to pixel coordinates. To test this, I ran some of ColdFusion 8's X/Y based methods:

  • <!--- Create 10x10 image. --->
  • <cfset objImage = ImageNew(
  • "",
  • 10,
  • 10,
  • "argb",
  • "##262626"
  • ) />
  •  
  • <!--- Copy based on zero-based coordinates. --->
  • <cfset objImage2 = ImageCopy(
  • objImage,
  • 0,
  • 0,
  • 10,
  • 10
  • ) />
  •  
  • <!--- Crop image based on zero-based coordinates. --->
  • <cfset ImageCrop(
  • objImage2,
  • 0,
  • 0,
  • 5,
  • 5
  • ) />
  •  
  • <!--- Draw rectangle using zero-based coordinates. --->
  • <cfset ImageDrawRect(
  • objImage2,
  • 0,
  • 0,
  • 5,
  • 1,
  • "yes"
  • ) />
  •  
  • <!--- Draw line using zero-based coordinates. --->
  • <cfset ImageDrawLine(
  • objImage2,
  • 0,
  • 0,
  • 50,
  • 50
  • ) />

As you can see, things like ImageCopy(), ImageCrop(), ImageDrawRect(), and ImageDrawLine() all work with zero-base coordinates. Ok, in all fairness, I believe that ImageDrawRect() and ImageDrawLine() will actually work with off-canvas values; however, ImageCrop() will throw an OutOfBounds error if you try to go off canvas.

So, there you have it - ColdFusion image manipulation is zero-based, not one-based. This isn't hugely important information, but there are some things that I will have to go back and fix in the ImageUtils.cfc where some one-to-zero base calculations take place.




Reader Comments

Feb 25, 2008 at 10:45 AM // reply »
92 Comments

This is as expected. Every photo editing software out there uses 0,0 as the top left corner. Good find nonetheless.


Feb 25, 2008 at 10:50 AM // reply »
10,640 Comments

@Andy,

That's actually how I came to realize this. I was using the underlying Java AWT library that helps to power the ColdFusion image object and I saw that their pixel coordinates all start at 0,0. Then, I was doing a translation from 1,1 (CF) to 0,0 (Java) and was getting the out of bounds errors.

I would say, however, to be careful about what is "Expected". Could you not say the same thing about arrays? Every other language in the universe uses zero based arrays, and yet, ColdFusion does not. So, the expectation set by the programming world at large does not necessarily translate to what is done in CF. Not that I am bad mouthing CF in any way, I love it. Just saying, I don't think either way would have been obvious.


Feb 26, 2008 at 4:37 AM // reply »
18 Comments

'every other language'?

http://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28array%29#Array_system_cross-reference_list

I wonder if JJ Allaire had a background in something like Fortran or Smalltalk which influenced his decision (assuming it was his) to use 1-based arrays.


Feb 26, 2008 at 4:39 AM // reply »
18 Comments

that URL doesn't seem to work in your blog, here it is in short format:
http://tinyurl.com/2hz4s2


Feb 26, 2008 at 7:26 AM // reply »
10,640 Comments

Ok, maybe not every other language :) But a good deal of them. My only point is that since so much stuff starts at "1" in ColdFusion, I didn't think right away that the image functionality would start at zero.


Jul 15, 2010 at 3:54 PM // reply »
20 Comments

Your blog is always awesome. I am working on tiling images for zoomify and my math wasn't working out. Why? I started with 1,1 for my grid. Thanks for the help :)


Jul 18, 2010 at 11:46 AM // reply »
10,640 Comments

@Jeff,

Always happy to help!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »