<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>bitmapData &#8211; Wade Tregaskis</title>
	<atom:link href="https://wadetregaskis.com/tags/bitmapdata/feed/" rel="self" type="application/rss+xml" />
	<link>https://wadetregaskis.com</link>
	<description></description>
	<lastBuildDate>Thu, 21 Mar 2024 05:04:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wadetregaskis.com/wp-content/uploads/2016/03/Stitch-512x512-1-256x256.png</url>
	<title>bitmapData &#8211; Wade Tregaskis</title>
	<link>https://wadetregaskis.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">226351702</site>	<item>
		<title>getBitmapDataPlanes can break NSImages &#038; NSBitmapImageReps</title>
		<link>https://wadetregaskis.com/getbitmapdataplanes-can-break-nsimages-nsbitmapimagereps/</link>
					<comments>https://wadetregaskis.com/getbitmapdataplanes-can-break-nsimages-nsbitmapimagereps/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Thu, 21 Mar 2024 04:44:26 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[10-bit]]></category>
		<category><![CDATA[bitmapData]]></category>
		<category><![CDATA[Bugs!]]></category>
		<category><![CDATA[getBitmapDataPlanes]]></category>
		<category><![CDATA[NSBitmapImageRep]]></category>
		<category><![CDATA[NSImage]]></category>
		<category><![CDATA[Sad]]></category>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=7889</guid>

					<description><![CDATA[Today was one of those days where you plan to real make a dent in your todo list, and end up spending the entire day debugging why the hell some images are suddenly rendering as completely opaque black. Long story short, on at least some bitmap images, as soon as you call getBitmapDataPlanes it somehow&#8230; <a class="read-more-link" href="https://wadetregaskis.com/getbitmapdataplanes-can-break-nsimages-nsbitmapimagereps/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>Today was one of those days where you plan to real make a dent in your todo list, and end up spending the entire day debugging why the hell some images are suddenly rendering as completely opaque black.</p>



<p>Long story short, on at least some bitmap images, as soon as you call <code><a href="https://developer.apple.com/documentation/appkit/nsbitmapimagerep/1395490-getbitmapdataplanes" data-wpel-link="external" target="_blank" rel="external noopener">getBitmapDataPlanes</a></code> it somehow permanently breaks that <code><a href="https://developer.apple.com/documentation/appkit/nsimage" data-wpel-link="external" target="_blank" rel="external noopener">NSImage</a></code> and <code><a href="https://developer.apple.com/documentation/appkit/nsbitmapimagerep" data-wpel-link="external" target="_blank" rel="external noopener">NSBitmapImageRep</a></code>.  The telltale sign of this &#8211; aside from the image rendering incorrectly &#8211; is the stderr message from AppKit:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><code>Failed to extract pixel data from NSBitmapImageRep. Error: -21778</code></p>
</blockquote>



<p>This definitely occurs with 10-bit AVIFs, but it&#8217;s not limited to that bit depth nor that file format, because I found <a href="https://developer.apple.com/forums/thread/700010" data-wpel-link="external" target="_blank" rel="external noopener">the <em>one</em> other mention of this error message online</a>, where the source image was 1-bit.</p>



<p>I&#8217;ve never seen this happen with 8-bit, 12-bit, or 16-bit images, AVIF or otherwise.</p>



<p>This makes me suspect it&#8217;s tied to the internal pixel format &#8211; 10-bit AVIFs always load as 10 / 40 (bits per sample / bits per pixel), irrespective of whether they have an alpha channel.  8-bit AVIFs are always 8 / 32, 12-bit are always 12 / 48.  Maybe <code>NSBitmapImageRep</code> has a bug when the bits per pixel isn&#8217;t a multiple of 16?</p>



<p>If you call <code><a href="https://developer.apple.com/documentation/appkit/nsbitmapimagerep/1395421-bitmapdata" data-wpel-link="external" target="_blank" rel="external noopener">bitmapData</a></code> instead you get the same error message but it does <em>not</em> stop the <code>NSImage</code> / <code>NSBitmapImageRep</code> from rendering correctly.  But the pointer returned from <code>bitmapData</code> points to opaque black.  It&#8217;s particularly weird that it returns a pointer to a valid memory allocation, of at least the expected size, yet the contents are nothing but zeroes.  Seems like it pre-allocates some output buffer, as zeroed memory, and then fails to actually write to that buffer.  Yet it returns it anyway, instead of returning nil. 😕</p>



<p>FB13693411.</p>



<h2 class="wp-block-heading">Partial workaround</h2>



<p>If you call <code><a href="https://developer.apple.com/documentation/appkit/nsimage/1519890-recache" data-wpel-link="external" target="_blank" rel="external noopener">recache</a></code> on the <code>NSImage</code> afterwards, the image is capable of rendering correctly again.  That doesn&#8217;t help if your objective is to access the bitmap bytes, but at least if you don&#8217;t &#8211; e.g. you&#8217;re encountering this only because some library code is triggering the bug &#8211; you might be able to work around it through <code>recache</code>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/getbitmapdataplanes-can-break-nsimages-nsbitmapimagereps/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			<media:content url="https://wadetregaskis.com/wp-content/uploads/2024/03/Rainbow-Stitch-10-bit-sRGB.avif" medium="image" />
<post-id xmlns="com-wordpress:feed-additions:1">7889</post-id>	</item>
	</channel>
</rss>
