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 »
11,247 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 »
11,247 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 »
11,247 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
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools