imageworsener icon indicating copy to clipboard operation
imageworsener copied to clipboard

ImageWorsener corrupts image when used as optimization tool in FileOptimizerFull

Open devedse opened this issue 2 years ago • 18 comments

Hello,

I'm the owner of DeveImageOptimizerWPF which uses https://nikkhokkho.sourceforge.io/static.php?page=FileOptimizer to optimize images. (But in parallel with some additional image verification after the optimization has completed).

When optimizing a certain .bmp image though the resulting image that was created by imagew was not equal to the input image:

Original Optimized
image image

The command that was used is:

imagew.exe -noresize -zipcmprlevel 9 -outfmt bmp -compress "rle" "MonoGameIcon.bmp" "MonoGameIconOriginal_Optimized.bmp"

I've also attached the Original bmp and Optimized bmp files in a zip file (can't attach them directly as .bmp).

failingbmp.zip

devedse avatar Mar 03 '22 20:03 devedse

MonoGameIcon.bmp is a 32-bit bmp with alpha, and the thumbnail fails to reflect that (transparency shown as black).

I'm not positive but I don't think 8 bit bmp's support transparency so it would have to flatten it, and no background color was specified.

dtfinch avatar Mar 04 '22 18:03 dtfinch

Hmmm that could be the reason. All imaging tools I've used do display the image as expected though so I would hope this tool could have the same result.

devedse avatar Mar 04 '22 20:03 devedse

Thanks for the report. I think it's technically working as intended, but I can see there's sort of an issue here.

Your source image is in BMPv5 format, which supports transparency. By default, Imagew only writes the more portable BMPv3 format, which does not support transparency. So there's a problem.

If you want to only write portable BMPv3, then transparency has to be sacrificed. You could use "-bkgd 000", for example.

If you want to only write BMPv5, use "-opt bmp:version=5", and then it will "work". But unfortunately, it will unconditionally use BMPv5 format, which is probably not what you want.

I don't think there's a way to get Imagew to intelligently choose the BMP version. Maybe there should be. Though I'm not sure exactly how it should work. Should it depend on whether the image has transparency, or on the BMP version of the source image, or what?

jsummers avatar Mar 06 '22 16:03 jsummers

Hey @jsummers , thanks for your response :).

I think the way it should work is that it that it should use BMPv3 in all cases except if a feature is used in the original that can only be saved in the v5 format.

I'm not sure if there's more features like transparency that BMPv3 doesn't support.

Let me know what you think 😄

Edit: I could imagine the code being something like:

outputFormat = BMPv3;

if (inputImage.UsesFeatureABC)
{
    outputFormat = BMPv4;
}

if (inputImage.UsesTransparency)
{
    outputFormat = BMPv5;
}

if (inputImage.UsesFeatureXYZ)
{
    outputFormat = BMPv5;
}

devedse avatar Mar 14 '22 00:03 devedse

@jsummers, Do you think this is something that could be implemented possibly?

devedse avatar Mar 26 '22 15:03 devedse

I'm thinking that I'll add an option, probably "-opt bmp:version=auto", that will cause BMPv5 to be written if the image has transparency, otherwise BMPv3. Just need to figure out how to do that.

I'm not sure that making it the default behavior is a good idea, for compatibility reasons. Then again, maybe BMPv5 is so widely supported nowadays that it could be done. I dunno.

jsummers avatar Mar 26 '22 19:03 jsummers

The thing is that if the original image had transparency it must've been v5 so why not have the output v5 as well.

devedse avatar Mar 26 '22 19:03 devedse

Input formats other than BMP have to be accounted for.

While I am considering having Imagew remember whether the original image was in a BMPv5 file, that would be quirky, and not very consistent with the rest of the software. I'd prefer to avoid things like that.

jsummers avatar Mar 27 '22 18:03 jsummers

Ok makes sense. Let me know if you've made progress on this. I can then let Nikkho from file optimizer know so he can update the cli command.

devedse avatar Mar 27 '22 18:03 devedse

I added the "-opt bmp:version=auto" feature (commit daa9283699). If you can test it, please do.

jsummers avatar Mar 28 '22 19:03 jsummers

Could you provide me with a windows compiled executable? I can then test if it fixes the problem.

devedse avatar Mar 29 '22 00:03 devedse

@jsummers , I did some playing around with compiling the source code myself but having to manually include all the library's etc. took a bit too much time so I gave up. Do you think you could help me provide a compiled .exe file so I can try if the issue is resolved?

devedse avatar Apr 04 '22 20:04 devedse

Try the attached file. imagew-test41.zip

jsummers avatar Apr 05 '22 18:04 jsummers

Just verified it and it works! image

devedse avatar Apr 05 '22 22:04 devedse

Thank you for the fix.

javiergutierrezchamorro avatar Apr 06 '22 03:04 javiergutierrezchamorro

@jsummers, can you maybe release a new official version 1.3.5 for imageworsener?

devedse avatar Nov 02 '22 14:11 devedse

I know, it's overdue. I'll make a new release within a week or so.

jsummers avatar Nov 14 '22 19:11 jsummers

Thanks :)

devedse avatar Nov 14 '22 19:11 devedse