MibIconCache

class core.MibIconCache

MIBIBONCACHE - Static cache for MIB icons and images backed by a MAT resource file.

Provides fast access to cached icon and image files stored in a MAT resource file. Improves performance up to 10 times relative to direct disk reads. The cache is automatically built from asset directories and persists across calls.

Basic usage:

img = core.MibIconCache.get('icons', 'about_24px');
img = core.MibIconCache.get('images', 'mib_question');
core.MibIconCache.buildResourceFile(resourceFilePath, assetsDir);

Example - benchmark cache performance vs. direct file reads:

% Scan icons directory
files = dir('C:\MATLAB\MIB3\mib\assets\icons\*.png');
filesNames1 = {files.name};
filesNames2 = cellfun(@(f) erase(f, '.png'), filesNames1, 'UniformOutput', false);

% Test 1: Cache (returns image arrays)
t1 = tic;
imgs = cell(numel(filesNames2), 1);
for i = 1:numel(filesNames2)
    imgs{i} = core.MibIconCache.get('icons', filesNames2{i});
end
timeCache = toc(t1);

% Test 2: Direct imread (actually load images)
t1 = tic;
imgs2 = cell(numel(filesNames1), 1);
for i = 1:numel(filesNames1)
    imgs2{i} = imread(fullfile(obj.controller.mibPath, 'assets/icons', filesNames1{i}));
end
timeFile = toc(t1);

% Show performance improvement
fprintf('Time difference (timeCache/timeFile) = %f\n', timeCache/timeFile);
Method Summary
static buildResourceFile(assetsDir, resourceFilePath)

BUILDRESOURCEFILE - Scan asset folders and rebuild the resource cache file.

Syntax:
MibIconCache.buildResourceFile(assetsDir, resourceFilePath)

Scans the asset directories (icons, images) for image files and builds a MAT resource file containing all images. This is called automatically when the resource file is missing or when a requested icon is not found.

Input Arguments:
  • assetsDir - (optional) [char] path to assets directory; when empty uses default (MIB3/mib/assets)

  • resourceFilePath - (optional) [char] path for output resource file; when empty uses default (MIB3/mib/assets/mib_icons.res)

Example - build resource file from assets directory:

assetsDir = fullfile(obj.mibPath, 'assets');
resourceFile = fullfile(obj.mibPath, 'assets', 'mib_icons.res');
core.MibIconCache.buildResourceFile(assetsDir, resourceFile);
static get(foldername, name, resourceFilePath, assetsDir)

GET - Return icon/image from the resource cache by name.

Syntax:
img = MibIconCache.get(foldername, name, resourceFilePath, assetsDir)

Retrieves a cached icon or image from the MAT resource file. If the cache is not yet loaded, it is loaded from the persistent variable. If the resource file is missing, it is automatically generated from the assets directory.

Input Arguments:
  • foldername - [char] folder name specifying which type of resource:

    • 'icons' - icons folder

    • 'images' - images folder

  • name - [char] icon/image name without extension

  • resourceFilePath - (optional) [char] full path to resource file (default: MIB3/mib/assets/mib_icons.res)

  • assetsDir - (optional) [char] path to assets directory containing ‘images’ and ‘icons’ folders (default: MIB3/mib/assets)

Output Arguments:
  • img - [numeric array] image data for the requested icon/image

Example 1 - get icon from the icons folder:

img = core.MibIconCache.get('icons', 'about_24px');

Example 2 - get image from the images folder:

img = core.MibIconCache.get('images', 'mib_question');
static getDefaultAssetsDir()

GETDEFAULTASSETSDIR - Get the default folder where assets files live.

Syntax:

function assetsDir = getDefaultAssetsDir()

static getDefaultResourcePath()

GETDEFAULTRESOURCEPATH - Get location of the default MAT resource path.

Syntax:

function resourceFilename = getDefaultResourcePath()

(assetsmib_icons.res) for icons and images

static getIconData(iconData)

GETICONDATA - Extract icon image array from cache data and apply transparency.

Syntax:

function img = getIconData(iconData)

Input Arguments:
  • iconData - a structure with

  • .cdata - matrix ([height, width, col_channel]) with intensity values for the icon

  • .alpha - matrix with the alpha value, can be empty