1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 ///////////////////////////////////////////////////
18 // ImageUtils.h
19 // $Id: ImageUtils.h,v 1.9 2011/05/16 15:33:06 mbansal Exp $
20 
21 #ifndef IMAGE_UTILS_H
22 #define IMAGE_UTILS_H
23 
24 #include <stdlib.h>
25 
26 /**
27  *  Definition of basic image types
28  */
29 typedef unsigned char ImageTypeBase;
30 typedef ImageTypeBase *ImageType;
31 
32 typedef short ImageTypeShortBase;
33 typedef ImageTypeShortBase *ImageTypeShort;
34 
35 typedef float ImageTypeFloatBase;
36 typedef ImageTypeFloatBase *ImageTypeFloat;
37 
38 
39 class ImageUtils {
40 public:
41 
42   /**
43    *  Default number of channels in image.
44    */
45   static const int IMAGE_TYPE_NUM_CHANNELS = 3;
46 
47   /**
48    *  Definition of an empty image.
49    */
50   static const int IMAGE_TYPE_NOIMAGE = NULL;
51 
52   /**
53    *  Convert image from BGR (interlaced) to YVU (non-interlaced)
54    *
55    *  Arguments:
56    *    out: Resulting image (note must be preallocated before
57    *    call)
58    *    in: Input image
59    *    width: Width of input image
60    *    height: Height of input image
61    */
62   static void rgb2yvu(ImageType out, ImageType in, int width, int height);
63 
64   static void rgba2yvu(ImageType out, ImageType in, int width, int height);
65 
66   /**
67    *  Convert image from YVU (non-interlaced) to BGR (interlaced)
68    *
69    *  Arguments:
70    *    out: Resulting image (note must be preallocated before
71    *    call)
72    *    in: Input image
73    *    width: Width of input image
74    *    height: Height of input image
75    */
76   static void yvu2rgb(ImageType out, ImageType in, int width, int height);
77   static void yvu2bgr(ImageType out, ImageType in, int width, int height);
78 
79   /**
80    *  Convert image from BGR to grayscale
81    *
82    *  Arguments:
83    *    in: Input image
84    *    width: Width of input image
85    *    height: Height of input image
86    *
87    *  Return:
88    *    Pointer to resulting image (allocation is done here, free
89    *    must be done by caller)
90    */
91   static ImageType rgb2gray(ImageType in, int width, int height);
92   static ImageType rgb2gray(ImageType out, ImageType in, int width, int height);
93 
94   /**
95    *  Read a binary PPM image
96    */
97   static ImageType readBinaryPPM(const char *filename, int &width, int &height);
98 
99   /**
100    *  Write a binary PPM image
101    */
102   static void writeBinaryPPM(ImageType image, const char *filename, int width, int height, int numChannels = IMAGE_TYPE_NUM_CHANNELS);
103 
104   /**
105    *  Allocate space for a standard image.
106    */
107   static ImageType allocateImage(int width, int height, int numChannels, short int border = 0);
108 
109   /**
110    *  Free memory of image
111    */
112   static void freeImage(ImageType image);
113 
114   static ImageType *imageTypeToRowPointers(ImageType out, int width, int height);
115   /**
116    *  Get time.
117    */
118   static double getTime();
119 
120 protected:
121 
122   /**
123   *  Constants for YVU/RGB conversion
124   */
125   static const int REDY = 257;
126   static const int REDV = 439;
127   static const int REDU = 148;
128   static const int GREENY = 504;
129   static const int GREENV = 368;
130   static const int GREENU = 291;
131   static const int BLUEY = 98;
132   static const int BLUEV = 71;
133   static const int BLUEU = 439;
134 
135 };
136 
137 /**
138  * Structure containing an image and other bookkeeping items.
139  * Used in YUVinfo to store separate YVU image planes.
140  */
141 typedef struct {
142   ImageType *ptr;
143   unsigned short width;
144   unsigned short height;
145   unsigned short border;
146   unsigned short pitch;
147 } BimageInfo;
148 
149 /**
150  * A YUV image container,
151  */
152 class YUVinfo {
153 public:
154   static YUVinfo *allocateImage(unsigned short width, unsigned short height);
155   static void mapYUVInfoToImage(YUVinfo *img, unsigned char *position);
156 
157   /**
158   * Y Plane
159   */
160   BimageInfo Y;
161 
162   /**
163   *  V (1st color) plane
164   */
165   BimageInfo V;
166 
167   /**
168   *  U (1st color) plane
169   */
170   BimageInfo U;
171 };
172 
173 #endif
174