Changeset 11895

Show
Ignore:
Timestamp:
08/17/07 16:03:51 (1 year ago)
Author:
rouault
Message:

* fix bug #1744 : Bad colors for a CADRG file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/gdal/frmts/nitf/nitfimage.c

    r11813 r11895  
    4545 
    4646static void NITFLoadLocationTable( NITFImage *psImage ); 
     47static void NITFLoadColormapSubSection( NITFImage *psImage ); 
    4748static int NITFLoadVQTables( NITFImage *psImage ); 
    4849 
     
    525526/* -------------------------------------------------------------------- */ 
    526527    NITFLoadLocationTable( psImage ); 
     528     
     529    /* Fix bug #1744 */ 
     530    if (psImage->nBands == 1) 
     531        NITFLoadColormapSubSection ( psImage ); 
    527532 
    528533/* -------------------------------------------------------------------- */ 
     
    20882093    return TRUE; 
    20892094} 
     2095 
     2096/************************************************************************/ 
     2097/*                       NITFLoadColormapSubSection()                   */ 
     2098/************************************************************************/ 
     2099 
     2100/* This function is directly inspired by function parse_clut coming from ogdi/driver/rpf/utils.c 
     2101   and placed under the following copyright */ 
     2102 
     2103/* 
     2104 ****************************************************************************** 
     2105 * Copyright (C) 1995 Logiciels et Applications Scientifiques (L.A.S.) Inc 
     2106 * Permission to use, copy, modify and distribute this software and 
     2107 * its documentation for any purpose and without fee is hereby granted, 
     2108 * provided that the above copyright notice appear in all copies, that 
     2109 * both the copyright notice and this permission notice appear in 
     2110 * supporting documentation, and that the name of L.A.S. Inc not be used  
     2111 * in advertising or publicity pertaining to distribution of the software  
     2112 * without specific, written prior permission. L.A.S. Inc. makes no 
     2113 * representations about the suitability of this software for any purpose. 
     2114 * It is provided "as is" without express or implied warranty. 
     2115 ****************************************************************************** 
     2116 */ 
     2117 
     2118 
     2119static void NITFLoadColormapSubSection( NITFImage *psImage ) 
     2120{ 
     2121    int nLocBaseColorGrayscaleSection = 0; 
     2122    int nLocBaseColormapSubSection = 0; 
     2123    int colorGrayscaleSectionSize = 0; 
     2124    int colormapSubSectionSize = 0; 
     2125    NITFFile *psFile = psImage->psFile; 
     2126    int i, j; 
     2127    unsigned char nOffsetRecs; 
     2128    NITFColormapRecord* colormapRecords; 
     2129    unsigned int colormapOffsetTableOffset; 
     2130    unsigned short offsetRecLen; 
     2131     
     2132    NITFBandInfo *psBandInfo = psImage->pasBandInfo; 
     2133   
     2134    for( i = 0; i < psImage->nLocCount; i++ ) 
     2135    { 
     2136        if( psImage->pasLocations[i].nLocId == LID_ColorGrayscaleSectionSubheader ) 
     2137        { 
     2138            nLocBaseColorGrayscaleSection = psImage->pasLocations[i].nLocOffset; 
     2139            colorGrayscaleSectionSize = psImage->pasLocations[i].nLocSize; 
     2140        } 
     2141        else if( psImage->pasLocations[i].nLocId == LID_ColormapSubsection ) 
     2142        { 
     2143            nLocBaseColormapSubSection = psImage->pasLocations[i].nLocOffset; 
     2144            colormapSubSectionSize = psImage->pasLocations[i].nLocSize; 
     2145        } 
     2146    } 
     2147    if (nLocBaseColorGrayscaleSection == 0) 
     2148    { 
     2149        //fprintf(stderr, "nLocBaseColorGrayscaleSection == 0\n"); 
     2150        return; 
     2151    } 
     2152    if (nLocBaseColormapSubSection == 0) 
     2153    { 
     2154        //fprintf(stderr, "nLocBaseColormapSubSection == 0\n"); 
     2155        return; 
     2156    } 
     2157     
     2158    if( VSIFSeekL( psFile->fp, nLocBaseColorGrayscaleSection,  
     2159                  SEEK_SET ) != 0 ) 
     2160    { 
     2161        CPLError( CE_Failure, CPLE_FileIO,  
     2162                  "Failed to seek to %d.", 
     2163                  nLocBaseColorGrayscaleSection ); 
     2164        return; 
     2165    } 
     2166     
     2167     
     2168    VSIFReadL( &nOffsetRecs, 1, 1, psFile->fp ); 
     2169     
     2170    if( VSIFSeekL( psFile->fp, nLocBaseColormapSubSection,  
     2171                  SEEK_SET ) != 0  ) 
     2172    { 
     2173        CPLError( CE_Failure, CPLE_FileIO,  
     2174                  "Failed to seek to %d.", 
     2175                  nLocBaseColormapSubSection ); 
     2176        return; 
     2177    } 
     2178     
     2179    colormapRecords = (NITFColormapRecord*)CPLMalloc(nOffsetRecs * sizeof(NITFColormapRecord)); 
     2180 
     2181     /* colormap offset table offset length */ 
     2182    VSIFReadL( &colormapOffsetTableOffset, 1, sizeof(colormapOffsetTableOffset),  psFile->fp ); 
     2183    CPL_MSBPTR32( &colormapOffsetTableOffset ); 
     2184 
     2185     /* offset record length */ 
     2186    VSIFReadL( &offsetRecLen, 1, sizeof(offsetRecLen),  psFile->fp ); 
     2187    CPL_MSBPTR16( &offsetRecLen ); 
     2188     
     2189    for (i = 0; i < nOffsetRecs; i++) 
     2190    { 
     2191        VSIFReadL( &colormapRecords[i].tableId, 1, sizeof(colormapRecords[i].tableId),  psFile->fp ); 
     2192        CPL_MSBPTR16( &colormapRecords[i].tableId ); 
     2193         
     2194        VSIFReadL( &colormapRecords[i].nRecords, 1, sizeof(colormapRecords[i].nRecords),  psFile->fp ); 
     2195        CPL_MSBPTR32( &colormapRecords[i].nRecords ); 
     2196         
     2197        VSIFReadL( &colormapRecords[i].elementLength, 1, sizeof(colormapRecords[i].elementLength),  psFile->fp ); 
     2198     
     2199        VSIFReadL( &colormapRecords[i].histogramRecordLength, 1, sizeof(colormapRecords[i].histogramRecordLength),  psFile->fp ); 
     2200        CPL_MSBPTR16( &colormapRecords[i].histogramRecordLength ); 
     2201     
     2202        VSIFReadL( &colormapRecords[i].colorTableOffset, 1, sizeof(colormapRecords[i].colorTableOffset),  psFile->fp ); 
     2203        CPL_MSBPTR32( &colormapRecords[i].colorTableOffset ); 
     2204     
     2205        VSIFReadL( &colormapRecords[i].histogramTableOffset, 1, sizeof(colormapRecords[i].histogramTableOffset),  psFile->fp ); 
     2206        CPL_MSBPTR32( &colormapRecords[i].histogramTableOffset ); 
     2207    } 
     2208     
     2209    for (i=0; i<nOffsetRecs; i++) 
     2210    { 
     2211        if( VSIFSeekL( psFile->fp, nLocBaseColormapSubSection + colormapRecords[i].colorTableOffset,  
     2212                    SEEK_SET ) != 0  ) 
     2213        { 
     2214            CPLError( CE_Failure, CPLE_FileIO,  
     2215                    "Failed to seek to %d.", 
     2216                    nLocBaseColormapSubSection + colormapRecords[i].colorTableOffset ); 
     2217            CPLFree(colormapRecords); 
     2218            return; 
     2219        } 
     2220         
     2221        /* This test is very CADRG specific. See MIL-C-89038, paragraph 3.12.5.a */ 
     2222        if (i == 0 && 
     2223            colormapRecords[i].tableId == 2 && 
     2224            colormapRecords[i].elementLength == 4 && 
     2225            colormapRecords[i].nRecords == 216)   /* read, use colortable */ 
     2226        { 
     2227            GByte* rgbm = (GByte*)CPLMalloc(colormapRecords[i].nRecords * 4); 
     2228            if (VSIFReadL(rgbm, 1, colormapRecords[i].nRecords * 4,  
     2229                     psFile->fp ) != colormapRecords[i].nRecords * 4 ) 
     2230            { 
     2231                CPLError( CE_Failure, CPLE_FileIO,  
     2232                          "Failed to read %d byte rgbm.", 
     2233                           colormapRecords[i].nRecords * 4); 
     2234                CPLFree(rgbm); 
     2235                CPLFree(colormapRecords); 
     2236                return; 
     2237            } 
     2238            for (j = 0; j < colormapRecords[i].nRecords; j++) 
     2239            { 
     2240                psBandInfo->pabyLUT[j] = rgbm[4*j]; 
     2241                psBandInfo->pabyLUT[j+256] = rgbm[4*j+1]; 
     2242                psBandInfo->pabyLUT[j+512] = rgbm[4*j+2]; 
     2243            } 
     2244            CPLFree(rgbm); 
     2245        } 
     2246    }  
     2247 
     2248    CPLFree(colormapRecords); 
     2249} 
     2250 
     2251 
    20902252 
    20912253 
  • trunk/gdal/frmts/nitf/nitflib.h

    r11524 r11895  
    9999    int nLocSize; 
    100100} NITFLocation; 
     101 
     102typedef struct 
     103{ 
     104  unsigned short   tableId; 
     105  unsigned int     nRecords; 
     106  unsigned char    elementLength; 
     107  unsigned short   histogramRecordLength; 
     108  unsigned int     colorTableOffset; 
     109  unsigned int     histogramTableOffset; 
     110} NITFColormapRecord; 
     111 
    101112 
    102113typedef struct {