<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>richardjdare.com &#187; programming</title>
	<atom:link href="http://richardjdare.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://richardjdare.com</link>
	<description>Games, Ideas, Imagination</description>
	<lastBuildDate>Thu, 06 Apr 2023 12:02:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>bb2-mode &#8211; Edit Blitz Basic II code in Emacs</title>
		<link>http://richardjdare.com/blog/2017/05/bb2-mode-edit-blitz-basic-ii-code-in-emacs/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=bb2-mode-edit-blitz-basic-ii-code-in-emacs</link>
		<comments>http://richardjdare.com/blog/2017/05/bb2-mode-edit-blitz-basic-ii-code-in-emacs/#comments</comments>
		<pubDate>Mon, 22 May 2017 19:45:07 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://richardjdare.com/?p=1683</guid>
		<description><![CDATA[For the past couple of months I&#8217;ve been messing about with some retro-programming, working on a little game in Blitz Basic II for the Commodore.]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-1684" src="http://richardjdare.com/blog/wp-content/uploads/2017/05/bb2-mode.jpg" alt="bb2-mode screenshot" width="689" height="727" /></p>
<p>For the past couple of months I&#8217;ve been messing about with some retro-programming, working on a little game in Blitz Basic II for the Commodore Amiga (more info coming soon). I just wanted a nice no-stress project to spend a few weeks on before making yet another attempt to change my life through code.</p>
<p>I grew up on Blitz, and it was fun to return to it again after 20 years. On a whim, I decided to write an Emacs major mode for Blitz II so I could edit and read code outside of an Amiga Emulator. It was also a good opportunity to practice my Lisp programming (Emacs plugins are written in Emacs Lisp).</p>
<p>bb2-mode not only provides syntax highlighting and online help for over 2400 Blitz II commands, it can load and save tokenized files. Blitz II saved its source code in a custom file format in order to save disk space. As far as I know, bb2-mode is the only thing outside an Amiga which can load and save these files. So even if the Emacs-using-retro-Blitz-programmer demographic <em>isn&#8217;t</em> that large, bb2-mode still has some value.</p>
<p>Anyhow, bb2-mode is on <a href="https://github.com/richardjdare/bb2-mode" target="_blank">github</a> and is under active development. I&#8217;ll have more to say about Blitz II in the future, so stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2017/05/bb2-mode-edit-blitz-basic-ii-code-in-emacs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making a heat map effect for Toxin</title>
		<link>http://richardjdare.com/blog/2013/06/making-a-heat-map-effect-for-toxin/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-a-heat-map-effect-for-toxin</link>
		<comments>http://richardjdare.com/blog/2013/06/making-a-heat-map-effect-for-toxin/#comments</comments>
		<pubDate>Thu, 20 Jun 2013 14:18:07 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=1010</guid>
		<description><![CDATA[As you might already know my forthcoming iOS game, Toxin involves shooting rapidly dividing toxic cells which eventually fill the playfield or burst to spawn.]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-1011" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-shot.jpg" alt="Toxin Heatmap" width="500" height="259" /></p>
<p>As you might already know my forthcoming iOS game, <a href="http://toxingame.com" target="_blank">Toxin</a> involves shooting rapidly dividing toxic cells which eventually fill the playfield or burst to spawn flying enemies.</p>
<p>Now, not long ago I read an <a href="http://twitter-heatmap.vertalab.com/" target="_blank">article</a> about generating <a href="http://en.wikipedia.org/wiki/Heat_map" target="_blank">heat maps </a>to display web statistics, and I thought it would be cool to use this technique to colourise the ever changing clumps of cells in Toxin. After finding out how they work, I knocked up a prototype in <a href="http://processing.org" target="_blank">Processing</a> before adding the effect to the game.</p>
<p>Generating heatmaps is pretty simple. Take a 2d grid of values, perform a Gaussian blur on it, then use each resulting value as an index into a gradient of colours.</p>
<p>For Toxin, the first step was to create a simple array of numbers representing the current state of the cell grid. Toxin cells can be different sizes. To simplify things, I store each big cell as several small cells.</p>
<p>Each cell is given an initial value of 255. When we draw our cells, we use a range from black to white, where values of 0 are black, 255 is white and values in between are shades of gray.</p>
<p><img class="aligncenter size-full wp-image-1020" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-initial.jpg" alt="heatmap-initial" width="500" height="361" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">The cells are represented as a grid of numbers. Each cell is given the initial value of 255</span></em></p>
<p>The next step is to apply a simple <a href="http://en.wikipedia.org/wiki/Box_blur" target="_blank">box blur</a> to this grid of numbers. Normally heat maps use <a href="http://en.wikipedia.org/wiki/Gaussian_blur" target="_blank">Gaussian blur</a>, but since Toxin&#8217;s grid is such a low resolution we can get away with using a simpler algorithm which is much faster to calculate. To learn more about blurring techniques, I recommend <a href="http://www.gamasutra.com/view/feature/3102/four_tricks_for_fast_blurring_in_.php?print=1" target="_blank">Alex Evan&#8217;s article</a> on Gamasutra.</p>
<p><img class="aligncenter size-full wp-image-1027" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-blur.jpg" alt="Toxin heat map blur" width="500" height="361" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">The cell grid after being blurred. Notice how the values have changed</span></em></p>
<p>Now look what happens when we draw our original cell grid using the values from the blur operation. Cells in the middle of large clumps have higher values than solitary cells.</p>
<p><img class="aligncenter size-full wp-image-1033" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-grayscale.jpg" alt="Toxin heat map grayscale" width="500" height="361" /><br />
<em><span style="color: #808080; padding-bottom: 20px;">Drawing the original grid with the blurred values</span></em></p>
<p>The final step is to colourise these grayscale values so they look cool. We do this by mapping these values onto a gradient I made in Photoshop. Values of 0 map onto the left of this gradient, 255 to the right, and the others to the colours in between.</p>
<div style="text-align: center;"><img class="aligncenter wp-image-1037 size-full" style="border: 0;" src="http://www.richardjdare.com/blog/wp-content/uploads/2013/06/heatmap-gradient.jpg" alt="Toxin heat map gradient" width="320" height="80" /></div>
<div style="width: 500px; text-align: center;"></div>
<p>When we draw the grid with the colours from the gradient we get our heatmap!</p>
<p><img class="aligncenter size-full wp-image-1044" style="border: 0;" src="http://richardjdare.com/blog/wp-content/uploads/2013/06/final-heatmap.jpg" alt="Toxin final heat map" width="500" height="361" /></p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2013/06/making-a-heat-map-effect-for-toxin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Next Photoshop</title>
		<link>http://richardjdare.com/blog/2013/02/the-next-photoshop/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-next-photoshop</link>
		<comments>http://richardjdare.com/blog/2013/02/the-next-photoshop/#comments</comments>
		<pubDate>Sun, 24 Feb 2013 15:06:12 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[photoshop]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[photoshop scripting]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=905</guid>
		<description><![CDATA[If you’ve read this blog for very long you’ll know that I am a big fan of Photoshop scripting and procedural graphics. Toxin, the iPhone.]]></description>
				<content:encoded><![CDATA[<p>If you’ve read this blog for very long you’ll know that I am a big fan of Photoshop scripting and procedural graphics. <a href="http://toxingame.com" target="_blank">Toxin</a>, the iPhone game I’ve been <a href="http://richardjdare.com/blog/category/toxin/" target="_blank">working on</a>, uses procedural art and animated image processing effects extensively. I don’t think the game would be possible without Photoshop scripting.</p>
<p>However, Photoshop’s scripting system is far from perfect. You cannot access pixel data from within scripts, nor can you draw lines or circles or anything like that. These limitations have become really frustrating to me.</p>
<p>For example, on the day I started work on Toxin, I needed to draw an ellipse with precise dimensions. The whole game takes place inside an ellipse, you see. I couldn’t do it in Photoshop by clicking and dragging, it was too inaccurate, so I wrote a C program using the <a href="http://alleg.sourceforge.net/" target="_blank">Allegro</a> game library to create the precise shapes I needed. These were then loaded into Photoshop and used as guides while I created the game artwork.</p>
<p>Later I discovered <a href="http://processing.org/" target="_blank">Processing</a>, which is much quicker to develop in than C. I soon developed a workflow where I would go back and forth between Photoshop and Processing, generating procedural content then processing it or running it through scripts until I got the desired result.</p>
<p>Processing, however has some frustrating quirks of its own. As a simplified front end to Java, you often get Java errors that are incomprehensible without digging into the underlying platform. Also, it cannot effectively save semi-transparent PNGs which means that the anti-aliased sprites I was creating all had a solid background which had to be removed. I ended up redesigning my workflow and using the excellent Ghost and AntiGhost plugins from <a href="http://www.flamingpear.com/download.html" target="_blank">Flaming Pear</a> to overcome these problems.</p>
<h3>From graphics app to graphics platform</h3>
<p>All these issues got me thinking about what I really want from a high end graphics program. What I want is something like a programmable, graphical <a href="http://en.wikipedia.org/wiki/Lisp_machine" target="_blank">Lisp Machine</a>; a powerful graphics programming platform with the UI of an art application, not an art application with a bolted-on scripting system.</p>
<p>I want the ability to programmatically control every aspect of Photoshop from both external scripts and from a real-time <a href="http://en.wikipedia.org/wiki/Read–eval–print_loop" target="_blank">REPL</a> inside Photoshop that works like the command line used in some CAD programs.</p>
<p>I want to be able to type<em> line(10,10,100,100)</em> and have a line appear on my presently selected image. I want to be able to open the REPL and assign my current selection to a variable which I can later re-select with a simple command. I want to be able to access the pixel data in my images, and write my own filters in Javascript within the scripting system itself. I think anyone in high-end graphics or scientific imaging would love something like this.</p>
<p>A true graphics platform like I have described would likely have to be written from the ground up with programmability built in. Nevertheless I did spend some time finding out how far I could go with Photoshop&#8217;s native capabilities.</p>
<p>I had the idea of writing a Photoshop plugin that contained a number of drawing functions which could be called from scripts via a Javascript library that integrates with the Photoshop document model. So you could write <em>PSDraw.line(10,10,100,100);</em> in ExtendScript Toolkit, and my Javascript library would construct a call to the plugin which would draw the line.</p>
<p>Unfortunately, it&#8217;s not possible to get information from a plugin back to the Photoshop script thats running it, so it wouldn&#8217;t be possible to do a <em>getPixel()</em> or anything like that. At best, this plugin/script combo would give us a somewhat clunky way of drawing programmatically but little else that would justify the effort involved. (have you ever seen the <a href="http://www.mactech.com/articles/mactech/Vol.15/15.04/PhotoshopPlug-InsPart1/index.html" target="_blank">Photoshop plugin API?</a>)</p>
<p>As a secondary option I thought about actually embedding <a href="https://code.google.com/p/v8/" target="_blank">V8</a> inside a plugin which would contain an editor and debugger. Besides the ugliness of having 2 distinct Javascript engines running in the same app, the plugin architecture put paid to this idea. Photoshop plugins are one-shot operations. Load the plugin, set the parameters, perform the operation, then shutdown. My idea would only be effective if the plugin was a persistent part of the Photoshop environment.</p>
<p>So the only options then, would be for Adobe to integrate scripting more completely, or for someone else to start making what would be a true sucessor to Photoshop.</p>
<p>&nbsp;</p>
<p><figure class='media-wrapper player'><iframe width="500" height="375" src="http://www.youtube.com/embed/mOZqRJzE8xg?wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe></figure></p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2013/02/the-next-photoshop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Toxin &#8211; Part One</title>
		<link>http://richardjdare.com/blog/2012/10/programming-toxin-part-one/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=programming-toxin-part-one</link>
		<comments>http://richardjdare.com/blog/2012/10/programming-toxin-part-one/#comments</comments>
		<pubDate>Fri, 19 Oct 2012 12:56:43 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[Toxin]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=243</guid>
		<description><![CDATA[Over the next few posts I want to write a bit about how my first iPhone game, Toxin, is being developed. Although I&#8217;ve been programming.]]></description>
				<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-244" style="border: 0px;" title="Toxin" src="http://richardjdare.com/blog/wp-content/uploads/2012/10/toxin-screen1.jpg" alt="" width="500" height="249" /></p>
<p>Over the next few posts I want to write a bit about how my first iPhone game, Toxin, is being developed. Although I&#8217;ve been programming for a long time, this is my first ever project in the Apple ecosystem; I hadn&#8217;t even used a Mac before starting this game.</p>
<p>Toxin is written in C++ inside a thin layer of Objective C. I did this for a number of reasons. Firstly I am much more familiar with C++. Secondly, as the most common game development language I knew I would have better luck finding additional libraries and answers to any development problems if I worked in it.</p>
<p>It also meant I could reuse some of my own code. Toxin actually contains code used in my first game, <a href="http://richardjdare.com/blog/games/">Starblaster</a> (2003). Some of it goes back to DirectX stuff I did in the late 90&#8217;s. I always liked having a sense of continuity in my work, a feeling that all my games are connected.</p>
<p>The Objective C layer handles iOS specific stuff like getting the app started up, passing touch information to the game logic, and setting up threads for Toxin&#8217;s concurrent level loading. The sound library I use, <a href="http://www.cocos2d-iphone.org/wiki/doku.php/cocosdenshion:faq" target="_blank">CocosDenshion</a> is also in Objective C. I wouldn&#8217;t rule out doing a whole project in Objective C. I&#8217;ve enjoyed learning it and I&#8217;ve used it to create several utilities already, including the Toxin Level Editor, which I will probably write about soon. I would, however, want to spend some time comparing the performance of the Objective C Container libraries with the STL beforehand.</p>
<h3>Engine? What Engine?</h3>
<p>Now, some of my friends believe that the main reason Toxin has taken me so long is because I didn&#8217;t use one of the popular 2d engines out there like <a href="http://www.cocos2d-iphone.org/" target="_blank">Cocos2d</a> and instead wrote all my own graphics code. They don&#8217;t believe me when I tell them that large engines are unnecessary for small games (though I would never stop anyone from using them) and that I could rewrite my entire graphics system in about 2 weeks.</p>
<div id="#whatsinbox" style="margin: 10px; padding: 8px; background-color: #ebebeb; height: 155px;">
<h4>What&#8217;s in Toxin</h4>
<ul>
<li style="float: left; width: 220px;">Batched sprite drawing</li>
<li style="float: left; width: 220px;">Resource management</li>
<li style="float: left; width: 220px;">Messaging system</li>
<li style="float: left; width: 220px;">2d particle system</li>
<li style="float: left; width: 220px;">JSON file loading</li>
<li style="float: left; width: 220px;">Zip file loading</li>
<li style="float: left; width: 220px;">Chipmunk physics</li>
<li style="float: left; width: 220px;">Oriented bounding box collision</li>
<li style="float: left; width: 220px;">Keyframe interpolation system</li>
<li style="float: left; width: 220px;">Concurrent level loading</li>
</ul>
</div>
<p>&nbsp;</p>
<p>My &#8220;engine&#8221; if you could call it that, is a basic set of classes for things like sprite drawing, line drawing, loading textures, and drawing basic primitive shapes. All sprite and line drawing is batched using vertex buffers for maximum speed. The classes are loosely related; I just add something when I need it, not worrying about architecture or anything. The whole thing is about 7000 lines of code, and that includes some significant utility classes I wrote, such as my keyframe interpolation system. I use this all over the place to animate transitions, colour blends and movements of all kinds. I wrote interpolators for many different data types and I&#8217;m sure I could reduce the lines of code by making them into template classes.</p>
<p>I used this graphics system to build Toxin&#8217;s particle engine which I based on John van der Burg&#8217;s Gamasutra article, <em><a href="http://www.gamasutra.com/view/feature/3157/building_an_advanced_particle_.php" target="_blank">Building an advanced Particle System.</a></em> The particles use some simple physics to bounce them off the inside of the elliptical playfield.</p>
<p>At one point, the game had elliptical gravity: When a particle hit the ellipse, gravity would activate for that particle and it would &#8220;fall&#8221; into the ellipse, over 360 degrees. It was a cool effect but it didn&#8217;t look like anything you would expect to happen, so I replaced it with simple bouncing which is much more satisfying to watch.</p>
<p>Some of the enemies in Toxin use the <a href="http://chipmunk-physics.net/" target="_blank">Chipmunk physics engine</a>. One of the things I&#8217;m proud of in this game is how you can easily mix and match different types of objects. Some use Chipmunk, others use simple bounding box and oriented-bounding box collision.</p>
<p>Game structure is accounted for by a basic game state manager with simple transitions and there&#8217;s a resource managment system that lets me load and unload graphics whenever I need to.</p>
<p>After reading <a href="http://www.mcshaffry.com/mrmike/">Mike McShaffry&#8217;s</a> <a href="http://www.amazon.co.uk/Game-Coding-Complete-Mike-McShaffry/dp/1133776574/ref=dp_ob_title_bk">Game Coding Complete</a> I wrote a messaging system which allows different parts of my game to easily communicate with each other with a minimal amount of dependency. I use this messaging system for spawning enemies, creating explosions, and playing sound effects.</p>
<p>Next time I&#8217;ll write something about how Toxin levels work, and I&#8217;ll describe my first native Mac program, the Toxin Level Editor. Please let me know if you have any questions or if you want more information about any of the things in this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2012/10/programming-toxin-part-one/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toxin &#8211; Coming Soon</title>
		<link>http://richardjdare.com/blog/2011/06/toxin-coming-soon/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=toxin-coming-soon</link>
		<comments>http://richardjdare.com/blog/2011/06/toxin-coming-soon/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 00:03:24 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=34</guid>
		<description><![CDATA[This is the titlescreen of Toxin, my first iPhone game. It was only supposed to be a warm-up; a short project to help me get.]]></description>
				<content:encoded><![CDATA[<p><img class="size-full wp-image-36 aligncenter" style="border: none;" title="toxin-titles1" src="http://richardjdare.com/blog/wp-content/uploads/2011/06/toxin-titles1.jpg" alt="Toxin Title Screen" width="500" height="290" /></p>
<p>This is the titlescreen of Toxin, my first iPhone game. It was only supposed to be a warm-up; a short project to help me get back into the game after a few years developing web applications, but it has taken much longer than I anticipated.</p>
<p>I&#8217;m doing the whole thing myself, code, graphics and sound, largely because I don&#8217;t know anyone who could help me out, but also because I was a child in the 80&#8217;s, and despite all the developments in gaming that have happened since then, I still have the image of the lone game developer somewhere in the foundations of my mind. I grew up idolizing 8-bit legends like Tony Crowther and Andrew Braybrook, and learned to code in a world where games had a<em>uthors </em>associated with them. Things have changed, but I still carry all that with me; I just wouldn&#8217;t feel like a proper game developer if I didn&#8217;t do at least one game on my own.</p>
<p>Oh, and the title screen is animated. It uses a pretty cool effect that I will probably write about soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2011/06/toxin-coming-soon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the size of a PNG without loading it</title>
		<link>http://richardjdare.com/blog/2010/10/getting-the-size-of-a-png-without-loading-it/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-the-size-of-a-png-without-loading-it</link>
		<comments>http://richardjdare.com/blog/2010/10/getting-the-size-of-a-png-without-loading-it/#comments</comments>
		<pubDate>Sat, 09 Oct 2010 13:37:21 +0000</pubDate>
		<dc:creator><![CDATA[Richard Dare]]></dc:creator>
				<category><![CDATA[game development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[png]]></category>

		<guid isPermaLink="false">http://richardjdare.com/blog/?p=33</guid>
		<description><![CDATA[My current iPhone game (more info coming soon, I promise!) has a resource system that allows me to load texture maps when I need them.]]></description>
				<content:encoded><![CDATA[<p>My current iPhone game (more info coming soon, I promise!) has a resource system that allows me to load texture maps when I need them and unload them when I don’t. This helps makes the game very memory efficient, but it had one annoying flaw. At program startup I had to load and unload several textures to get size information I needed to set up other game structures. To avoid this wasteful operation I started looking at ways to extract information from a png without loading the whole thing.</p>
<p>This <a href="http://stackoverflow.com/questions/1551300/get-size-of-image-without-loading-in-to-memory">StackOverflow discussion</a> was useful, along with this <a href="http://www.w3.org/TR/PNG/">guide to the png format</a> from the W3C, but unfortunately, Apple threw a spanner in the works.</p>
<p>If you’ve been programming the iPhone for a while you’ll know that by default, Xcode premultiplies the alpha channel of PNG files before adding them to the application bundle. It also adds its own undocumented data chunk to the beginning of the file, breaking PNG guidelines which state that the IHDR chunk should always be at the beginning.</p>
<p>This Objective C function will return the size of an image for both standard and Apple PNGs. It scans through the file looking for the IHDR chunk, making no assumptions about its position. It should be easy enough to convert to other members of the C family.</p>
<div style="margin-top:32px;">
<style>
.wp_syntax{overflow-y:auto;max-height:400px};
</style>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//----------------------------------------------------------------------------</span>
<span style="color: #11740a; font-style: italic;">// GetPNGSize() - gets PNG Dimensions without loading any data</span>
<span style="color: #11740a; font-style: italic;">//----------------------------------------------------------------------------</span>
<span style="color: #a61390;">BOOL</span> GetPNGSize<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>fileName,CGSize <span style="color: #002200;">*</span>pngSize<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// PNG Signature</span>
    <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> png_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span><span style="color: #2400d9;">137</span>, <span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">78</span>, <span style="color: #2400d9;">71</span>, <span style="color: #2400d9;">13</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">26</span>, <span style="color: #2400d9;">10</span><span style="color: #002200;">&#125;</span>;
    <span style="color: #11740a; font-style: italic;">// IHDR chunk identifier</span>
    <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> ihdr_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">=</span><span style="color: #002200;">&#123;</span><span style="color: #bf1d1a;">'I'</span>,<span style="color: #bf1d1a;">'H'</span>,<span style="color: #bf1d1a;">'D'</span>,<span style="color: #bf1d1a;">'R'</span><span style="color: #002200;">&#125;</span>;
&nbsp;
    <span style="color: #a61390;">FILE</span> <span style="color: #002200;">*</span>fp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>fp <span style="color: #002200;">=</span> <span style="color: #a61390;">fopen</span><span style="color: #002200;">&#40;</span>fileName,<span style="color: #bf1d1a;">&quot;rb&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// load png signature and check it</span>
    <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> sigBuf<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">fread</span><span style="color: #002200;">&#40;</span>sigBuf, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">8</span>, fp<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">memcmp</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>sigBuf<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&amp;</span>png_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">fclose</span><span style="color: #002200;">&#40;</span>fp<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// examine chunks until we find IHDR</span>
    <span style="color: #a61390;">BOOL</span> done <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #a61390;">while</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>done <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #a61390;">feof</span><span style="color: #002200;">&#40;</span>fp<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// get chunk length and type</span>
        <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">fread</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>chunkInfo, <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">8</span>, fp<span style="color: #002200;">&#41;</span>;
&nbsp;
        <span style="color: #a61390;">long</span> length <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// is this the IHDR?</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #a61390;">memcmp</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>chunkInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&amp;</span>ihdr_signature<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">4</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span> sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">8</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">fread</span><span style="color: #002200;">&#40;</span>sizeInfo,<span style="color: #2400d9;">1</span>,<span style="color: #2400d9;">8</span>,fp<span style="color: #002200;">&#41;</span>;
&nbsp;
            pngSize<span style="color: #002200;">-</span>&gt;width <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>; 
            pngSize<span style="color: #002200;">-</span>&gt;height<span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">24</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">16</span><span style="color: #002200;">&#41;</span> | <span style="color: #002200;">&#40;</span>sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#93;</span> &lt;&lt; <span style="color: #2400d9;">8</span><span style="color: #002200;">&#41;</span> | sizeInfo<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">7</span><span style="color: #002200;">&#93;</span>;
            done <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> 
        <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// skip over the data and checksum and go to next chunk</span>
            <span style="color: #a61390;">fseek</span><span style="color: #002200;">&#40;</span>fp, length<span style="color: #002200;">+</span><span style="color: #2400d9;">4</span> , <span style="color: #a61390;">SEEK_CUR</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">fclose</span><span style="color: #002200;">&#40;</span>fp<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> done;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

</div>
]]></content:encoded>
			<wfw:commentRss>http://richardjdare.com/blog/2010/10/getting-the-size-of-a-png-without-loading-it/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
