1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/pt_image.c Sat Aug 01 12:42:34 2009 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +#include <stdio.h> 1.5 +#include <stdlib.h> 1.6 +#include <string.h> 1.7 +#include "pt_image.h" 1.8 + 1.9 +/** 1.10 + * Create a new pt_Image object. 1.11 + * 1.12 + * Creates a new pt_Image of a specified width and height, and initialises 1.13 + * all the pixels in its buffer to zero. 1.14 + * 1.15 + * @param width The width of the image. 1.16 + * @param height The height of the image. 1.17 + * @return The new pt_Image, or NULL on failure. 1.18 + */ 1.19 +pt_Image *ptimage_Create(unsigned long width, unsigned long height) 1.20 +{ 1.21 + pt_Image *image; 1.22 + 1.23 +#ifdef DEBUG 1.24 + printf("%s[%d]: image size = %u\n", __FILE__, __LINE__, sizeof(pt_Image)); 1.25 + printf("%s[%d]: data size = %u\n", __FILE__, __LINE__, sizeof(image->data[0])); 1.26 + printf("%s[%d]: create %lu x %lu image\n", __FILE__, __LINE__, width, height); 1.27 +#endif 1.28 + 1.29 + // Allocate memory for the new image and its data buffer 1.30 + image = malloc(sizeof(pt_Image)); 1.31 + if (image == NULL) return NULL; 1.32 + 1.33 + image->data = malloc(width * height * sizeof(image->data[0])); 1.34 + if (image->data == NULL) return NULL; 1.35 + 1.36 + // Set the image's parameters 1.37 + image->width = width; 1.38 + image->height = height; 1.39 + 1.40 + // Clear the image's data buffer to "blank" 1.41 + memset(image->data, 0, width * height * sizeof(image->data[0])); 1.42 + 1.43 + return image; 1.44 +} 1.45 + 1.46 +/** 1.47 + * Free a pt_Image object. 1.48 + * 1.49 + * @param image The image object to free. 1.50 + */ 1.51 +void ptimage_Free(pt_Image *image) 1.52 +{ 1.53 + // Make sure image is non-null 1.54 + if (image == NULL) { 1.55 + return; 1.56 + } 1.57 + 1.58 + // Free the image data 1.59 + if (image->data != NULL) { 1.60 + free(image->data); 1.61 + } 1.62 + 1.63 + // Free the image 1.64 + free(image); 1.65 +} 1.66 + 1.67 +/** 1.68 + * Get the value of a pixel in a pt_Image object. 1.69 + * 1.70 + * @param image Image object. 1.71 + * @param x X position of the pixel, zero-based. 1.72 + * @param y Y position of the pixel, zero-based. 1.73 + * @return Value of the pixel, or negative on error. 1.74 + */ 1.75 +int ptimage_GetPixel(pt_Image *image, unsigned long x, unsigned long y) 1.76 +{ 1.77 + // Make sure the image is not null and that the data buffer has been 1.78 + // allocated 1.79 + if (image == NULL) { 1.80 + return -1; // TODO: make constant 1.81 + } 1.82 + 1.83 + if (image->data == NULL) { 1.84 + return -1; // TODO: make constant 1.85 + } 1.86 + 1.87 + // Range-check 1.88 + if ((x < 0) || (x > image->width)) { 1.89 + return -2; // TODO: make constant 1.90 + } 1.91 + if ((y < 0) || (y > image->height)) { 1.92 + return -3; // TODO: make constant 1.93 + } 1.94 + 1.95 + // Return the pixel value 1.96 + return image->data[(y*image->width)+x]; 1.97 +} 1.98 + 1.99 +/** 1.100 + * Set the value of a pixel in a pt_Image object. 1.101 + * 1.102 + * @param image Image object. 1.103 + * @param x X position of the pixel, zero-based. 1.104 + * @param y Y position of the pixel, zero-based. 1.105 + * @param val New value of the pixel. 1.106 + * @return Zero on success, or negative on error. 1.107 + */ 1.108 +int ptimage_SetPixel(pt_Image *image, unsigned long x, unsigned long y, unsigned char val) 1.109 +{ 1.110 + // Make sure the image is not null and that the data buffer has been 1.111 + // allocated 1.112 + if (image == NULL) { 1.113 + return -1; // TODO: make constant 1.114 + } 1.115 + 1.116 + if (image->data == NULL) { 1.117 + return -1; // TODO: make constant 1.118 + } 1.119 + 1.120 + // Range-check 1.121 + if ((x < 0) || (x > image->width)) { 1.122 + return -2; // TODO: make constant 1.123 + } 1.124 + if ((y < 0) || (y > image->height)) { 1.125 + return -3; // TODO: make constant 1.126 + } 1.127 + 1.128 + // Set the pixel value 1.129 + image->data[(y*image->width)+x] = val; 1.130 + 1.131 + return 0; 1.132 +} 1.133 +