Short question:
Does anyone know why HQ2x is producing corrupt PNGs or knows of another implementation that supports transparency that I can try.
Longer question:
I’m attempting to use HQ2x (with alpha support) to produce upscaled images.
The problem however is that the following code produces corrupt PNGs.
UIImage *image = [UIImage imageNamed:@"testIcon.png"];
NSMutableData *data = [NSMutableData dataWithData:UIImagePNGRepresentation(image)];
uint32_t *imageData = (uint32_t *) malloc([data length]);
uint32_t *newImageData = (uint32_t *) malloc(image.size.width * 4 * image.size.height * 4 * sizeof(uint32_t));
[data getBytes:imageData];
hqxInit();
hq2x_32(imageData, newImageData, image.size.width, image.size.height);
FILE *file;
file = fopen("/Users/iPhone/Desktop/out.png","w");
fwrite(newImageData, image.size.width * 4 * image.size.height * 4 * sizeof(uint32_t), 1, file);
fclose(file);
Examining the first few bytes of the output, I can deduce that something has gone wrong as there are two intermingled PNG Magic Numbers (89 50 4e 47 0d 0a 1a 0a):
89 50 4E 47 89 50 4E 47 0D 0A 1A 0A 0D 0A 1A 0A
The input image is not corrupted.
This is an objective-c program, but the important bits are in pure c.
Parts of this answer were discussed in chat. (Transcript)
The blob you write to
/Users/iPhone/Desktop/out.pngis raw pixel data. You have to encode it as PNG becausehq2x_32doesn’t do it automatically.If you’re not decoding your PNG before you pass it to
hq2x_32then your input and subsequently your output is wrong.Take a look at
hqx.c, you can see that the data passed tohq2x_32is first decoded and then the output is manually encoded. Both transformations are handled using DevIL.