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