Beispiel #1
0
// UnParse the marker out to the stream.
CNCSError CNCSJPCMarker::UnParse(CNCSJPC &JPC, CNCSJPCIOStream &Stream)
{
	&JPC;//Keep compiler happy
	m_nOffset = Stream.Tell();
	Stream.WriteUINT16((UINT16)m_eMarker);
	return(Stream.GetError());
}
CNCSError JP2UUID3DNBox::UnParse(CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
    CNCSError Error;

    Error = CNCSJP2Box::UnParse(JP2File, Stream);
    NCSJP2_CHECKIO_BEGIN(Error, Stream);
    Stream.WriteIEEE8(_OrigMaxVal);
    Stream.WriteIEEE8(_OrigMinVal);
    Stream.WriteUINT32(_NormalizedMaxVal);
    Stream.WriteUINT32(_NormalizedMinVal);
    NCSJP2_CHECKIO_END();
    return(Error);
} // JP2UUID3DNBox::UnParse
Beispiel #3
0
// Parse the marker in from the JP2 file.
CNCSError CNCSJPCMarker::Parse(CNCSJPC &JPC, CNCSJPCIOStream &Stream)
{
	&JPC;//Keep compiler happy
		// Marker.
	UINT16 t16;

	m_nOffset = Stream.Tell();

	if(Stream.ReadUINT16(t16)) {
		m_eMarker = (Type)t16;
		m_bHaveMarker = true;
	}
	return(Stream.GetError());
}
CNCSError JP2UUID3DNBox::Parse(class CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
    CNCSError Error;

//See NCSJPCIOStream.h to get an understanding of these macros
    NCSJP2_CHECKIO_BEGIN(Error, Stream);
    NCSJP2_CHECKIO(Read(m_UUID.m_UUID, sizeof(m_UUID.m_UUID)));


    if (m_UUID == sm_UUID)
    {
        Stream.ReadIEEE8(_OrigMaxVal);
        Stream.ReadIEEE8(_OrigMinVal);
        Stream.ReadUINT32(_NormalizedMaxVal);
        Stream.ReadUINT32(_NormalizedMinVal);
    } // if
    NCSJP2_CHECKIO_END();

    return(Error);
} // JP2UUID3DNBox::Parse
Beispiel #5
0
// UnParse the tree to the JPC codestream.
bool CNCSJPCTagTree::UnParse(CNCSJPCIOStream &Stream, INT32 nX, INT32 nY, INT32 nThreshold)
{
	INT32 nLeaf = nY * m_nWidth + nX;
    Node *stk[31];
    Node **stkptr;
    Node *node;
    int low;
	bool bRet = true;

    stkptr = stk;
    node = &m_Nodes[nLeaf];
    while(node->m_pParent) {
        *stkptr++ = node;
        node = node->m_pParent;
    }

    low=0;
    for (;;) {
		if (low > node->m_nState) {
            node->m_nState = (UINT16)low;
        } else {
            low = node->m_nState;
        }
        while (low < nThreshold) {
            if (low >= node->m_nValue) {
				if (!node->m_bKnown) {
                    bRet &= Stream.Stuff(true);
                    node->m_bKnown = true;
                }
                break;
            }
			bRet &= Stream.Stuff(false);
            ++low;
        }
		node->m_nState = (UINT16)low;
        if (stkptr == stk) break;
        node = *--stkptr;
    }
	return(bRet);
}
// UnParse the box to the stream
CNCSError CNCSJP2File::CNCSJP2GMLGeoLocationBox::UnParse(class CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
#ifdef NCS_BUILD_WITH_STDERR_DEBUG_INFO
    fprintf(stderr,"UnParsing GML box information\n");
#endif


    char buf[1024];
    CNCSError Error = FormatXML(buf, sizeof(buf));

    if(Error == NCS_SUCCESS) {
        Error = CNCSJP2Box::UnParse(JP2File, Stream);
        NCSJP2_CHECKIO_BEGIN(Error, Stream);
        Stream.Write(buf, (UINT32)strlen(buf));
        NCSJP2_CHECKIO_END();
    }
    return(Error);
}
Beispiel #7
0
// Parse the tree from the JPC codestream.
bool CNCSJPCTagTree::Parse(CNCSJPCIOStream &Stream, INT32 nX, INT32 nY, INT32 nAbort, bool &bVal)
{
	Node *leaf = &m_Nodes[nY * m_nWidth + nX];
	Node *stk[32 - 1];
	Node **stkptr;
	Node *node;

	stkptr = stk;
	node   = leaf;

	while (node->m_pParent)
    {
		*stkptr++ = node;
		node = node->m_pParent;
	}

	int low = 0;
	for (;;)
    {
		if (low > (int)node->m_nState) node->m_nState = (UINT16)low;
        else                     low = node->m_nState;

		while (low < nAbort && low < node->m_nValue)
        {
			bool bBit;
			if(Stream.UnStuff(bBit) == false) {
				return(false);
			}
			if (bBit) {
				node->m_nValue = (UINT16)low;
				node->m_bKnown = true;
			}
			else           low++;
		}
		node->m_nState = (UINT16)low;

		if (stkptr == stk) break;

        node = *--stkptr;
	}
	bVal = node->m_nValue < nAbort;
	return(true);
}
Beispiel #8
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);
}
Beispiel #9
0
INT32 CNCSJPCCodeBlock::WriteNewSegs(CNCSJPCIOStream &Stream)
{
	INT32 nWritten = 0;
	UINT32 nNewSegs = (UINT32)m_NextSegments.size();
	if(nNewSegs) {
		for(UINT32 i = 0; i < nNewSegs; i++) {
			CNCSJPCSegment &Seg = m_NextSegments[i];

			if(Stream.Write(Seg.m_pData, Seg.m_nLength) == false) {
				// Read error;
				nWritten = -1;
				break;
			}
			nWritten += Seg.m_nLength;

			sm_Tracker.RemoveMem(Seg.m_nLength);
		}
		m_NextSegments.clear();
	}
	return(nWritten);
}
Beispiel #10
0
// UnParse the box out to the stream.
CNCSError CNCSJP2Box::UnParse(class CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
	CNCSError Error;

	// If length is > 2^32, need to write out the 64bit XLBox field.
	if(m_nXLBox > 0xffffffff) {
		// Will be writing XLBox, so write out 1 for the LBox field.
		Stream.WriteUINT32(1);
	} else {
		Stream.WriteUINT32((UINT32)m_nXLBox);
	}
	if(Stream.GetError() == NCS_SUCCESS) {
		// Write out the box type;
		if(Stream.WriteUINT32(m_nTBox) && m_nXLBox > 0xffffffff) {
			// Write out the XLBox field.
			Stream.WriteUINT64(m_nXLBox);
		}
		Error = Stream.GetError();
	}
	return(Error);
}
Beispiel #11
0
// Parse the box in from the JP2 file.
CNCSError CNCSJP2Box::Parse(class CNCSJP2File &JP2File, CNCSJPCIOStream &Stream)
{
	CNCSError Error;

	CNCSJP2BoxList::iterator pCur = m_Prev.begin();

	while(pCur != m_Prev.end()) {  // Make sure box follows the correct box(es)
		if((*pCur)->m_bHaveBox == false) {
			Error = NCS_FILE_INVALID;
			break;
		}
		pCur++;
	}
	if(Error == NCS_SUCCESS) {	// Make sure box proceeds the correct box(es)
		pCur = m_Next.begin();

		while(pCur != m_Next.end()) {
			if((*pCur)->m_bHaveBox == true) {
				Error = NCS_FILE_INVALID;
				break;
			}
			pCur++;
		}

		if(Error == NCS_SUCCESS) {
			if(Stream.Mark()) { // Mark the stream, so we can rewind on an error.
				UINT32 nLen;

					// Store absolute offset of box in stream
				m_nBoxOffset = Stream.Tell();

					// Box length.
				if(Stream.ReadUINT32(nLen)) {
			
						// Box type
					if(Stream.ReadUINT32(m_nTBox)) {
						//
						// If the 32 bit nLen is equal to 1, then there is a 64bit length field present.
						//
						if(nLen == 1) {
							// Read in the 64bit length
							if(Stream.ReadUINT64(m_nXLBox)) {
								// Calculate the Data length within the box.
								m_nLDBox = m_nXLBox - 16;
							}
						} else {
							if(nLen == 0) {
								// Box consists of the rest of the stream
								m_nXLBox = 8 + Stream.Size() - Stream.Tell();
							} else {
								// No 64bit length present
								m_nXLBox = nLen;
							}
							m_nLDBox = m_nXLBox - 8;
						}
						if(Stream.GetError() == NCS_SUCCESS) {
							m_nDBoxOffset = Stream.Tell();
							// The type matches, or we don't care.  Unmark the stream.
							Stream.UnMark();
							// Got the box, so set the flag indicating we have it.
							m_bHaveBox = true;
						}
					}
				}
			}
			Error = Stream.GetError();
		}
	}
	return(Error);
}
// 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;
}