Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ImageDefinitions.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027     #include <phissionconfig.h>
00028 #endif
00029 
00030 #include <phStandard.h>
00031 
00032 #define PH_USE_BYTEORDERPRIVATE 1
00033 #include <phByteOrderPrivate.h>
00034 #include <ImageDefinitions.h>
00035 
00036 #include <errno.h>
00037 #if USE_FILETYPE
00038 #else
00039     //#include <sys/file.h>
00040     #include <sys/types.h>
00041     #include <sys/stat.h>
00042     #include <fcntl.h>
00043     //#include <unistd.h>
00044 #endif
00045 #include <phError.h>
00046 #include <phFile.h>
00047 #include <phMemory.h>
00048 #include <phPrint.h>
00049 
00050 #ifdef __cplusplus
00051 extern "C" 
00052 {
00053 #endif
00054 
00055 /* ---------------------------------------------------------------------- */
00056 char *phColorNames[phColorCount] = {
00057         "phColorTypeArray",
00058         "phColorTypeRGB24",
00059         "phColorTypeBGR24",
00060         "phColorTypeABGR32",
00061         "phColorTypeRGBA32",
00062         "phColorTypeGREY8",
00063         "phColorTypeYUV9",
00064         "phColorTypeHSV24",
00065         "phColorTypeBGRA32",
00066         "phColorTypeSCT24"
00067 };
00068 uint8_t phColorDepth[phColorCount] = { 4,3,3,4,4,1,1,3,4,3 };
00069 
00070 #if phBYTE_ORDER == phLITTLE_ENDIAN
00071 int8_t phImageChannelIndex[phImageFormatCount][4] = {
00072         /*R G  B  A */
00073         {-1,-1,-1,-1 }, /* Invalid */
00074         { 0, 1, 2,-1 }, /* RGB24   */
00075         { 2, 1, 0,-1 }, /* BGR24   */
00076         { 3, 2, 1, 0 }, /* ABGR32  */
00077         { 0, 1, 2, 3 }, /* RGBA32  */
00078         { 0, 0, 0, 0 }, /* GREY8   */
00079         { 0, 0, 0, 0 }, /* YUV9    */
00080         { 0, 1, 2,-1 }, /* HSV24   */
00081         { 2, 1, 0, 3 }, /* BGRA32  */
00082         { 0, 1, 2,-1 }  /* SCT24   */
00083 };
00084 uint32_t phImageChannelMasks[phImageFormatCount][4] = {
00085         /*    R            G           B           A                   */
00086         { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }, /* Invalid */
00087         { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, /* RGB24   */
00088         { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, /* BGR24   */
00089         { 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000 }, /* ABGR32  */
00090         { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, /* RGBA32  */
00091         { 0x000000ff, 0x000000ff, 0x000000ff, 0x00000000 }, /* GREY8   */
00092         { 0x000000ff, 0x000000ff, 0x000000ff, 0x00000000 }, /* YUV9    */
00093         { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, /* HSV24   */
00094         { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, /* BGRA32  */
00095         { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }  /* SCT24   */
00096 };
00097 #elif phBYTE_ORDER == phBIG_ENDIAN
00098 int8_t phImageChannelIndex[phImageFormatCount][4] = {
00099         /*R G  B  A */
00100         {-1,-1,-1,-1 }, /* Invalid */
00101         { 2, 1, 0,-1 }, /* RGB24   */
00102         { 0, 1, 2,-1 }, /* BGR24   */
00103         { 0, 1, 2, 3 }, /* ABGR32  */
00104         { 3, 2, 1, 0 }, /* RGBA32  */
00105         { 0, 0, 0, 0 }, /* GREY8   */
00106         { 0, 0, 0, 0 }, /* YUV9    */
00107         { 2, 1, 0,-1 }, /* HSV24   */
00108         { 1, 2, 3, 0 }, /* BGRA32  */
00109         { 2, 1, 0,-1 }  /* SCT24   */
00110 };
00111 uint32_t phImageChannelMasks[phImageFormatCount][4] = {
00112         /*    R            G           B           A                   */
00113         { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }, /* Invalid */
00114         { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, /* RGB24   */
00115         { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, /* BGR24   */
00116         { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, /* ABGR32  */
00117         { 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000 }, /* RGBA32  */
00118         { 0xff000000, 0xff000000, 0xff000000, 0x00000000 }, /* GREY8   */
00119         { 0xff000000, 0xff000000, 0xff000000, 0x00000000 }, /* YUV9    */
00120         { 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000 }, /* HSV24   */
00121         { 0x0000ff00, 0x00ff0000, 0xff000000, 0x00000000 }, /* BGRA32  */
00122         { 0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000 }  /* SCT24   */
00123 };
00124 
00125 #else
00126 #error "Phission doesn't know what endianness this machine is."
00127 #endif
00128 
00129 char *phImageNames[phImageFormatTotalCount] = {
00130         "ImageInvalid",
00131         "phImageRGB24",
00132         "phImageBGR24",
00133         "phImageABGR32",
00134         "phImageRGBA32",
00135         "phImageGREY8",
00136         "phImageYUV9",
00137         "phImageHSV24",
00138         "phImageBGRA32",
00139         "phImageSCT24",
00140         
00141         /* Zlib compression alone */
00142         "phImageZlib::ImageInvalid",
00143         "phImageZlib::phImageRGB24",
00144         "phImageZlib::phImageBGR24",
00145         "phImageZlib::phImageABGR32",
00146         "phImageZlib::phImageRGBA32",
00147         "phImageZlib::phImageGREY8",
00148         "phImageZlib::phImageYUV9",
00149         "phImageZlib::phImageHSV24",
00150         "phImageZlib::phImageBGRA32",
00151         "phImageZlib::phImageSCT24",
00152         
00153         /* JPEG compression alone */
00154         "phImageJPEG::ImageInvalid",
00155         "phImageJPEG::phImageRGB24",
00156         "phImageJPEG::phImageBGR24", /* Should never happen, at least not yet*/
00157         "phImageJPEG::phImageABGR32",/* ditto */
00158         "phImageJPEG::phImageRGBA32",/* ditto */
00159         "phImageJPEG::phImageGREY8",
00160         "phImageJPEG::phImageYUV9",  /* ditto */
00161         "phImageJPEG::phImageHSV24", /* ditto */
00162         "phImageJPEG::phImageBGRA32",/* ditto */
00163         "phImageJPEG::phImageSCT24", /* ditto */
00164 
00165         /* Zlib and JPEG compression alone */
00166         "phImageZlib::phImageJPEG::ImageInvalid",
00167         "phImageZlib::phImageJPEG::phImageRGB24",
00168         "phImageZlib::phImageJPEG::phImageBGR24", /* Should never happen, at least not yet*/
00169         "phImageZlib::phImageJPEG::phImageABGR32",/* ditto */
00170         "phImageZlib::phImageJPEG::phImageRGBA32",/* ditto */
00171         "phImageZlib::phImageJPEG::phImageGREY8",
00172         "phImageZlib::phImageJPEG::phImageYUV9",  /* ditto */
00173         "phImageZlib::phImageJPEG::phImageHSV24", /* ditto */
00174         "phImageZlib::phImageJPEG::phImageBGRA32",/* ditto */
00175         "phImageZlib::phImageJPEG::phImageSCT24"  /* ditto */
00176 };
00177 
00178 uint8_t phImageDepth[phImageFormatTotalCount] = 
00179 {
00180      /* no compression */
00181      0,3,3,4,4,1,1,3,4,3,
00182      /* ZlibCompress */
00183      1,1,1,1,1,1,1,1,1,1,
00184      /* JPEGCompress */
00185      1,1,1,1,1,1,1,1,1,1,
00186      /* Zlib & JPEGCompress */
00187      1,1,1,1,1,1,1,1,1,1
00188 };
00189 
00190 
00191 
00192 /* ---------------------------------------------------------------------- */
00193 phColor phColor_new (uint8_t t)
00194 {
00195     phColor newColor;
00196     uint8_t type = phColorTypeArray;
00197     if (t < phColorCount) type = t;
00198     
00199     newColor.type = t;
00200     newColor.array.v[0] = 
00201     newColor.array.v[1] = 
00202     newColor.array.v[2] = 
00203     newColor.array.v[3] = 
00204     newColor.bytes[5] = 
00205     newColor.bytes[6] = 
00206     newColor.bytes[7] = 0;
00207     
00208     return newColor;
00209 }
00210 /* ---------------------------------------------------------------------- */
00211 phColor phColor8_new (uint8_t t,uint8_t b)
00212 {
00213     phColor newColor;
00214     uint8_t type = phColorTypeArray;
00215     if (t < phColorCount) type = t;
00216     
00217     newColor.type = t;
00218     newColor.array.v[0] = b;
00219     newColor.array.v[1] = 
00220     newColor.array.v[2] = 
00221     newColor.array.v[3] = 
00222     newColor.bytes[5] = 
00223     newColor.bytes[6] = 
00224     newColor.bytes[7] = 0;
00225     
00226     return newColor;
00227 }
00228 /* ---------------------------------------------------------------------- */
00229 phColor phColor16_new (uint8_t t,uint8_t b0,uint8_t b1 )
00230 {
00231     phColor newColor;
00232     uint8_t type = phColorTypeArray;
00233     if (t < phColorCount) type = t;
00234     
00235     newColor.type = t;
00236     newColor.array.v[0] = b0;
00237     newColor.array.v[1] = b1;
00238     newColor.array.v[2] = 
00239     newColor.array.v[3] = 
00240     newColor.bytes[5] = 
00241     newColor.bytes[6] = 
00242     newColor.bytes[7] = 0;
00243     
00244     return newColor;
00245 }
00246 /* ---------------------------------------------------------------------- */
00247 phColor phColor24_new (uint8_t t,uint8_t b0,uint8_t b1,uint8_t b2)
00248 {
00249     phColor newColor;
00250     uint8_t type = phColorTypeArray;
00251     if (t < phColorCount) type = t;
00252     
00253     newColor.type = t;
00254     newColor.array.v[0] = b0;
00255     newColor.array.v[1] = b1;
00256     newColor.array.v[2] = b2;
00257     newColor.array.v[3] =
00258     newColor.bytes[5] = 
00259     newColor.bytes[6] = 
00260     newColor.bytes[7] = 0;
00261     
00262     return newColor;
00263 }
00264 /* ---------------------------------------------------------------------- */
00265 phColor phColor32_new (uint8_t t,uint8_t b0,uint8_t b1,
00266                                  uint8_t b2,uint8_t b3)
00267 {
00268     phColor newColor;
00269  
00270     newColor.type = phColorTypeArray;
00271     newColor.array.v[0] = b0;
00272     newColor.array.v[1] = b1;
00273     newColor.array.v[2] = b2;
00274     newColor.array.v[3] = b3;
00275     newColor.bytes[5] = 
00276     newColor.bytes[6] = 
00277     newColor.bytes[7] = 0;
00278     
00279     return newColor;
00280 }
00281 
00282 /* ---------------------------------------------------------------------- */
00283 phColor phColorArray24_new (uint8_t b0,uint8_t b1,uint8_t b2)
00284 {
00285     phColor newColor;
00286     
00287     newColor.type = phColorTypeArray;
00288     newColor.array.v[0] = b0;
00289     newColor.array.v[1] = b1;
00290     newColor.array.v[2] = b2;
00291     newColor.array.v[3] = 
00292     newColor.bytes[5] = 
00293     newColor.bytes[6] = 
00294     newColor.bytes[7] = 0;
00295     
00296     return newColor;
00297 }
00298 /* ---------------------------------------------------------------------- */
00299 phColor phColorArray32_new (uint8_t b0,uint8_t b1,uint8_t b2,uint8_t b3)
00300 {
00301     phColor newColor;
00302  
00303     newColor.type = phColorTypeArray;
00304     newColor.array.v[0] = b0;
00305     newColor.array.v[1] = b1;
00306     newColor.array.v[2] = b2;
00307     newColor.array.v[3] = b3;
00308     newColor.bytes[5] = 
00309     newColor.bytes[6] = 
00310     newColor.bytes[7] = 0;
00311     
00312     return newColor;
00313 }
00314 /* ---------------------------------------------------------------------- */
00315 phColor phColorRGB24_new (uint8_t r,uint8_t g,uint8_t b)
00316 {
00317     phColor newColor;
00318     
00319     newColor.type = phColorTypeRGB24;
00320     newColor.rgb24.r = r;
00321     newColor.rgb24.g = g;
00322     newColor.rgb24.b = b;
00323     newColor.bytes[4] = 
00324     newColor.bytes[5] = 
00325     newColor.bytes[6] = 
00326     newColor.bytes[7] = 0;
00327     
00328     return newColor;
00329 }
00330 /* ---------------------------------------------------------------------- */
00331 phColor phColorRGBA32_new(uint8_t r,uint8_t g,uint8_t b,uint8_t a)
00332 {
00333     phColor newColor;
00334     
00335     newColor.type = phColorTypeRGBA32;
00336     newColor.rgba32.r = r;
00337     newColor.rgba32.g = g;
00338     newColor.rgba32.b = b;
00339     newColor.rgba32.a = a;
00340     newColor.bytes[5] = 
00341     newColor.bytes[6] = 
00342     newColor.bytes[7] = 0;
00343     
00344     return newColor;
00345 }
00346 /* ---------------------------------------------------------------------- */
00347 phColor phColorBGR24_new(uint8_t b,uint8_t g,uint8_t r)
00348 {
00349     phColor newColor;
00350     
00351     newColor.type = phColorTypeBGR24;
00352     newColor.bgr24.r = r;
00353     newColor.bgr24.g = g;
00354     newColor.bgr24.b = b;
00355     newColor.bytes[4] = 
00356     newColor.bytes[5] = 
00357     newColor.bytes[6] = 
00358     newColor.bytes[7] = 0;
00359     
00360     return newColor;
00361 }
00362 /* ---------------------------------------------------------------------- */
00363 phColor phColorABGR32_new(uint8_t a,uint8_t b,uint8_t g,uint8_t r)
00364 {
00365     phColor newColor;
00366     
00367     newColor.type = phColorTypeABGR32;
00368     newColor.abgr32.r = r;
00369     newColor.abgr32.g = g;
00370     newColor.abgr32.b = b;
00371     newColor.abgr32.a = a;
00372     newColor.bytes[5] = 
00373     newColor.bytes[6] = 
00374     newColor.bytes[7] = 0;
00375     
00376     return newColor;
00377 }
00378 /* ---------------------------------------------------------------------- */
00379 phColor phColorBGRA32_new(uint8_t b,uint8_t g,uint8_t r,uint8_t a)
00380 {
00381     phColor newColor;
00382     
00383     newColor.type = phColorTypeBGRA32;
00384     newColor.bgra32.r = r;
00385     newColor.bgra32.g = g;
00386     newColor.bgra32.b = b;
00387     newColor.bgra32.a = a;
00388     newColor.bytes[5] = 
00389     newColor.bytes[6] = 
00390     newColor.bytes[7] = 0;
00391     
00392     return newColor;
00393 }
00394 /* ---------------------------------------------------------------------- */
00395 phColor phColorYUV9_new(uint8_t y,uint8_t u,uint8_t v)
00396 {
00397     phColor newColor;
00398     
00399     newColor.type = phColorTypeYUV9;
00400     newColor.yuv9.y = y;
00401     newColor.yuv9.u = u;
00402     newColor.yuv9.v = v;
00403     newColor.bytes[4] = 
00404     newColor.bytes[5] = 
00405     newColor.bytes[6] = 
00406     newColor.bytes[7] = 0;
00407     
00408     return newColor;
00409 }
00410 /* ---------------------------------------------------------------------- */
00411 phColor phColorHSV24_new(uint8_t h,uint8_t s,uint8_t v)
00412 {
00413     phColor newColor;
00414     
00415     newColor.type = phColorTypeHSV24;
00416     newColor.hsv24.h = h;
00417     newColor.hsv24.s = s;
00418     newColor.hsv24.v = v;
00419     newColor.bytes[4] = 
00420     newColor.bytes[5] = 
00421     newColor.bytes[6] = 
00422     newColor.bytes[7] = 0;
00423     
00424     return newColor;
00425 }
00426 /* ---------------------------------------------------------------------- */
00427 phColor phColorGREY8_new(uint8_t y)
00428 {
00429     phColor newColor;
00430     
00431     newColor.type = phColorTypeGREY8;
00432     newColor.grey8.y = y;
00433     newColor.bytes[2] = 
00434     newColor.bytes[3] = 
00435     newColor.bytes[4] = 
00436     newColor.bytes[5] = 
00437     newColor.bytes[6] = 
00438     newColor.bytes[7] = 0;
00439     
00440     return newColor;
00441 }
00442 
00443 /* ---------------------------------------------------------------------- */
00444 phColor phColorSCT24_new(uint8_t s,uint8_t c,uint8_t t)
00445 {
00446     phColor newColor;
00447     
00448     newColor.type = phColorTypeSCT24;
00449     newColor.sct24.s = s;
00450     newColor.sct24.c = c;
00451     newColor.sct24.t = t;
00452     newColor.bytes[4] = 
00453     newColor.bytes[5] = 
00454     newColor.bytes[6] = 
00455     newColor.bytes[7] = 0;
00456     
00457     return newColor;
00458 }
00459 
00460 /* ---------------------------------------------------------------------- */
00461 int phColorPrint( int fd, phColor color )
00462 {
00463     phFUNCTION("phColorSave")
00464     int     j   = 0;
00465     char   *color_name = NULL;
00466     
00467 #if !defined(__ADSPBLACKFIN__)
00468     FILE    *fp = NULL;
00469    
00470     if ((fd < 0) || (fd == fileno(stdin))) fd = fileno(stdout);
00471     
00472     fp = fdopen(fd,"w");
00473     phCHECK_PTR(fp,"fdopen","fdopen failed(%d)",fd);
00474    
00475     if (color.type >= phColorCount)
00476     {
00477         color_name = "phColorUnknown";
00478     }
00479     else
00480     {
00481         color_name = phColorNames[color.type];
00482     }
00483     rc = fprintf(fp,"%s ", color_name );
00484     if (rc == EOF) goto success;
00485 
00486     for (j = 0; j < phCOLOR_MAXBYTES; j++ )
00487     {
00488         rc = fprintf(fp,"%d ",color.array.v[j]);
00489         if (rc == EOF) goto success;
00490     }
00491 
00492     rc = fprintf(fp,"\n");
00493     if (rc == EOF) goto success;
00494 
00495     fflush(fp);
00496 
00497 success:
00498     return phSUCCESS;
00499     
00500 #endif /* #if !defined(__ADSPBLACKFIN__) */
00501 error:
00502     return phFAIL;
00503 }
00504 
00505 /* ---------------------------------------------------------------------- */
00506 int phColorSave( const char   *filename, 
00507                  uint32_t      num_colors, 
00508                  phColor      *colors       )
00509 {
00510     phFUNCTION("phColorSave")
00511 
00512     uint32_t    i           = 0;
00513     uint32_t    j           = 0;
00514     char       *color_name  = NULL;
00515     
00516 #if !defined(__ADSPBLACKFIN__)
00517     FILE        *fp         = NULL;
00518     
00519     if ((num_colors == 0) || 
00520         (colors == NULL) || 
00521         (filename == NULL))
00522     {
00523         return phFAIL;
00524     }
00525 
00526     /* open the output file */
00527     fp = fopen(filename, "wt");
00528     phCHECK_PTR(fp,"fopen","fopen(%s,\"wt\")",filename);
00529 
00530     rc = ph_file_lock(fp);
00531     phPRINT_RC(rc,"ph_file_lock","ph_file_lock(%p)",fp);
00532 
00533     for ( i = 0; i < num_colors; i++ )
00534     {
00535         if (colors[i].type >= phColorCount)
00536         {
00537             color_name = "phColorUnknown";
00538         }
00539         else
00540         {
00541             color_name = phColorNames[colors[i].type];
00542         }
00543 
00544         rc = fprintf(fp,"%s ", color_name);
00545         if (rc == EOF) goto success;
00546 
00547         for (j = 0; j < phCOLOR_MAXBYTES; j++ )
00548         {
00549             rc = fprintf(fp,"%d ",colors[i].array.v[j]);
00550             if (rc == EOF) goto success;
00551         }
00552 
00553         rc = fprintf(fp,"\n");
00554         if (rc == EOF) goto success;
00555     }
00556 
00557 success:
00558     rc = ph_file_unlock(fp);
00559     phPRINT_RC(rc,"ph_file_lock","ph_file_lock(%p)",fp);
00560 
00561     fclose(fp);
00562     fp = NULL;
00563 
00564     return phSUCCESS;
00565 error:
00566     if (fp != NULL)
00567     {
00568         fclose(fp);
00569     }
00570 #endif /* #if !defined(__ADSPBLACKFIN__) */
00571     return phFAIL;
00572 }
00573 
00574 /* ---------------------------------------------------------------------- */
00575 int phColorRead(  int          fd,
00576                   uint32_t     max_colors,
00577                   uint32_t     *num_colors, 
00578                   phColor      **colors      )
00579 {
00580     phFUNCTION("phColorRead")
00581 
00582     char        *typebuf    = NULL;
00583 
00584     uint32_t    read_colors = 0;
00585     int         alloc_d     = 0;
00586 
00587     uint32_t    i           = 0;
00588     uint32_t    j           = 0;
00589     uint32_t    val         = 0;
00590     phColor     color;
00591     
00592 #if !defined(__ADSPBLACKFIN__)
00593     FILE        *fp         = NULL;
00594     
00595     
00596     if ((fd < 0) || (fd == fileno(stdin))) fd = fileno(stdout);
00597 
00598     if ((num_colors == NULL) || (colors == NULL))
00599     {
00600         return phFAIL;
00601     }
00602 
00603     typebuf = (char *)phCalloc(255,sizeof(char));
00604     phCHECK_PTR(typebuf,"phCalloc","phCalloc failed");
00605 
00606     /* open the output file handle */
00607     fp = fdopen(fd,"r");
00608     phCHECK_PTR(fp,"fdopen","fdopen failed(%d)",fd);
00609 
00610     rc = ph_file_lock(fp);
00611     phPRINT_RC(rc,"ph_file_lock","%p",fp);
00612 
00613     for ( i = 0; 
00614           
00615           ((max_colors == 0) || (i < max_colors)); 
00616           
00617           i++ )
00618     {
00619         rc = fscanf(fp,"%s ", typebuf);
00620         if (rc == EOF) goto success;
00621         
00622         for (j = 0; j < phCOLOR_MAXBYTES; j++ )
00623         {
00624             rc = fscanf(fp,"%u ", &val );
00625             if (rc == EOF) goto success;
00626 
00627             color.array.v[j] = val;
00628         }
00629         
00630         rc = fscanf(fp,"\n");
00631         if (rc == EOF) goto success;
00632         
00633         /* Record the color to the array */
00634         read_colors++;
00635         
00636         if ((colors != NULL) && ((*colors) == NULL)) 
00637         {
00638             (*colors) = (phColor *)phCalloc(read_colors,sizeof(phColor));
00639             phCHECK_PTR((*colors),"phCalloc","phCalloc failed allocating color array");
00640             alloc_d = 1;
00641         }
00642         else if (colors != NULL)
00643         {
00644             (*colors) = (phColor *)phRealloc(*colors,read_colors * sizeof(phColor));
00645             phCHECK_PTR((*colors),"phRealloc",
00646                     "phRealloc failed resizing the color array (read_colors:%u)",
00647                     read_colors);
00648         }
00649 
00650         if ((colors != NULL) && (*colors != NULL))
00651         {
00652             (*colors)[i].type = phColorStringToType(typebuf);
00653             
00654             phMemcpy((*colors)[i].array.v,
00655                      color.array.v,
00656                      sizeof(uint8_t) * phCOLOR_MAXBYTES);
00657         }
00658     }
00659     
00660 success:
00661     rc = ph_file_unlock(fp);
00662     phPRINT_RC(rc,"ph_file_unlock","ph_file_unlock(%p)",fp);
00663 
00664     phFree(typebuf);
00665     
00666     /* Keep track of the number of colors */
00667     if (num_colors != NULL)
00668     {
00669         *num_colors = read_colors;
00670     }
00671         
00672     return phSUCCESS;
00673 error:
00674     phFree(typebuf);
00675 
00676     if (alloc_d)
00677     {
00678         phFree(*colors);
00679     }
00680 #endif /* #if !defined(__ADSPBLACKFIN__) */
00681     return phFAIL;
00682 }
00683 /* ---------------------------------------------------------------------- */
00684 int phColorLoad(  const char   *filename, 
00685                   uint32_t     max_colors,
00686                   uint32_t     *num_colors, 
00687                   phColor      **colors      )
00688 {
00689     phFUNCTION("phColorLoad")
00690 
00691     /* Pointer to the allocated char buffer */
00692     char        *buf        = NULL;
00693     /* Total length read in to buf */
00694     size_t      buflen      = 0;
00695     /* Pointer to the beginning of data in buf */
00696     char        *bufptr     = NULL;
00697     /* Total length of the processed bufptr */
00698     size_t      bufptrlen   = 0;
00699     uint32_t    bufstart    = 0;
00700 
00701     char        *typebuf    = NULL;
00702     char        *valbuf     = NULL;
00703     uint32_t    read_colors = 0;
00704     int         alloc_d     = 0;
00705 
00706     uint32_t    j           = 0;
00707     uint32_t    val         = 0;
00708     phColor     color;
00709     uint8_t     color_type  = 0;
00710     uint8_t     color_depth = 0;
00711     
00712 #if !defined(__ADSPBLACKFIN__)
00713     FILE        *fp         = NULL;
00714     
00715     
00716     if ((num_colors == 0) || 
00717         (colors == NULL) || 
00718         (filename == NULL))
00719     {
00720         return phFAIL;
00721     }
00722 
00723     buf = (char *)phCalloc(255,sizeof(char));
00724     phCHECK_PTR(buf,"phCalloc","phCalloc failed");
00725 
00726     typebuf = (char *)phCalloc(255,sizeof(char));
00727     phCHECK_PTR(typebuf,"phCalloc","phCalloc failed");
00728 
00729     valbuf = (char *)phCalloc(255,sizeof(char));
00730     phCHECK_PTR(valbuf,"phCalloc","phCalloc failed");
00731 
00732     /* open the output file */
00733     fp = fopen(filename, "rt");
00734     phCHECK_PTR(fp,"fopen","fopen(%s,\"rt\")",filename);
00735 
00736     rc = ph_file_lock(fp);
00737     phPRINT_RC(rc,"ph_file_lock","ph_file_lock(%p)",fp);
00738 
00739     for ( read_colors = 0; 
00740           
00741           ((max_colors == 0) || (read_colors < max_colors)); 
00742           
00743           )
00744     {
00745         //rc = fscanf(fp,"%s ", buf);
00746         /* fgets reads in total - 1; stores a newline at the end */
00747         /* fgets returns NULL when there is no more data */
00748         rc = (fgets(buf,255,fp) == NULL ? 
00749                 EOF         /* ==NULL*/ : 
00750                 phSUCCESS   /* !=NULL*/);
00751         if (rc == EOF) goto success;
00752         /* Get the line size */
00753         buflen = strnlen(buf,255);
00754         /* increment passed whitespace; save begining of line */
00755         for (j = 0, bufptr = buf; 
00756              ((*bufptr == ' ') || (*bufptr == '\t')) && (j < buflen); 
00757              bufptr++, j++ );
00758         bufstart = j;
00759         /* insert a newline at a # */
00760         for (; 
00761              (*bufptr != '#') && (*bufptr != '\n') && (j < buflen); 
00762              bufptr++, j++ );
00763         /* Set the newline to be where the comment starts; if there is one */
00764         if (*bufptr == '#') *bufptr = '\0';
00765         /* setup the bufptr after processing it */
00766         bufptr = &(buf[bufstart]);
00767         /* Get the processed string length */
00768         bufptrlen = strnlen(bufptr,255-bufstart);
00769        
00770         /* if the processed buffer isn't empty, then read in the data */
00771         if (bufptrlen > 0)
00772         {
00773             rc = sscanf(bufptr,"%s ", typebuf);
00774             bufptr += strlen(typebuf) + 1;
00775             if (rc != EOF)
00776             {
00777                 color_type = phColorStringToType(typebuf);
00778                 color_depth = phColorTypeToDepth(color_type);
00779                 phMemset(&color,0,sizeof(phColor));
00780                 
00781                 for (j = 0; j < color_depth; j++ )
00782                 {
00783                     rc = sscanf(bufptr,"%s ", valbuf );
00784 
00785                     /* if we've consumed all the input, default the val to 0 */
00786                     if (rc == EOF)  
00787                         val = 0;
00788                     else
00789                         bufptr += strlen(valbuf) + 1;
00790 
00791                     val = atoi(valbuf);
00792 
00793                     color.array.v[j] = val;
00794                 }
00795         
00796                 /* Record the color to the array */
00797                 read_colors++;
00798 
00799                 if ((colors != NULL) && ((*colors) == NULL)) 
00800                 {
00801                     (*colors) = (phColor *)phCalloc(read_colors,sizeof(phColor));
00802                     phCHECK_PTR((*colors),"phCalloc","phCalloc failed allocating color array");
00803                     alloc_d = 1;
00804                 }
00805                 else if (colors != NULL)
00806                 {
00807                     (*colors) = (phColor *)phRealloc(*colors,read_colors * sizeof(phColor));
00808                     phCHECK_PTR((*colors),"phRealloc",
00809                                 "phRealloc failed resizing the color array (read_colors:%u)",
00810                     read_colors);
00811                 }
00812 
00813                 if ((colors != NULL) && (*colors != NULL))
00814                 {
00815                     (*colors)[read_colors-1].type = color_type;
00816 
00817                     phMemcpy((*colors)[read_colors-1].array.v,
00818                             color.array.v,
00819                             sizeof(uint8_t) * phCOLOR_MAXBYTES);
00820                 }
00821             }
00822         }
00823     }
00824     
00825 success:
00826     rc = ph_file_unlock(fp);
00827     phPRINT_RC(rc,"ph_file_unlock","ph_file_unlock(%p)",fp);
00828 
00829     phFree(typebuf);
00830     phFree(valbuf);
00831     phFree(buf);
00832     
00833     fclose(fp);
00834     fp = NULL;
00835 
00836     /* Keep track of the number of colors */
00837     if (num_colors != NULL)
00838     {
00839         *num_colors = read_colors;
00840     }
00841         
00842     return phSUCCESS;
00843 error:
00844     phFree(typebuf);
00845 
00846     if (alloc_d)
00847     {
00848         phFree(*colors);
00849     }
00850     if (fp != NULL)
00851     {
00852         fclose(fp);
00853     }
00854 #endif /* #if !defined(__ADSPBLACKFIN__) */
00855     return phFAIL;
00856 }
00857 
00858 /* ---------------------------------------------------------------------- */
00859 uint32_t phImageFormatToIndex(uint32_t f)
00860 {
00861     uint32_t v = 0;
00862     
00863     if ((f == 0) || (f == phImageNOFORMAT)) return 0;
00864 
00865     /* Special compression support */
00866     if (f & phImageSpecialMask) 
00867     {
00868         if ((f & phImageJPEG) && (f & phImageZlib))
00869         {
00870             v = phImageFormatCount * 3;
00871         }
00872         else if (f & phImageJPEG)
00873         {
00874             v = phImageFormatCount * 2;
00875         }
00876         else if (f & phImageZlib)
00877         {
00878             /* The Zlib compression depth and strings start after
00879              * the normal depth and strings */
00880             v = phImageFormatCount * 1;
00881         }
00882 
00883         /* Remove the compression bit to find out the format of the
00884          * decompressed data */
00885         f &= ~phImageSpecialMask;
00886     }    
00887     f &= phImageValidFormatMask;
00888     
00889     while (f)
00890     {
00891         f = (f >> 1);
00892         v++;
00893     }
00894     return v;
00895 }
00896 
00897 /* ---------------------------------------------------------------------- */
00898 const char *phImageFormatToString(uint32_t f)
00899 {
00900     return (const char*)phImageNames[phImageFormatToIndex(f)];
00901 }
00902 
00903 /* ---------------------------------------------------------------------- */
00904 uint8_t phImageFormatToDepth(uint32_t f)
00905 {
00906     return phImageDepth[phImageFormatToIndex(f)];
00907 }
00908 
00909 /* ---------------------------------------------------------------------- */
00910 uint32_t phImageFormatTo32BitMask(uint32_t f, uint32_t c)
00911 {
00912     uint32_t index = phImageFormatToIndex(f);
00913     if (index > phImageFormatTotalCount) index = 0;
00914     return phImageChannelMasks[index][c];
00915 }
00916 
00917 /* ---------------------------------------------------------------------- */
00918 uint8_t phImageFormatToColorType( uint32_t f)
00919 {
00920     /* Return the index from only the format type, 
00921      * not the special type and the format type */
00922     return phImageFormatToIndex(f & ~phImageSpecialMask);
00923 }
00924 
00925 /* ---------------------------------------------------------------------- */
00926 void phImageFormatIndecies(uint32_t f, 
00927                            uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
00928 {
00929     uint32_t index = phImageFormatToIndex(f);
00930     if (r != 0) *r = phImageChannelIndex[index][0];
00931     if (g != 0) *g = phImageChannelIndex[index][1];
00932     if (b != 0) *b = phImageChannelIndex[index][2];
00933     if (a != 0) *a = phImageChannelIndex[index][3];
00934 }
00935 
00936 /* ---------------------------------------------------------------------- */
00937 uint32_t phColorTypeToFormat( uint8_t type )
00938 {
00939     if (type >= phColorCount)       return phImageNOFORMAT;
00940     if (type == phColorTypeArray)   return phImageNOFORMAT;
00941     return (1<<(type - 1));
00942 }
00943 
00944 /* ---------------------------------------------------------------------- */
00945 uint8_t phColorTypeToDepth(uint8_t type)
00946 {
00947     if (type >= phColorCount) return 4;
00948     return phColorDepth[type];
00949 }
00950 
00951 /* ---------------------------------------------------------------------- */
00952 uint8_t phColorStringToType(const char *str)
00953 {
00954     int     j = 0;
00955     uint8_t type = 0;
00956 
00957     for (j = 0; (type == 0) && (j < phColorCount); j++ )
00958     {
00959         if (strcmp(str,phColorNames[j]) == 0)
00960         {        
00961             type = j;
00962         }
00963     }
00964     return type;
00965 }
00966 
00967 /* ---------------------------------------------------------------------- */
00968 const char *phColorTypeToString(uint8_t type)
00969 {
00970     if (type >= phColorCount) return "phColorUnknown";
00971     return phColorNames[type];
00972 }
00973 /* ---------------------------------------------------------------------- */
00974 uint32_t phColorToFormat(const phColor c )
00975 {
00976     return phColorTypeToFormat(c.type);
00977 }
00978 /* ---------------------------------------------------------------------- */
00979 uint8_t phColorToDepth(const phColor c)
00980 {
00981     return phColorTypeToDepth(c.type);
00982 }
00983 /* ---------------------------------------------------------------------- */
00984 const char *phColorToString(const phColor c)
00985 {
00986     return phColorTypeToString(c.type);
00987 }
00988 
00989 /* ---------------------------------------------------------------------- */
00990 #define phColorMIN(a,b) ((a < b) ? a : b)
00991 int phColorEqual(phColor one, phColor two)
00992 {
00993     uint32_t j          = 0;
00994     uint8_t bytes_one   = 0;
00995     uint8_t bytes_two   = 0;
00996     uint8_t bytes       = 0;
00997     uint8_t match       = 0;
00998     
00999     if (one.type >= phColorCount) one.type = phColorTypeArray;
01000     if (two.type >= phColorCount) two.type = phColorTypeArray;
01001     
01002     bytes_one = phColorToDepth(one);
01003     bytes_two = phColorToDepth(two);
01004 
01005     /* This checks to see if we're trying to match two different color
01006      * types; i.e. they aren't the same; But this allows one to test
01007      * the values in a non-format color type with any format. I.E. the
01008      * phColorTypeArray can be compared to the bytes in any phColor cause
01009      * phColorTypeArray isn't a strict color type */
01010     if ((bytes_one != bytes_two) && 
01011         (one.type != phColorTypeArray) &&
01012         (one.type != phColorTypeArray))
01013     {
01014         return 0;
01015     }
01016     bytes = phColorMIN(bytes_one,bytes_two);
01017     
01018     for (j = 0; j < bytes; j++ )
01019         if (one.array.v[j] == two.array.v[j])
01020             match++;
01021         
01022     if (match == bytes) return 1;
01023     
01024     return 0;
01025 }
01026 
01027     
01028 #ifdef __cplusplus
01029 }
01030 #endif
01031 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:02 2007 for phission by  doxygen 1.4.4