MiniLibX(3)

πŸ“¦ version 0-unstable-2026-06, in nixpkgs unstable

πŸ’– MiniLibX(3) πŸ’–

okay bestie, so this is the MiniLibX page β€” it lives in Library Functions Manual β€” and today we're doing manipulating images. yes. images. pixels. slay. let's go πŸŽ€


✨ SYNOPSYS ✨

void *
mlx_new_image ( void *mlx_ptr, int width, int height );

char *
mlx_get_data_addr ( void *img_ptr, int *bits_per_pixel, int *size_line,
int *endian );

int
mlx_put_image_to_window ( void *mlx_ptr, void *win_ptr, void *img_ptr,
int x, int y );

unsigned int
mlx_get_color_value ( void *mlx_ptr, int color );

void *
mlx_xpm_to_image ( void *mlx_ptr, char **xpm_data, int *width, int
*height );

void *
mlx_xpm_file_to_image ( void *mlx_ptr, char *filename, int *width, int
*height );

int
mlx_destroy_image ( void *mlx_ptr, void *img_ptr );

🌈 DESCRIPTION 🌈

πŸ¦‹ making a baby image: mlx_new_image()

mlx_new_image () creates a new image in memory. it hands you back a void * identifier β€” that's your lil handle, you need it to manipulate the image later, so don't lose it πŸ™ˆ. honestly it only needs two things: the size of the image you want, via the width and height parameters, and the mlx_ptr connection identifier (peep the mlx manual for that one, iykyk).

🫢 putting it on screen: mlx_put_image_to_window()

so you can draw inside the image (more on that below πŸ‘‡), and you can dump the whole image inside a specified window at any time to display it on the screen. that's literally what mlx_put_image_to_window () does. three identifiers needed here, one for each vibe:

  • the connection to the display β†’ mlx_ptr
  • the window to use β†’ win_ptr
  • the image β†’ img_ptr

and the (x, y) coordinates define where the image should be placed in the window. main character placement energy πŸ‘‘

🧚 touching the pixels: mlx_get_data_addr()

mlx_get_data_addr () returns info about the created image, so you can modify it later. the img_ptr parameter says which image we're talking about. the next three parameters should be the addresses of three different valid integers, no cap:

  • bits_per_pixel gets filled with the number of bits needed to represent a pixel color (this is also called the depth of the image).
  • size_line is the number of bytes used to store one line of the image in memory. you need this to move from one line to another in the image. important!! don't skip it πŸ’…
  • endian tells you whether the pixel color in the image needs to be stored in little endian (endian == 0), or big endian (endian == 1). yes, wether β€” the original said it like that, we keep it, it's iconic.

mlx_get_data_addr returns a char * address that represents the begining of the memory area where the image is stored. from this adress:

  • the first bits_per_pixel bits = the color of the first pixel in the first line of the image
  • the second group of bits_per_pixel bits = the second pixel of the first line
  • and so on, forever, that's the whole system ⭐

add size_line to the adress to get the begining of the second line. you can reach any pixels of the image that way. it's giving pointer math but make it cute πŸŽ€

πŸ’€ bye bye image: mlx_destroy_image()

mlx_destroy_image destroys the given image (img_ptr). gone. poof. ✨


🎨 STORING COLOR INSIDE IMAGES 🎨

okay so depending on the display, the number of bits used to store a pixel color can change. the user usually represents a color in RGB mode, using one byte for each component (check the mlx_pixel_put manual for that). this must be translated to fit the bits_per_pixel requirement of the image, and to make the color understandable to the X-Server. that is literally the whole purpose of the mlx_get_color_value () function 🫑

it takes a standard RGB color parameter, and returns an unsigned int value. the bits_per_pixel least significant bits of this value can be stored in the image. periodt.

⚠️ bestie, keep in mind that the least significant bits position depends on the local computer's endian. if the endian of the image (in fact the endian of the X-Server's computer) differs from the local endian, then the value should be transformed before being used. don't skip this, it will bite you 😭

🌸 XPM IMAGES 🌸

the mlx_xpm_to_image () and mlx_xpm_file_to_image () functions will create a new image the same way. they fill it using the specified xpm_data or filename, depending on which function is used. cute!

⚠️ note that MiniLibX does not use the standard Xpm library to deal with xpm images. you may not be able to read all types of xpm images. but bestie β€” it however handles transparency πŸ’…βœ¨

⭐ RETURN VALUES ⭐

the three functions that create images β€” mlx_new_image(), mlx_xpm_to_image() and mlx_xpm_file_to_image() β€” will return NULL if an error occurs. otherwise they return a non-null pointer as an image identifier. no in-between, it's giving all or nothing πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘

mlx(3), mlx_new_window(3), mlx_pixel_put(3), mlx_loop(3)

go read those next, bestie, they're the supporting cast πŸ¦‹


πŸ’œ AUTHOR πŸ’œ

Copyright ol@ - 2002-2014 - Olivier Crouzet


and that's the page! this lil manual dropped September 19, 2002 and it's still out here as MiniLibX(3) in the Library Functions Manual β€” a true icon, we stan πŸ«ΆπŸŽ€πŸ’–

πŸ“¦ where this page comes from