Beispiel #1
0
INT32 CNCSJPCCodeBlock::ReadNewSegs(CNCSJPCIOStream &Stream)
{
	INT32 nRead = 0;
	UINT32 nNewSegs = (UINT32)m_NextSegments.size();
	if(nNewSegs) {
		for(UINT32 i = 0; i < nNewSegs; i++) {
			UINT32 iSeg = m_NextSegments[i].m_nIndex;

			if(m_Segments.size() < iSeg + 1) {
				m_Segments.resize(iSeg + 1);
			}
			CNCSJPCSegment &Seg = m_Segments[iSeg];
			CNCSJPCSegment &NextSeg = m_NextSegments[i];

				// To make the MQ Decoder faster, we always append 0xffff on the end here.
				// This saves a lot of special case handling later inside some very
				// tight loops...
			Seg.m_nIndex = (UINT16)iSeg;
			Seg.m_pData = (UINT8*)NCSRealloc(Seg.m_pData, Seg.m_nLength + NextSeg.m_nLength + 2, FALSE);
			if(Stream.Read(Seg.m_pData + Seg.m_nLength, NextSeg.m_nLength) == false) {
				// Read error;
				Seg.m_pData[Seg.m_nLength] = 0xff;
				Seg.m_pData[Seg.m_nLength + 1] = 0xff;
				nRead = -1;
				break;
			}
			Seg.m_nLength += NextSeg.m_nLength;
			Seg.m_pData[Seg.m_nLength] = 0xff;
			Seg.m_pData[Seg.m_nLength + 1] = 0xff;
			Seg.m_nPasses = Seg.m_nPasses + NextSeg.m_nPasses;

			nRead += NextSeg.m_nLength;

			sm_Tracker.AddMem(NextSeg.m_nLength);
		}
		m_NextSegments.clear();
		// Force T1 re-decode
		m_DecBuf.Free();
	}
	return(nRead);
}
// Parse the box from the stream
CNCSError CNCSJP2File::CNCSJP2GMLGeoLocationBox::Parse(class CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
#ifdef NCS_BUILD_WITH_STDERR_DEBUG_INFO
    fprintf(stderr,"Parsing GML box information\n");
#endif

    CNCSError Error(NCS_SUCCESS);
    m_bValid = false;

    double dRegX = 0.0;
    double dRegY = 0.0;
    double p1[3];
    double p2[3];
    UINT32 nEPSGCode = 0;
    int nResults = 0;
    bool bSRSAttributePresent = false;
    UINT32 nImageWidth = JP2File.m_FileInfo.nSizeX;
    UINT32 nImageHeight = JP2File.m_FileInfo.nSizeY;

    NCSJP2_CHECKIO_BEGIN(Error, Stream);
    char buf[1024 + 1];
    Stream.Read(buf, NCSMin((UINT32)m_nLDBox, sizeof(buf)-1));
    buf[NCSMin((UINT32)m_nLDBox, sizeof(buf)-1)] = '\0';

    TiXmlDocument doc;
    doc.Parse(buf);
    TiXmlHandle docHandle(&doc);
    TiXmlElement *GeoLocation_1 = docHandle.FirstChild("JPEG2000_GeoLocation").FirstChild("gml:RectifiedGrid").Element();//.FirstChild( "Element" ).Child( "Child", 1 ).Element();
    if(GeoLocation_1 && GeoLocation_1->Attribute("gml:id") && !stricmp(GeoLocation_1->Attribute("gml:id"), "JPEG2000_GeoLocation_1")) {
        TiXmlElement *OriginPoint = docHandle.FirstChild("JPEG2000_GeoLocation").FirstChild("gml:RectifiedGrid").FirstChild("gml:origin").FirstChild("gml:Point").Element();
        if(OriginPoint && OriginPoint->Attribute("gml:id") && !stricmp(OriginPoint->Attribute("gml:id"), "JPEG2000_Origin")) {
            const char *pTxt = OriginPoint->Attribute("srsName");
            if(pTxt) {
                nResults += sscanf(pTxt, "epsg:%u", &nEPSGCode);
                bSRSAttributePresent = true;
            }
            TiXmlText *Coords = docHandle.FirstChild("JPEG2000_GeoLocation").FirstChild("gml:RectifiedGrid").FirstChild("gml:origin").FirstChild("gml:Point").FirstChild("gml:coordinates").FirstChild().Text();
            if(Coords) {
                pTxt = Coords->Value();
                if(pTxt) {
                    nResults += sscanf(pTxt, "%lf,%lf", &dRegX, &dRegY);
                }
            }
        }
        TiXmlElement *offsetVector = docHandle.FirstChild("JPEG2000_GeoLocation").FirstChild("gml:RectifiedGrid").FirstChild("gml:offsetVector").Element();
        if(offsetVector && offsetVector->Attribute("gml:id") && !stricmp(offsetVector->Attribute("gml:id"), "p1")) {
            TiXmlText *Coords = docHandle.FirstChild("JPEG2000_GeoLocation").FirstChild("gml:RectifiedGrid").FirstChild("gml:offsetVector").FirstChild().Text();
            if(Coords) {
                const char *pTxt = Coords->Value();
                if(pTxt) {
                    nResults += sscanf(pTxt, "%lf,%lf,%lf", &p1[0], &p1[1], &p1[2]);
                }
            }

            offsetVector = (TiXmlElement*)offsetVector->NextSibling("gml:offsetVector");
            if(offsetVector && offsetVector->Attribute("gml:id") && !stricmp(offsetVector->Attribute("gml:id"), "p2")) {
                TiXmlText *Coords = ((TiXmlElement*)offsetVector->FirstChild())->ToText();
                if(Coords) {
                    const char *pTxt = Coords->Value();
                    if(pTxt) {
                        nResults += sscanf(pTxt, "%lf,%lf,%lf", &p2[0], &p2[1], &p2[2]);
                    }
                }
            }
        }
    }
    NCSJP2_CHECKIO_END();

    if((nResults == 9 && bSRSAttributePresent) || (nResults == 8 && !bSRSAttributePresent)) {
        IEEE8 dRegistrationX = dRegX + nImageHeight * p1[0];
        IEEE8 dRegistrationY = dRegY + nImageHeight * p1[1];

        if(p1[2] == 0.0 && p2[2] == 0.0) {
//				p1[0] = sin(Deg2Rad(dCWRotationDegrees)) * dCellSizeX;
//				p1[1] = cos(Deg2Rad(dCWRotationDegrees)) * dCellSizeY;
//				p2[0] = cos(Deg2Rad(dCWRotationDegrees)) * dCellSizeX;
//				p2[1] = -sin(Deg2Rad(dCWRotationDegrees)) * dCellSizeY;

            double dCWRotationDegrees = Rad2Deg(atan(p1[0] / p2[0]));
            double dCellSizeX = p2[0] / cos(atan(p1[0] / p2[0]));
            double dCellSizeY = p1[1] / cos(atan(p1[0] / p2[0]));

            m_GMLFileInfo.fOriginX = dRegistrationX;
            m_GMLFileInfo.fOriginY = dRegistrationY;
            m_GMLFileInfo.fCellIncrementX = dCellSizeX;
            m_GMLFileInfo.fCellIncrementY = dCellSizeY;
            m_GMLFileInfo.fCWRotationDegrees = dCWRotationDegrees;

            CNCSGDTEPSG& Epsg = *CNCSGDTEPSG::Instance();
            char *pProjection = NULL;
            char *pDatum = NULL;
            NCSFree(m_GMLFileInfo.szProjection);
            NCSFree(m_GMLFileInfo.szDatum);
            if (bSRSAttributePresent && nEPSGCode &&
                    (Epsg.GetProjectionAndDatum(nEPSGCode, &pProjection, &pDatum) == NCS_SUCCESS))
            {
                if(pProjection && pDatum)
                {
                    m_GMLFileInfo.szProjection= NCSStrDup(pProjection);
                    m_GMLFileInfo.szDatum = NCSStrDup(pDatum);
                    NCSFree(pProjection);
                    NCSFree(pDatum);
                }
                else if (nEPSGCode) //EPSG code present but invalid or unrecognised?
                {
                    char szEPSG[32];
                    *szEPSG = '\0';
                    sprintf(szEPSG,"epsg:%u",nEPSGCode);
                    m_GMLFileInfo.szProjection = NCSStrDup(szEPSG);
                    m_GMLFileInfo.szDatum = NCSStrDup(szEPSG);
                }
            }
            else
            {
                m_GMLFileInfo.szDatum = NCSStrDup("RAW");
                m_GMLFileInfo.szProjection = NCSStrDup("RAW");
            }
            if(stricmp(m_GMLFileInfo.szProjection, "GEODETIC") == 0)
                m_GMLFileInfo.eCellSizeUnits = ECW_CELL_UNITS_DEGREES;
            else
                m_GMLFileInfo.eCellSizeUnits = ECW_CELL_UNITS_METERS;
        }
        else return NCS_JP2_GEODATA_READ_ERROR;
    }
    else return NCS_JP2_GEODATA_READ_ERROR;
    m_bValid = true;
    NCSStandardizeFileInfoEx(&m_GMLFileInfo);

    return NCS_SUCCESS;
}