$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2009, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <opencv/cv.h> 00037 #include <opencv/highgui.h> //for cvLoadImage() 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 /* frees a photo image */ 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; // R 00087 data[n] = ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 1]; ++n; // G 00088 data[n] = ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 0]; ++n; // B 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++; // R 00107 ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 1] = data[n]; n++; // G 00108 ((unsigned char *)(img->imageData + r*img->widthStep))[c*img->nChannels + 0] = data[n]; n++; // B 00109 } 00110 } 00111 res = cvSaveImage(filename, img); 00112 cvReleaseImage(&img); 00113 return 1; 00114 } 00115