@@ -359,6 +359,48 @@ void *load(void *args)
359359 // not webp, try jpg png etc.
360360#if _WIN32
361361 pixeldata = wic_decode_image (imagepath.c_str (), &w, &h, &c);
362+ if (pixeldata)
363+ {
364+ // WIC channel conversion logic similar to stb_image
365+ if (c == 1 )
366+ {
367+ // grayscale -> rgb
368+ unsigned char *rgbdata = (unsigned char *)malloc (w * h * 3 );
369+ if (rgbdata)
370+ {
371+ for (int i = 0 ; i < w * h; i++)
372+ {
373+ unsigned char gray = pixeldata[i];
374+ rgbdata[i * 3 + 0 ] = gray; // B
375+ rgbdata[i * 3 + 1 ] = gray; // G
376+ rgbdata[i * 3 + 2 ] = gray; // R
377+ }
378+ free (pixeldata);
379+ pixeldata = rgbdata;
380+ c = 3 ;
381+ }
382+ }
383+ else if (c == 2 )
384+ {
385+ // grayscale + alpha -> rgba
386+ unsigned char *rgbadata = (unsigned char *)malloc (w * h * 4 );
387+ if (rgbadata)
388+ {
389+ for (int i = 0 ; i < w * h; i++)
390+ {
391+ unsigned char gray = pixeldata[i * 2 ];
392+ unsigned char alpha = pixeldata[i * 2 + 1 ];
393+ rgbadata[i * 4 + 0 ] = gray; // B
394+ rgbadata[i * 4 + 1 ] = gray; // G
395+ rgbadata[i * 4 + 2 ] = gray; // R
396+ rgbadata[i * 4 + 3 ] = alpha; // A
397+ }
398+ free (pixeldata);
399+ pixeldata = rgbadata;
400+ c = 4 ;
401+ }
402+ }
403+ }
362404#else // _WIN32
363405 pixeldata = stbi_load_from_memory (filedata, length, &w, &h, &c, 0 );
364406 if (pixeldata)
@@ -401,9 +443,9 @@ void *load(void *args)
401443 path_t output_filename2 = get_file_name_without_extension (ltp->output_files [i]) + PATHSTR (' .' ) + ext;
402444 v.outpath = output_filename2;
403445#if _WIN32
404- fwprintf (stderr, L" ℹ️ Info: Image %s has alpha channel!\n " , imagepath.c_str ());
446+ fwprintf (stderr, L" ℹ️ Info: Image %s has alpha channel! Converting to RGB for JPEG output. \n " , imagepath.c_str ());
405447#else // _WIN32
406- fprintf (stderr, " ℹ️ Info: Image %s has alpha channel!\n " , imagepath.c_str ());
448+ fprintf (stderr, " ℹ️ Info: Image %s has alpha channel! Converting to RGB for JPEG output. \n " , imagepath.c_str ());
407449#endif // _WIN32
408450 }
409451
@@ -412,9 +454,9 @@ void *load(void *args)
412454 else
413455 {
414456#if _WIN32
415- fwprintf (stderr, L" 🚨 Error: Couldn't read the image '%s'!\n " , imagepath.c_str ());
457+ fwprintf (stderr, L" 🚨 Error: Couldn't read the image '%s'! (channels: %d) \n " , imagepath.c_str (), c );
416458#else // _WIN32
417- fprintf (stderr, " 🚨 Error: Couldn't read the image '%s'!\n " , imagepath.c_str ());
459+ fprintf (stderr, " 🚨 Error: Couldn't read the image '%s'! (channels: %d) \n " , imagepath.c_str (), c );
418460#endif // _WIN32
419461 }
420462 }
@@ -637,6 +679,10 @@ void *save(void *args)
637679 else if (ext == PATHSTR (" jpg" ) || ext == PATHSTR (" JPG" ) || ext == PATHSTR (" jpeg" ) || ext == PATHSTR (" JPEG" ))
638680 {
639681#if _WIN32
682+ if (verbose)
683+ {
684+ fwprintf (stderr, L" 🔧 Debug: Saving JPEG with %d channels, size %dx%d\n " , v.outimage .elempack , v.outimage .w , v.outimage .h );
685+ }
640686 success = wic_encode_jpeg_image (v.outpath .c_str (), v.outimage .w , v.outimage .h , v.outimage .elempack , v.outimage .data );
641687#else
642688 success = stbi_write_jpg (v.outpath .c_str (), v.outimage .w , v.outimage .h , v.outimage .elempack , v.outimage .data , 100 - (int )stp->compression );
0 commit comments