ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Image Struct Reference

Image loading. More...

#include <Image.h>

Inheritance diagram for Image:
AtlasImage

Public Member Functions

Class * _Image (void)
 The Image archetype.
 
Image * imageWithBytes (const uint8_t *bytes, size_t length)
 Instantiates an Image with the specified bytes.
 
Image * imageWithData (const Data *data)
 Instantiates an Image with the specified Data.
 
Image * imageWithResource (const Resource *resource)
 Instantiates an Image with the specified Resource.
 
Image * imageWithResourceName (const char *name)
 Instantiates an Image with the specified Resource name.
 
Image * imageWithSurface (SDL_Surface *surface)
 Instantiates an Image with the specified surface.
 
Image * imageWithSVG (const uint8_t *bytes, size_t length, float scale)
 Instantiates an Image by rasterizing the specified SVG.
 
Image * initWithBytes (Image *self, const uint8_t *bytes, size_t length)
 Initializes this Image with the specified bytes.
 
Image * initWithData (Image *self, const Data *data)
 Initializes this Image with the specified Data.
 
Image * initWithResource (Image *self, const Resource *resource)
 Initializes this Image with the specified Resource.
 
Image * initWithResourceName (Image *self, const char *name)
 Initializes this Image, loading the Resource by the given name.
 
Image * initWithSurface (Image *self, SDL_Surface *surface)
 Initializes this Image with the given surface.
 
Image * initWithSVG (Image *self, const uint8_t *bytes, size_t length, float scale)
 Initializes this Image by rasterizing the specified SVG at its intrinsic size times scale.
 
Image * initWithSVGSize (Image *self, const uint8_t *bytes, size_t length, SDL_Size points)
 Initializes this Image by rasterizing the specified SVG at points times the pixel density, so that Image::size reports points.
 
SDL_Size size (const Image *self)
 

Data Fields

Object object
 The superclass.
 
float scale
 Pixels of surface per point: 1 for raster images, the requested scale for SVG.
 
SDL_Surface * surface
 The backing surface.
 
const char * type
 The image type, inferred if instantiated with a Resource.
 

Protected Attributes

ImageInterface * interface [0]
 The interface type.
 

Detailed Description

Image loading.

Raster formats load at their native size. SVG, recognized by type or by sniffing, rasterizes at its intrinsic size times a scale, so that a vector asset drawn into a frame of its intrinsic size stays sharp at any pixel density; Image::size reports points.

Definition at line 41 of file Image.h.

Member Function Documentation

◆ _Image()

Class * _Image ( void  )

The Image archetype.

Returns
The Image Class.

Definition at line 452 of file Image.c.

452 {
453 static Class *clazz;
454 static Once once;
455
456 do_once(&once, {
457 clazz = _initialize(&(const ClassDef) {
458 .name = "Image",
459 .superclass = _Object(),
460 .instanceSize = sizeof(Image),
461 .interfaceSize = sizeof(ImageInterface),
462 .initialize = initialize,
463 });
464 });
465
466 return clazz;
467}
static void initialize(Class *clazz)
Definition AtlasImage.c:74
Image loading.
Definition Image.h:41

◆ imageWithBytes()

Image * imageWithBytes ( const uint8_t *  bytes,
size_t  length 
)

Instantiates an Image with the specified bytes.

Parameters
bytesThe encoded image bytes.
lengthThe length of bytes.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The new Image, or NULL on error.

◆ imageWithData()

Image * imageWithData ( const Data *  data)

Instantiates an Image with the specified Data.

Parameters
dataThe encoded image Data.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The new Image, or NULL on error.

◆ imageWithResource()

Image * imageWithResource ( const Resource *  resource)

Instantiates an Image with the specified Resource.

Parameters
resourceThe Resource containing encoded image data.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The new Image, or NULL on error.

◆ imageWithResourceName()

Image * imageWithResourceName ( const char *  name)

Instantiates an Image with the specified Resource name.

Parameters
nameThe name of a Resource containing encoded image data.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The new Image, or NULL on error.

◆ imageWithSurface()

Image * imageWithSurface ( SDL_Surface *  surface)

Instantiates an Image with the specified surface.

Parameters
surfaceThe surface.
Returns
The new Image, or NULL on error.

Definition at line 82 of file Image.c.

82 {
83 return $(alloc(Image), initWithSurface, surface);
84}
Image * initWithSurface(Image *self, SDL_Surface *surface)
Initializes this Image with the given surface.
Definition Image.c:391
SDL_Surface * surface
The backing surface.
Definition Image.h:62

◆ imageWithSVG()

Image * imageWithSVG ( const uint8_t *  bytes,
size_t  length,
float  scale 
)

Instantiates an Image by rasterizing the specified SVG.

Parameters
bytesThe SVG document.
lengthThe length of bytes.
scalePixels per point, e.g. the window's pixel density.
Returns
The new Image, or NULL on error.

Definition at line 90 of file Image.c.

90 {
91 return $(alloc(Image), initWithSVG, bytes, length, scale);
92}
float scale
Pixels of surface per point: 1 for raster images, the requested scale for SVG.
Definition Image.h:57
Image * initWithSVG(Image *self, const uint8_t *bytes, size_t length, float scale)
Initializes this Image by rasterizing the specified SVG at its intrinsic size times scale.
Definition Image.c:327

◆ initWithBytes()

Image * initWithBytes ( Image *  self,
const uint8_t *  bytes,
size_t  length 
)

Initializes this Image with the specified bytes.

Parameters
selfThe Image.
bytesThe encoded image bytes.
lengthThe length of bytes.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The initialized Image, or NULL on error.

◆ initWithData()

Image * initWithData ( Image *  self,
const Data *  data 
)

Initializes this Image with the specified Data.

Parameters
selfThe Image.
dataThe encoded image Data.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The initialized Image, or NULL on error.

◆ initWithResource()

Image * initWithResource ( Image *  self,
const Resource *  resource 
)

Initializes this Image with the specified Resource.

Parameters
selfThe Image.
resourceThe Resource containing encoded image data.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The initialized Image, or NULL on error.

◆ initWithResourceName()

Image * initWithResourceName ( Image *  self,
const char *  name 
)

Initializes this Image, loading the Resource by the given name.

An @ decoration is interpreted, and where it sits says what it is. Trailing the whole name, heart.svg@24x24 rasterizes a vector at that size in points; it is a request rather than part of any file's name, so heart.svg is what loads. Inside the name, before the extension, heart@2x.png declares that a raster carries that many pixels per point, following Apple's convention, and names the file to load. An undecorated raster prefers the variant matching the pixel density where one exists, so heart.png loads heart@2x.png on a 2x display; an undecorated vector simply rasterizes at the pixel density.

Parameters
selfThe Image.
nameThe Resource name.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The initialized Image, or NULL on error.

◆ initWithSurface()

Image * initWithSurface ( Image *  self,
SDL_Surface *  surface 
)

Initializes this Image with the given surface.

Parameters
selfThe Image.
surfaceThe backing surface.
Returns
The initialized Image, or NULL on error.
Remarks
The surface's reference count is incremented, so that you can (and should) call SDL_DestroySurface when you no longer need it. The Image will also free the surface on dealloc.
Designated initializer.

Definition at line 391 of file Image.c.

391 {
392
393 self = (Image *) super(Object, self, init);
394 if (self) {
395 self->scale = 1.f;
396
397 if (surface) {
398 if (surface->format != SDL_PIXELFORMAT_RGBA32) {
399 self->surface = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
400 } else {
401 self->surface = surface;
402 self->surface->refcount++;
403 }
404
405 assert(self->surface);
406 }
407 }
408
409 return self;
410}
static View * init(View *self)
Definition Box.c:67

◆ initWithSVG()

Image * initWithSVG ( Image *  self,
const uint8_t *  bytes,
size_t  length,
float  scale 
)

Initializes this Image by rasterizing the specified SVG at its intrinsic size times scale.

Parameters
selfThe Image.
bytesThe SVG document.
lengthThe length of bytes.
scalePixels per point, e.g. the window's pixel density.
Returns
The initialized Image, or NULL on error.

Definition at line 327 of file Image.c.

327 {
328
329 scale = scale > 0.f ? scale : 1.f;
330
331 SDL_Surface *surface = rasterizeSVG(bytes, length, MakeSize(0, 0));
332
333 if (surface && scale != 1.f) {
334 const SDL_Size size = MakeSize(
335 (int) SDL_roundf(surface->w * scale),
336 (int) SDL_roundf(surface->h * scale)
337 );
338
339 SDL_DestroySurface(surface);
340 surface = rasterizeSVG(bytes, length, size);
341 }
342
343 if (surface) {
344 self = $(self, initWithSurface, surface);
345 SDL_DestroySurface(surface);
346
347 if (self) {
348 self->type = "svg";
349 self->scale = scale;
350 }
351 } else {
352 MVC_LogWarn("%s\n", SDL_GetError());
353 self = release(self);
354 }
355
356 return self;
357}
static SDL_Surface * rasterizeSVG(const uint8_t *bytes, size_t length, SDL_Size size)
Rasterizes bytes as SVG at size pixels, or at its intrinsic size when size is zero.
Definition Image.c:310
#define MVC_LogWarn(fmt,...)
Definition Log.h:54
SDL_Size size(const Image *self)
Definition Image.c:416
const char * type
The image type, inferred if instantiated with a Resource.
Definition Image.h:67

◆ initWithSVGSize()

Image * initWithSVGSize ( Image *  self,
const uint8_t *  bytes,
size_t  length,
SDL_Size  points 
)

Initializes this Image by rasterizing the specified SVG at points times the pixel density, so that Image::size reports points.

Parameters
selfThe Image.
bytesThe SVG document.
lengthThe length of bytes.
pointsThe desired size, in points.
pixelDensityPixels per point, e.g. the window's pixel density.
Returns
The initialized Image, or NULL on error.

◆ size()

SDL_Size size ( const Image *  self)
Parameters
selfThe Image.
Returns
The Image size.

Definition at line 416 of file Image.c.

416 {
417 return MakeSize(
418 (int) SDL_roundf(self->surface->w / self->scale),
419 (int) SDL_roundf(self->surface->h / self->scale)
420 );
421}

Field Documentation

◆ interface

ImageInterface* Image::interface[0]
protected

The interface type.

Definition at line 52 of file Image.h.

◆ object

Object Image::object

The superclass.

Definition at line 46 of file Image.h.

◆ scale

float Image::scale

Pixels of surface per point: 1 for raster images, the requested scale for SVG.

Definition at line 57 of file Image.h.

◆ surface

SDL_Surface* Image::surface

The backing surface.

Definition at line 62 of file Image.h.

◆ type

const char* Image::type

The image type, inferred if instantiated with a Resource.

Definition at line 67 of file Image.h.


The documentation for this struct was generated from the following files: