|
- #include "texture_private.h"
-
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include "sww/color.h"
-
- struct swwTexture {
- swwImage *image;
- };
-
- swwTexture *swwTexture_LoadFromFile(const char *filepath) {
- swwTexture *o = (swwTexture *)malloc(sizeof(swwTexture));
- o->image = swwImage_Load(filepath);
- return o;
- }
-
- /* channels: 1, 3, 4 is supported */
- swwTexture *swwTexture_LoadFromMemory(uint8_t *bitmap, int channels,
- uint32_t width, uint32_t height) {
- swwTexture *o = (swwTexture *)malloc(sizeof(swwTexture));
- o->image = swwImage_Create(width, height, 4);
-
- /* expand any channels to 4 channels */
- switch (channels) {
- case 1: {
- uint32_t i = 0;
- uint32_t j = 0;
- uint32_t idx = 0;
- uint32_t *p = NULL;
- for (j = 0; j < height; j++) {
- for (i = 0; i < width; i++, idx++) {
- p = (uint32_t *)o->image->buffer + idx;
- *p = Color_Gray(bitmap[idx]);
- }
- }
- } break;
- case 3: {
- uint32_t i = 0;
- uint32_t j = 0;
- uint32_t idx = 0;
- uint32_t *p = NULL;
- uint8_t *b = NULL;
- for (j = 0; j < height; j++) {
- for (i = 0; i < width; i++, idx++) {
- idx = j * width + i;
- p = (uint32_t *)o->image->buffer + idx;
- b = (uint8_t *)bitmap + idx * 3;
- *p = Color_RGB(b[0], b[1], b[2]);
- }
- }
- } break;
- case 4:
- default:
- memcpy(o->image->buffer, bitmap, width * height * 4);
- break;
- }
-
- return o;
- }
-
- void swwTexture_Destroy(swwTexture *o) {
- swwImage_Destroy(o->image);
- free(o);
- }
-
- void swwTexture_GetSize(swwTexture *o, uint32_t *width, uint32_t *height) {
- if (width != NULL) {
- *width = o->image->width;
- }
-
- if (height != NULL) {
- *height = o->image->height;
- }
- }
-
- uint32_t swwTexture_Get(swwTexture *o, uint32_t x, uint32_t y) {
- if (y >= o->image->height || x >= o->image->width) {
- return 0;
- }
- return swwImage_GetPixel(o->image, x, y);
- }
-
- void swwTexture_Set(swwTexture *o, uint32_t x, uint32_t y, uint32_t color) {
- if (y >= o->image->height || x >= o->image->width) {
- return;
- }
- swwImage_SetPixel(o->image, x, y, color);
- }
-
- swwTexture *swwTexture_ZoomTo(swwTexture *o, float zoom, int mode) {
- return NULL;
- }
-
- swwTexture *swwTexture_Area(swwTexture *o, int x, int y, int width,
- int height) {
- return NULL;
- }
-
- swwImage *swwTexture_GetImage(swwTexture *o) { return o->image; }
|