Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at BFusion / BFLEX 2010 (Bloomington, Indiana) with: Ed Bartram

ColdFusion Image Coordinates Are Zero-Based

By Ben Nadel on
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.

Tweet This Groovy post by @BenNadel - ColdFusion Image Coordinates Are Zero-Based Thanks my man — you rock the party that rocks the body!



Reader 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.

Reply to this Comment

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.

Reply to this Comment

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 :)

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
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.