[V8i MDL] White as transparent color for mdlImage_extCreateFileFromRGB

I'm using mdlImage_extCreateFileFromRGB to export design file as image with setting Preferences -> Black background -> White set to true. So I have white background in my view.


What I need is to set white background to be transparent when exported to image, but I'm not sure how to set this transparencyP parameter. From Docs:



As for black background this works fine:

Point2d imageSize;
byte *transp = (byte*)malloc(4);

byte *inputMap = (byte*)malloc(3 * g_tileCols * g_tileRows);

imageSize.x = 500;

imageSize.y = 500;

mdlImage_saveViewToRGB( &inputMap, &imageSize, WIREFRAME, FALSE, 0, viewIdx);
results = mdlImage_extCreateFileFromRGB (fn, g_fileFormat, g_colorMode, &imageSize, inputMap, g_Compression, transp, NULL);
Parents
  • Hi Mantas,

    The transparency parameter is only use with the PNG file format.

    I guess if your color mode is RGBA the value of the transparency should be :

    transp[0] = 255;
    transp[1] = 255;
    transp[2] = 255;

    HTH,

    Mathieu



  • Thanks for quick response and sorry for not including parameters, but I still can get it to work. Probably the reason it failed before is because I was using COLORMODE_RGB not RGBA. But if I set colormode to COLORMODE_RGBA I get "PNG warning: invalid TRNS" and it crashes with System Fault: 5 error on mdlImage_extCreateFileFromRGB.

    Now my code is:

    Private int g_fileFormat = IMAGEFILE_PNG;
    Private int g_Compression = COMPRESSTYPE_DEFLATE;
    Private int g_colorMode = COLORMODE_RGBA;
    
    ...
    
    byte *transp = NULL;
    byte white[] = {255, 255, 255};
    transp = (byte*)white;
    
    mdlImage_saveViewToRGB( &inputMap, &imageSize, WIREFRAME, FALSE, 0, viewIdx);
    
    results = mdlImage_extCreateFileFromRGB (fn, g_fileFormat, g_colorMode, &imageSize, inputMap, g_Compression, transp, NULL);
Reply
  • Thanks for quick response and sorry for not including parameters, but I still can get it to work. Probably the reason it failed before is because I was using COLORMODE_RGB not RGBA. But if I set colormode to COLORMODE_RGBA I get "PNG warning: invalid TRNS" and it crashes with System Fault: 5 error on mdlImage_extCreateFileFromRGB.

    Now my code is:

    Private int g_fileFormat = IMAGEFILE_PNG;
    Private int g_Compression = COMPRESSTYPE_DEFLATE;
    Private int g_colorMode = COLORMODE_RGBA;
    
    ...
    
    byte *transp = NULL;
    byte white[] = {255, 255, 255};
    transp = (byte*)white;
    
    mdlImage_saveViewToRGB( &inputMap, &imageSize, WIREFRAME, FALSE, 0, viewIdx);
    
    results = mdlImage_extCreateFileFromRGB (fn, g_fileFormat, g_colorMode, &imageSize, inputMap, g_Compression, transp, NULL);
Children
  • Hi Mantas,

    The crash was because the mdlImage_extCreateFileFromRGB expected an RGBA image. The transparency parameter seems to work with PNG and RGB buffer, but whatever the value black is always seem to be considered as the alpha value.

    Another solution is to manually convert the RGB view screenshot buffer into an RGBA buffer.

    Here is the code that is working on my computer :

    int g_fileFormat = IMAGEFILE_PNG;
    
    int g_Compression = COMPRESSTYPE_DEFLATE;
    
    int g_colorMode = COLORMODE_RGBA;
    
    
    
    
    Point2d imageSize;
    
    imageSize.x = 500;
    
    imageSize.y = 500;
    
    byte *inputMap = (byte*)malloc(3 * imageSize.x * imageSize.y);
    
    
    
    
    mdlImage_saveViewToRGB( &inputMap, &imageSize, WIREFRAME, FALSE, 0, 0);
    
    //Convert RGB to RGBA
    
    byte *inputMapWithAlpha = (byte*)malloc(4 * imageSize.x * imageSize.y);
    
    size_t nbPixels = (size_t)imageSize.x * imageSize.y;
    
    for (size_t lineInd = 0; lineInd < (size_t)imageSize.x; lineInd++)
    
    {
    
    size_t inputRedInd = lineInd * imageSize.y * 3;
    
    size_t inputBlueInd = inputRedInd + imageSize.y;
    
    size_t inputGreenInd = inputBlueInd + imageSize.y;
    
    
    
    
    size_t redInd = lineInd * imageSize.y * 4;
    
    size_t blueInd = redInd + imageSize.y;
    
    size_t greenInd = blueInd + imageSize.y;
    
    size_t alphaInd = greenInd + imageSize.y;
    
    for (size_t colInd = 0; colInd < (size_t)imageSize.y; colInd++)
    
    {
    
    inputMapWithAlpha[redInd] = inputMap[inputRedInd];
    
    inputMapWithAlpha[blueInd] = inputMap[inputBlueInd];
    
    inputMapWithAlpha[greenInd] = inputMap[inputGreenInd];
    
    if (inputMap[inputRedInd] == 0xFF && inputMap[inputBlueInd] == 0xFF && inputMap[inputGreenInd] == 0xFF)
    
    {
    
    inputMapWithAlpha[alphaInd] = 0x00;
    
    }
    
    else
    
    {
    
    inputMapWithAlpha[alphaInd] = 0xFF;
    
    }
    
    
    
    
    inputRedInd++;
    
    inputBlueInd++;
    
    inputGreenInd++;
    
    redInd++;
    
    blueInd++;
    
    greenInd++;
    
    alphaInd++;
    
    }
    
    }
    
    int results = mdlImage_extCreateFileFromRGB ("D:\\BeCommunities\\Transparency\\out.png", g_fileFormat, g_colorMode, &imageSize, inputMapWithAlpha, g_Compression, 0/*white*/, NULL);
    
    
    
    
    assert(results == SUCCESS);
    
    free(inputMap);
    
    free(inputMapWithAlpha);
    

    Thanks,

    Mathieu



  • Thanks Mathieu, this seems to be a working solution.

    Finally this week's headache is gone. Cheers!

    Mantas