c - CoreGraphics: Encode RGBA data to PNG -


i trying use c interface of coregraphics & corefoundation save buffer of 32-bit rgba data (as void*) png file. when try finialize cgimagedestinationref, following error message printed console:

libpng error: no idats written file

as far can tell, cgimageref i'm adding cgimagedestinationref valid.

relavent code:

void saveimage(const char* szimage, void* data, size_t datasize, size_t width, size_t height) {     cfstringref name = cfstringcreatewithcstring(null, szimage, kcfstringencodingascii);     cfurlref texture_url = cfurlcreatewithfilesystempath(                                                      null,                                                      name,                                                      kcfurlposixpathstyle,                                                      false);      cgcolorspaceref colorspace = cgcolorspacecreatedevicergb();     cgdataproviderref dataprovider = cgdataprovidercreatewithdata(null, data, datasize, null);     cgimageref image = cgimagecreate(width, height, 8, 32, 32 * width, colorspace,                                  kcgimagealphalast | kcgbitmapbyteorderdefault, dataprovider,                                  null, false, kcgrenderingintentdefault);      // image i/o programming guide, "working image destinations"     float compression = 1.0; // lossless compression if available.     int orientation = 4; // origin @ bottom, left.     cfstringref mykeys[3];     cftyperef   myvalues[3];     cfdictionaryref myoptions = null;     mykeys[0] = kcgimagepropertyorientation;     myvalues[0] = cfnumbercreate(null, kcfnumberinttype, &orientation);     mykeys[1] = kcgimagepropertyhasalpha;     myvalues[1] = kcfbooleantrue;     mykeys[2] = kcgimagedestinationlossycompressionquality;     myvalues[2] = cfnumbercreate(null, kcfnumberfloattype, &compression);     myoptions = cfdictionarycreate( null, (const void **)mykeys, (const void **)myvalues, 3,                                &kcftypedictionarykeycallbacks, &kcftypedictionaryvaluecallbacks);      cfstringref type = cfstringcreatewithcstring(null, "public.png", kcfstringencodingascii);     cgimagedestinationref dest = cgimagedestinationcreatewithurl(texture_url, type, 1, myoptions);      cgimagedestinationaddimage(dest, image, null);      if (!cgimagedestinationfinalize(dest))     {         // error!     }      cfrelease(image);     cfrelease(colorspace);     cfrelease(dataprovider);     cfrelease(dest);     cfrelease(texture_url); } 

this post similar, except i'm not using objective c interface: saving 32 bit rgba buffer .png file (cocoa osx)

answering own questions:

  1. in addition issues pointed out nsgod, idat issue invalid parameter cgimagecreate(): parameter 5 bytesperrow, not bitsperrow. 32 * width incorrect; 4 * width correct.

  2. despite this page of official documentation lists, utcoretypes.h located in coreservices.framework macosx, not mobilecoreservices.framework.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -