Giter Club home page Giter Club logo

extension-imageloader's Introduction

extension-imageloader

Image loader extension for Defold.

Loads JPG, PNG and other images efficiently into Defold's Buffer object.

It can load images in async and sync modes. And it can only process the header of an image to provide just width, height and channels information without fully decoding the image.

The extension uses stb_image.h under the hood.

Supported platforms

Android, iOS, macOS, Linux, Windows, HTML5.

URL for Defold

https://github.com/Lerg/extension-imageloader/archive/master.zip

Syntax

imageloader.load(params)

Params table

data - string, load image from string.

filename - string, load from file. Only if data is not set.

channels - number, force the provided amount of channels (1, 3 or 4). Default is 0 - automatic.

info - boolean, if true, don't decode the entire image, just return it's information. Default is false.

no_vertical_flip - boolean, if true, don't flip the image under the hood for compatibility with Defold. Default is false.

no_async - boolean, if true, perform sync call with a listener. Default is false.

listener - function, if listener is set, the loading is async. Listener receives two arguments: self - script instance and image_resource table.

Return value

If the loading is performed without a listener, the function returns image_resource table, nil otherwise.

image_resource table

  • header - table, image information header.
    • width - number, width of the loaded image.
    • height - number, height of the loaded image.
    • channels - number, actual channel count in the image.
    • format - number, one of resource.TEXTURE_FORMAT_LUMINANCE, resource.TEXTURE_FORMAT_RGB or resource.TEXTURE_FORMAT_RGBA.
    • type, always resource.TEXTURE_TYPE_2D.
    • num_mip_maps - number, always 1.
  • buffer - buffer, pixel data buffer object.

Examples

Sync load

local data = sys.load_resource('/res/image.jpg')

local image_resource = imageloader.load{
	data = data
}

pprint(image_resource)

Async load

local data = sys.load_resource('/res/image.jpg')

imageloader.load{
	data = data,
	listener = function(self, image_resource)
		pprint(image_resource)
	end
}

Info only

local data = sys.load_resource('/res/image.jpg')

local image_resource = imageloader.load{
	data = data,
	info = true
}

pprint(image_resource)

Load from a file and convert to grayscale

local filename = directories.path_for_file('image.jpg', directories.documents)

local image_resource = imageloader.load{
	filename = filename,
	channels = 1
}

pprint(image_resource)

Change texture of a model

local data = sys.load_resource('/res/image.jpg')

local image_resource = imageloader.load{
	data = data
}

resource.set_texture(go.get('#model', 'texture0'), image_resource.header, image_resource.buffer)

extension-imageloader's People

Contributors

lerg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.