Sat, 01 Aug 2009 12:44:09 +0100
update makefile
philpem@3 | 1 | #include <stdio.h> |
philpem@3 | 2 | #include <stdlib.h> |
philpem@3 | 3 | #include <string.h> |
philpem@3 | 4 | #include "pt_image.h" |
philpem@3 | 5 | |
philpem@3 | 6 | /** |
philpem@3 | 7 | * Create a new pt_Image object. |
philpem@3 | 8 | * |
philpem@3 | 9 | * Creates a new pt_Image of a specified width and height, and initialises |
philpem@3 | 10 | * all the pixels in its buffer to zero. |
philpem@3 | 11 | * |
philpem@3 | 12 | * @param width The width of the image. |
philpem@3 | 13 | * @param height The height of the image. |
philpem@3 | 14 | * @return The new pt_Image, or NULL on failure. |
philpem@3 | 15 | */ |
philpem@3 | 16 | pt_Image *ptimage_Create(unsigned long width, unsigned long height) |
philpem@3 | 17 | { |
philpem@3 | 18 | pt_Image *image; |
philpem@3 | 19 | |
philpem@3 | 20 | #ifdef DEBUG |
philpem@3 | 21 | printf("%s[%d]: image size = %u\n", __FILE__, __LINE__, sizeof(pt_Image)); |
philpem@3 | 22 | printf("%s[%d]: data size = %u\n", __FILE__, __LINE__, sizeof(image->data[0])); |
philpem@3 | 23 | printf("%s[%d]: create %lu x %lu image\n", __FILE__, __LINE__, width, height); |
philpem@3 | 24 | #endif |
philpem@3 | 25 | |
philpem@3 | 26 | // Allocate memory for the new image and its data buffer |
philpem@3 | 27 | image = malloc(sizeof(pt_Image)); |
philpem@3 | 28 | if (image == NULL) return NULL; |
philpem@3 | 29 | |
philpem@3 | 30 | image->data = malloc(width * height * sizeof(image->data[0])); |
philpem@3 | 31 | if (image->data == NULL) return NULL; |
philpem@3 | 32 | |
philpem@3 | 33 | // Set the image's parameters |
philpem@3 | 34 | image->width = width; |
philpem@3 | 35 | image->height = height; |
philpem@3 | 36 | |
philpem@3 | 37 | // Clear the image's data buffer to "blank" |
philpem@3 | 38 | memset(image->data, 0, width * height * sizeof(image->data[0])); |
philpem@3 | 39 | |
philpem@3 | 40 | return image; |
philpem@3 | 41 | } |
philpem@3 | 42 | |
philpem@3 | 43 | /** |
philpem@3 | 44 | * Free a pt_Image object. |
philpem@3 | 45 | * |
philpem@3 | 46 | * @param image The image object to free. |
philpem@3 | 47 | */ |
philpem@3 | 48 | void ptimage_Free(pt_Image *image) |
philpem@3 | 49 | { |
philpem@3 | 50 | // Make sure image is non-null |
philpem@3 | 51 | if (image == NULL) { |
philpem@3 | 52 | return; |
philpem@3 | 53 | } |
philpem@3 | 54 | |
philpem@3 | 55 | // Free the image data |
philpem@3 | 56 | if (image->data != NULL) { |
philpem@3 | 57 | free(image->data); |
philpem@3 | 58 | } |
philpem@3 | 59 | |
philpem@3 | 60 | // Free the image |
philpem@3 | 61 | free(image); |
philpem@3 | 62 | } |
philpem@3 | 63 | |
philpem@3 | 64 | /** |
philpem@3 | 65 | * Get the value of a pixel in a pt_Image object. |
philpem@3 | 66 | * |
philpem@3 | 67 | * @param image Image object. |
philpem@3 | 68 | * @param x X position of the pixel, zero-based. |
philpem@3 | 69 | * @param y Y position of the pixel, zero-based. |
philpem@3 | 70 | * @return Value of the pixel, or negative on error. |
philpem@3 | 71 | */ |
philpem@3 | 72 | int ptimage_GetPixel(pt_Image *image, unsigned long x, unsigned long y) |
philpem@3 | 73 | { |
philpem@3 | 74 | // Make sure the image is not null and that the data buffer has been |
philpem@3 | 75 | // allocated |
philpem@3 | 76 | if (image == NULL) { |
philpem@3 | 77 | return -1; // TODO: make constant |
philpem@3 | 78 | } |
philpem@3 | 79 | |
philpem@3 | 80 | if (image->data == NULL) { |
philpem@3 | 81 | return -1; // TODO: make constant |
philpem@3 | 82 | } |
philpem@3 | 83 | |
philpem@3 | 84 | // Range-check |
philpem@3 | 85 | if ((x < 0) || (x > image->width)) { |
philpem@3 | 86 | return -2; // TODO: make constant |
philpem@3 | 87 | } |
philpem@3 | 88 | if ((y < 0) || (y > image->height)) { |
philpem@3 | 89 | return -3; // TODO: make constant |
philpem@3 | 90 | } |
philpem@3 | 91 | |
philpem@3 | 92 | // Return the pixel value |
philpem@3 | 93 | return image->data[(y*image->width)+x]; |
philpem@3 | 94 | } |
philpem@3 | 95 | |
philpem@3 | 96 | /** |
philpem@3 | 97 | * Set the value of a pixel in a pt_Image object. |
philpem@3 | 98 | * |
philpem@3 | 99 | * @param image Image object. |
philpem@3 | 100 | * @param x X position of the pixel, zero-based. |
philpem@3 | 101 | * @param y Y position of the pixel, zero-based. |
philpem@3 | 102 | * @param val New value of the pixel. |
philpem@3 | 103 | * @return Zero on success, or negative on error. |
philpem@3 | 104 | */ |
philpem@3 | 105 | int ptimage_SetPixel(pt_Image *image, unsigned long x, unsigned long y, unsigned char val) |
philpem@3 | 106 | { |
philpem@3 | 107 | // Make sure the image is not null and that the data buffer has been |
philpem@3 | 108 | // allocated |
philpem@3 | 109 | if (image == NULL) { |
philpem@3 | 110 | return -1; // TODO: make constant |
philpem@3 | 111 | } |
philpem@3 | 112 | |
philpem@3 | 113 | if (image->data == NULL) { |
philpem@3 | 114 | return -1; // TODO: make constant |
philpem@3 | 115 | } |
philpem@3 | 116 | |
philpem@3 | 117 | // Range-check |
philpem@3 | 118 | if ((x < 0) || (x > image->width)) { |
philpem@3 | 119 | return -2; // TODO: make constant |
philpem@3 | 120 | } |
philpem@3 | 121 | if ((y < 0) || (y > image->height)) { |
philpem@3 | 122 | return -3; // TODO: make constant |
philpem@3 | 123 | } |
philpem@3 | 124 | |
philpem@3 | 125 | // Set the pixel value |
philpem@3 | 126 | image->data[(y*image->width)+x] = val; |
philpem@3 | 127 | |
philpem@3 | 128 | return 0; |
philpem@3 | 129 | } |
philpem@3 | 130 |