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