00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <opencv/cv.h>
00037 #include <opencv/highgui.h>
00038 #include <photo/photo_image.h>
00039
00040 photo_image_p photo_image_initialize()
00041 {
00042 photo_image_p image;
00043 image = (photo_image_p)calloc(1, sizeof(photo_image_t));
00044
00045 image->width = 0;
00046 image->height = 0;
00047 image->bytes_per_pixel = 3;
00048 image->image_size = 0;
00049 image->data = NULL;
00050
00051 return image;
00052 }
00053
00054
00055 void photo_image_free(photo_image_p image)
00056 {
00057 if(image->data)
00058 free(image->data);
00059 free(image);
00060 }
00061
00062 void photo_image_set_size(photo_image_p image, int image_width, int image_height)
00063 {
00064 if(image->data)
00065 free(image->data);
00066
00067 image->width = image_width;
00068 image->height = image_height;
00069 image->image_size = image->width*image->height*image->bytes_per_pixel;
00070 image->data = (char *) calloc(image->image_size, sizeof(char));
00071 memset(image->data, 0, image->image_size*sizeof(char));
00072 }
00073
00074 int photo_image_read(photo_image_p image, const char *filename)
00075 {
00076 int r,c,n=0;
00077 IplImage* img = cvLoadImage(filename,1);
00078 if(!img) return 0;
00079 int w = img->width;
00080 int h = img->height;
00081 if(image->width!=w || image->height!=h)
00082 photo_image_set_size(image, w, h);
00083 char* data = image->data;
00084 for(r=0;r<h;++r) {
00085 for(c=0;c<w;++c) {
00086 data[n] = ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 2]; ++n;
00087 data[n] = ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 1]; ++n;
00088 data[n] = ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 0]; ++n;
00089 }
00090 }
00091 cvReleaseImage(&img);
00092 return 1;
00093 }
00094
00095 int photo_image_write(photo_image_p image, const char *filename)
00096 {
00097 int res, r,c,n=0;
00098 int w = image->width;
00099 int h = image->height;
00100
00101 CvSize size = {w,h};
00102 IplImage* img = cvCreateImage(size,IPL_DEPTH_8U,3);
00103 char* data = image->data;
00104 for(r=0;r<h;++r) {
00105 for(c=0;c<w;++c) {
00106 ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 2] = data[n]; n++;
00107 ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 1] = data[n]; n++;
00108 ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 0] = data[n]; n++;
00109 }
00110 }
00111 res = cvSaveImage(filename, img);
00112 cvReleaseImage(&img);
00113 return 1;
00114 }
00115