3434
3535using namespace OIIO ;
3636
37+ void * getTypedPointer (OIIO::ImageBuf& buf, const OIIO::TypeDesc& type) {
38+ switch (type.basetype ) {
39+ case OIIO::TypeDesc::UINT8:
40+ return (uint8_t *)buf.localpixels ();
41+ case OIIO::TypeDesc::UINT16:
42+ return (uint16_t *)buf.localpixels ();
43+ case OIIO::TypeDesc::UINT32:
44+ return (uint32_t *)buf.localpixels ();
45+ case OIIO::TypeDesc::UINT64:
46+ return (uint64_t *)buf.localpixels ();
47+ case OIIO::TypeDesc::HALF:
48+ return (half*)buf.localpixels ();
49+ case OIIO::TypeDesc::FLOAT:
50+ return (float *)buf.localpixels ();
51+ case OIIO::TypeDesc::DOUBLE:
52+ return (double *)buf.localpixels ();
53+ default :
54+ return nullptr ;
55+ }
56+ }
57+
3758bool solidify_main (const std::string& inputFileName, const std::string& outputFileName, std::pair<ImageBuf, ImageBuf> mask_pair,
3859 QProgressBar* progressBar, MainWindow* mainWindow) {
3960 Timer g_timer;
@@ -42,7 +63,7 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
4263 // Generate a random delay
4364 std::random_device rd;
4465 std::mt19937 gen (rd ());
45- std::uniform_int_distribution<> distr (1 , 1000 ); // delay range in milliseconds
66+ std::uniform_int_distribution<> distr (1 , 500 ); // delay range in milliseconds
4667
4768 // Sleep for the random delay
4869 std::this_thread::sleep_for (std::chrono::milliseconds (distr (gen)));
@@ -335,11 +356,14 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
335356
336357 ImageSpec& ospec = out_buf.specmod ();
337358 if (settings.alphaMode == 1 ) {
338- ospec.nchannels = grayscale ? 2 : 4 ; // Only write RGB channels
359+ ospec.nchannels = grayscale ? 2 : 4 ; // Write RGB and alpha channels
360+ }
361+ else if (settings.alphaMode == 0 ) {
362+ ospec.nchannels = grayscale ? 1 : 3 ; // Only write RGB channels
339363 }
340364 else {
341- ospec.nchannels = grayscale ? 1 : 3 ; // Write RGB and alpha channels
342- }
365+ ospec.nchannels = 1 ; // Only write alpha channel
366+ }
343367
344368 ospec.erase_attribute (" Exif:LensSpecification" );
345369 LOG (info) << " OIIO Libtiff EXIF fix deleting: " << " Exif:LensSpecification" << std::endl;
@@ -368,7 +392,6 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
368392 ospec.erase_attribute(name);
369393 }
370394*/
371- // /////
372395/*
373396 // Initialize a vector to hold pairs of old and new attribute names
374397 std::vector<std::pair<std::string, std::string>> attrs_to_rename;
@@ -414,7 +437,6 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
414437
415438 ospec.alpha_channel = -1 ; // No alpha channel
416439 // int bits = settings.bitDepth != -1 ? settings.getBitDepth() : 2;
417-
418440 // rspec.attribute("oiio:BitsPerSample", bits);
419441 ospec.attribute (" pnm:binary" , 1 );
420442 ospec.attribute (" oiio:UnassociatedAlpha" , 1 );
@@ -447,7 +469,24 @@ bool solidify_main(const std::string& inputFileName, const std::string& outputFi
447469
448470 LOG (info) << " Writing " << outputFileName << std::endl;
449471
450- auto ok = out->write_image (out_format, out_buf.localpixels (),out_buf.pixel_stride (), out_buf.scanline_stride (), out_buf.z_stride (), *m_progress_callback, progressBar);
472+ bool ok = false ;
473+
474+ if (settings.alphaMode != 2 ) {
475+ ok = out->write_image (out_format, out_buf.localpixels (), out_buf.pixel_stride (), out_buf.scanline_stride (), out_buf.z_stride (), *m_progress_callback, progressBar);
476+ }
477+ else {
478+ int channel_to_extract = grayscale ? 1 : 3 ; // for Alpha channle from RGBA
479+ int channels = grayscale ? 2 : 4 ;
480+ int bytes = input_buf.spec ().format .size (); //
481+
482+ ok = out->write_image (out_format,
483+ (char *)out_buf.localpixels () + channel_to_extract * bytes, // pointer to the first pixel to write
484+ channels * bytes, // x stride
485+ out_buf.scanline_stride (), // y stride
486+ out_buf.z_stride (), // z stride
487+ *m_progress_callback, progressBar);
488+ }
489+
451490 if (!ok) {
452491 LOG (error) << " Error writing " << outputFileName << std::endl;
453492 LOG (error) << out->geterror () << std::endl;
0 commit comments