File: gdalchecksum.cpp

package info (click to toggle)
gdal 1.3.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 37,640 kB
  • ctags: 38,948
  • sloc: cpp: 303,891; ansic: 136,081; sh: 8,216; python: 6,215; java: 2,991; perl: 1,532; makefile: 674; xml: 185; php: 24
file content (117 lines) | stat: -rw-r--r-- 4,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/******************************************************************************
 * $Id: gdalchecksum.cpp,v 1.5 2005/04/04 15:24:16 fwarmerdam Exp $
 *
 * Project:  GDAL
 * Purpose:  Compute simple checksum for a region of image data. 
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 *
 ******************************************************************************
 * Copyright (c) 2003, Frank Warmerdam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 *
 * $Log: gdalchecksum.cpp,v $
 * Revision 1.5  2005/04/04 15:24:16  fwarmerdam
 * added CPL_STDCALL to some functions
 *
 * Revision 1.4  2004/12/30 20:40:54  fwarmerdam
 * Added documentation.
 *
 * Revision 1.3  2003/05/02 16:02:06  dron
 * Memory leak fixed.
 *
 * Revision 1.2  2003/03/13 16:47:20  warmerda
 * fixed bug in non-complex case
 *
 * Revision 1.1  2003/03/02 03:56:21  warmerda
 * New
 *
 */

#include "gdal_alg.h"
#include "cpl_conv.h"

CPL_CVSID("$Id: gdalchecksum.cpp,v 1.5 2005/04/04 15:24:16 fwarmerdam Exp $");

/************************************************************************/
/*                         GDALChecksumImage()                          */
/************************************************************************/

/**
 * Compute checksum for image region. 
 *
 * Computes a 16bit (0-65535) checksum from a region of raster data on a GDAL 
 * supported band.   Floating point data is converted to 32bit integer 
 * so decimal portions of such raster data will not affect the checksum.
 * Real and Imaginary components of complex bands influence the result. 
 *
 * @param hBand the raster band to read from.
 * @param nXOff pixel offset of window to read.
 * @param nYOff line offset of window to read.
 * @param nXSize pixel size of window to read.
 * @param nYSize line size of window to read.
 *
 * @return Checksum value. 
 */

int CPL_STDCALL 
GDALChecksumImage( GDALRasterBandH hBand, 
                   int nXOff, int nYOff, int nXSize, int nYSize )

{
    const static int anPrimes[11] = 
        { 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };

    int  iLine, i, nChecksum = 0, iPrime = 0, nCount;
    int  *panLineData;
    int  bComplex = GDALDataTypeIsComplex( GDALGetRasterDataType( hBand ) );

    panLineData = (GInt32 *) CPLMalloc(nXSize * sizeof(GInt32) * 2);

    for( iLine = nYOff; iLine < nYOff + nYSize; iLine++ )
    {
        if( bComplex )
        {
            GDALRasterIO( hBand, GF_Read, nXOff, iLine, nXSize, 1, 
                          panLineData, nXSize, 1, GDT_CInt32, 0, 0 );
            nCount = nXSize * 2;
        }
        else
        {
            GDALRasterIO( hBand, GF_Read, nXOff, iLine, nXSize, 1, 
                          panLineData, nXSize, 1, GDT_Int32, 0, 0 );
            nCount = nXSize;
        }

        for( i = 0; i < nCount; i++ )
        {
            nChecksum += (panLineData[i] % anPrimes[iPrime++]);
            if( iPrime > 10 )
                iPrime = 0;

            nChecksum &= 0xffff;
        }
    }

    CPLFree( panLineData );

    return nChecksum;
}