imagingmodules/jp2kcodec/Src/JP2KStreamReader.cpp
changeset 0 469c91dae73b
equal deleted inserted replaced
-1:000000000000 0:469c91dae73b
       
     1 /*
       
     2 * Copyright (c) 2003, 2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  TJ2kStreamReader class is to implement the
       
    15 *                MJ2kPacketHeaderReader interface for reading the
       
    16 *                packet header and packet body from the stream
       
    17 *                buffer provided by ICL framework.
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include <icl/imageprocessor.h>
       
    24 #include <icl/imagecodec.h>
       
    25 #include <icl/imagecodecdata.h>
       
    26 #include "JP2KImageUtils.h"
       
    27 #include "JP2KFormat.h"
       
    28 #include "JP2KStreamReader.h"
       
    29 #include "JP2KCodec.h"
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 
       
    33 // EXTERNAL FUNCTION PROTOTYPES  
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 // MACROS
       
    38 
       
    39 // LOCAL CONSTANTS AND MACROS
       
    40 
       
    41 // MODULE DATA STRUCTURES
       
    42 
       
    43 // LOCAL FUNCTION PROTOTYPES
       
    44 
       
    45 // FORWARD DECLARATIONS
       
    46 
       
    47 // ============================ MEMBER FUNCTIONS ===============================
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // TJ2kStreamReader::UpdateMainHeader
       
    51 // Update the codestream length read and offset of SOT marker
       
    52 // (other items were commented in a header).
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 void TJ2kStreamReader::UpdateMainHeader()
       
    56     {
       
    57     if ( iCSLength )
       
    58         {
       
    59         // Codestream left
       
    60         iCSLength -= ( iPtr - iPtrStartMarker );
       
    61         }
       
    62     iStartSOT += ( iPtr - iPtrStartMarker );
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // TJ2kStreamReader::UpdateTileHeader
       
    67 // Update the codestream length read and length of data consumed
       
    68 // (other items were commented in a header).
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 void TJ2kStreamReader::UpdateTileHeader()
       
    72     {
       
    73     if ( iCSLength )
       
    74         {
       
    75         // Codestream left
       
    76         iCSLength -= ( iPtr - iPtrStartMarker );
       
    77         }
       
    78     iDataUsed += ( iPtr - iPtrStartMarker );
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // TJ2kStreamReader::ReadEPHMarker
       
    83 // Try to consume the EPH marker if there is one
       
    84 // (other items were commented in a header).
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 TUint8 TJ2kStreamReader::ReadEPHMarker()
       
    88     {
       
    89     if ( ( iPtrEnd - iPtr ) < 2 )
       
    90         {
       
    91         // EPH marker should be tried on next read
       
    92         iTryEPHMarker = ETrue;
       
    93         }
       
    94     else
       
    95         {
       
    96         iTryEPHMarker = EFalse;
       
    97 
       
    98         // Try to consume the EPH marker
       
    99         if ( PtrReadUtil::ReadBigEndianUint16( iPtr ) == KEPH )
       
   100             {
       
   101             iPtr += 2;
       
   102             }
       
   103         }
       
   104     return EFalse;
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // TJ2kStreamReader::ReadBit
       
   109 // Read a bit from the packet header stream.
       
   110 // (other items were commented in a header).
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TUint8 TJ2kStreamReader::ReadBit( TUint8& aBit )
       
   114     {
       
   115     if ( iPos == 0 )
       
   116         {
       
   117         if ( iPtr < iPtrEnd )
       
   118             {
       
   119             // Buffer contains some packet header data
       
   120             iData = *iPtr++;
       
   121             iPos = iNextPos;
       
   122             if ( iNextPos == 0x08 && iData == 0xff )
       
   123                 {
       
   124                 iNextPos = 0x07;
       
   125                 }
       
   126             else
       
   127                 {
       
   128                 iNextPos = 0x08;
       
   129                 }
       
   130             }
       
   131         else
       
   132             {
       
   133             // No more data
       
   134             return ETrue;
       
   135             }
       
   136         }
       
   137     aBit = (TUint8)( ( iData >> ( --iPos ) ) & 0x01 );
       
   138     return EFalse;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // TJ2kStreamReader::ReadBits
       
   143 // Read some bits from the packet header stream.
       
   144 // (other items were commented in a header).
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 TUint8 TJ2kStreamReader::ReadBits( TUint8& aBit, TUint8 aBitLen )
       
   148     {
       
   149     aBit = (TUint8)0;
       
   150     TUint8 bit;
       
   151     for ( TInt8 index = (TInt8)( aBitLen - 1 ); index >= 0; --index )
       
   152         {
       
   153         if ( !ReadBit( bit ) )
       
   154             {
       
   155             aBit |= ( bit << index );
       
   156             }
       
   157         else
       
   158             {
       
   159             return ETrue;
       
   160             }
       
   161         }
       
   162     return EFalse;
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // TJ2kStreamReader::ReadBits
       
   167 // Read some bits from the packet header stream.
       
   168 // (other items were commented in a header).
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 TUint8 TJ2kStreamReader::ReadBits( TUint32& aBit, TUint8 aBitLen )
       
   172     {
       
   173     aBit = (TUint32)0;
       
   174     TUint8 bit = 0;
       
   175     for ( TInt8 index = ( TInt8 )( aBitLen - 1 ); index >= 0; --index )
       
   176         {
       
   177         if ( !ReadBit( bit ) )
       
   178             {
       
   179             aBit |= ( bit << index );
       
   180             }
       
   181         else
       
   182             {
       
   183             return ETrue;
       
   184             }
       
   185         }
       
   186     return EFalse;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // TJ2kStreamReader::StartReadBit
       
   191 // Start reading from packet header stream.
       
   192 // (other items were commented in a header).
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 TUint8 TJ2kStreamReader::StartReadBit()
       
   196     {
       
   197     if ( iAlign )
       
   198         {
       
   199         // Byte alignment is required
       
   200         ++iPtr;
       
   201         iAlign = 0;
       
   202         }
       
   203     if ( iPtr < iPtrEnd )
       
   204         {
       
   205         // Buffer contains some packet header data
       
   206         iData = *iPtr++;
       
   207         iPos = iNextPos = 0x08;
       
   208         if ( iData == 0xff )
       
   209             {
       
   210             iNextPos = 0x07;
       
   211             }
       
   212 
       
   213         return EFalse;
       
   214         }
       
   215     return ETrue;
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // TJ2kStreamReader::AlignReader
       
   220 // Align the stream to the next byte boundary if necessary.
       
   221 // (other items were commented in a header).
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void TJ2kStreamReader::AlignReader()
       
   225     {
       
   226     iAlign = 0;
       
   227     if ( iNextPos == 0x07 )
       
   228         {
       
   229         if ( iPtr < iPtrEnd )
       
   230             {
       
   231             ++iPtr;
       
   232             }
       
   233         else
       
   234             {
       
   235             // byte alignment is required on next read
       
   236             iAlign = 1;
       
   237             }
       
   238         }
       
   239     iData = iPos = iNextPos = 0;
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // TJ2kStreamReader::ReadSOPMarker
       
   244 // Try to consume the SOP marker if there is one
       
   245 // (other items were commented in a header).
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 TUint8 TJ2kStreamReader::ReadSOPMarker()
       
   249     {
       
   250     if ( ( iPtrEnd - iPtr ) < 6 )
       
   251         {
       
   252         return ETrue;
       
   253         }
       
   254     else
       
   255         {
       
   256         if ( PtrReadUtil::ReadBigEndianUint16( iPtr ) == KSOP )
       
   257             {
       
   258             iPtr += 2;
       
   259             if ( PtrReadUtil::ReadBigEndianUint16( iPtr ) == KSOP_LEN )
       
   260                 {
       
   261                 // SOP marker found
       
   262                 iPtr += 4;
       
   263                 }
       
   264             else
       
   265                 {
       
   266                 // SOP marker not found, backup the iterator
       
   267                 iPtr -= 2;
       
   268                 }
       
   269             }
       
   270         return EFalse;
       
   271         }
       
   272     }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // TJ2kStreamReader::TryReadEPHMarker
       
   276 // Try to consume the EPH marker after failure on previous try
       
   277 // (other items were commented in a header).
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 TUint8 TJ2kStreamReader::TryReadEPHMarker()
       
   281     {
       
   282     if ( iTryEPHMarker )
       
   283         {
       
   284         if ( ( iPtrEnd - iPtr ) < 2 )
       
   285             {
       
   286             return ETrue;
       
   287             }
       
   288         else
       
   289             {
       
   290             if ( PtrReadUtil::ReadBigEndianUint16( iPtr ) == KEPH )
       
   291                 {
       
   292                 iPtr += 2;
       
   293                 }
       
   294             iTryEPHMarker = EFalse;
       
   295             }
       
   296         }
       
   297     return EFalse;
       
   298     }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // TJ2kStreamReader::TryReAlignReader
       
   302 // Try to align to byte boundary after failure on previous try
       
   303 // (other items were commented in a header).
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 TUint8 TJ2kStreamReader::TryReAlignReader()
       
   307     {
       
   308     if ( iAlign )
       
   309         {
       
   310         if ( ( iPtrEnd - iPtr ) < 1 )
       
   311             {
       
   312             return ETrue;
       
   313             }
       
   314         else
       
   315             {
       
   316             ++iPtr;
       
   317             iAlign = 0;
       
   318             }
       
   319         }
       
   320     return EFalse;
       
   321     }
       
   322 
       
   323