Jump to content

How it's done?


Recommended Posts

I'm trying to write an image loading procedure in CodeLab. This works when loading an image for the first time. When I load a new image, the canvas remains unchanged and contains the image that was loaded for the first time.

This is the source code:

Spoiler

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
FilenameControl image = @""; // Image|png|jpg|gif|bmp
#endregion

private Surface _img = null;
private string imgPath;

protected Surface img
{
    get
    {
        if (_img != null)
        {
            return _img;
        }
        GetImageFromFile();
        return _img;
    }
}

private void GetImageFromFile()
{
    imgPath = image;
    Bitmap bitmap = null;
    try
    {
        bitmap = (Bitmap)Image.FromFile(imgPath, false);
    }
    catch (Exception)
    {
    }
    if (bitmap != null)
    {
        _img = Surface.CopyFromBitmap(bitmap);
        return;
    }
    _img = null;
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    // Delete any of these lines you don't need
    Rectangle selection = EnvironmentParameters.SelectionBounds;
    
    ColorBgra CurrentPixel;
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            CurrentPixel = src[x,y];

            if (img != null) CurrentPixel = img.GetBilinearSampleWrapped(x,y);

            dst[x,y] = CurrentPixel;
        }
    }
}

 

 

How can I fix this?

Link to comment
Share on other sites

This code has two issues:

 

- After _img is successfully set, it never gets invalidated (set to null), so GetImageFromFile() will never run more than once.

- Conversely, if an image file is not successfully loaded into _img, GetImageFromFile() will run on every single pixel, and fail over and over again. A serious performance issue.

 

Did you find this code somewhere on the forum? If so, it needs to dealt with. It's bad code, and shouldn't be used.

 

I would just recommend doing this stuff in PreRender().

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...