github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/jxr/jxrlib/jxrtestlib/JXRTest.c (about)

     1  //*@@@+++@@@@******************************************************************
     2  //
     3  // Copyright Microsoft Corp.
     4  // All rights reserved.
     5  // 
     6  // Redistribution and use in source and binary forms, with or without
     7  // modification, are permitted provided that the following conditions are met:
     8  // 
     9  // Redistributions of source code must retain the above copyright notice,
    10  //   this list of conditions and the following disclaimer.
    11  // Redistributions in binary form must reproduce the above copyright notice,
    12  //   this list of conditions and the following disclaimer in the documentation
    13  //   and/or other materials provided with the distribution.
    14  // 
    15  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    19  // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    20  // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    21  // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    22  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    23  // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    24  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  // POSSIBILITY OF SUCH DAMAGE.
    26  //
    27  //*@@@---@@@@******************************************************************
    28  
    29  #include <JXRTest.h>
    30  
    31  //================================================================
    32  const PKIID IID_PKImagePnmEncode = 102;
    33  const PKIID IID_PKImageBmpEncode = 103;
    34  const PKIID IID_PKImageTifEncode = 104;
    35  const PKIID IID_PKImageHdrEncode = 105;
    36  const PKIID IID_PKImageIyuvEncode = 106;
    37  const PKIID IID_PKImageYuv422Encode = 107;
    38  const PKIID IID_PKImageYuv444Encode = 108;
    39  
    40  const PKIID IID_PKImageBmpDecode = 202;
    41  const PKIID IID_PKImagePnmDecode = 203;
    42  const PKIID IID_PKImageTifDecode = 204;
    43  const PKIID IID_PKImageHdrDecode = 205;
    44  const PKIID IID_PKImageIyuvDecode = 206;
    45  const PKIID IID_PKImageYuv422Decode = 207;
    46  const PKIID IID_PKImageYuv444Decode = 208;
    47  
    48  //================================================================
    49  // Misc supporting functions
    50  //================================================================
    51  extern int PKStrnicmp(const char* s1, const char* s2, size_t c);
    52  
    53  //----------------------------------------------------------------
    54  typedef struct tagPKIIDInfo
    55  {
    56      const char* szExt;
    57      const PKIID* pIIDEnc;
    58      const PKIID* pIIDDec;
    59  } PKIIDInfo;
    60  
    61  static ERR GetTestInfo(const char* szExt, const PKIIDInfo** ppInfo)
    62  {
    63      ERR err = WMP_errSuccess;
    64  
    65      static PKIIDInfo iidInfo[] = {
    66          {".bmp", &IID_PKImageBmpEncode, &IID_PKImageBmpDecode},
    67          {".ppm", &IID_PKImagePnmEncode, &IID_PKImagePnmDecode},
    68          {".pgm", &IID_PKImagePnmEncode, &IID_PKImagePnmDecode},
    69          {".pnm", &IID_PKImagePnmEncode, &IID_PKImagePnmDecode},
    70          {".pfm", &IID_PKImagePnmEncode, &IID_PKImagePnmDecode},
    71          {".tif", &IID_PKImageTifEncode, &IID_PKImageTifDecode},
    72          {".hdr", &IID_PKImageHdrEncode, &IID_PKImageHdrDecode},
    73          {".iyuv", &IID_PKImageIyuvEncode, &IID_PKImageIyuvDecode},
    74          {".yuv422", &IID_PKImageYuv422Encode, &IID_PKImageYuv422Decode},
    75          {".yuv444", &IID_PKImageYuv444Encode, &IID_PKImageYuv444Decode},
    76      };
    77      size_t i = 0;
    78  
    79      *ppInfo = NULL;
    80      for (i = 0; i < sizeof2(iidInfo); ++i)
    81      {
    82          if (0 == PKStrnicmp(szExt, iidInfo[i].szExt, strlen(iidInfo[i].szExt)))
    83          {
    84              *ppInfo = &iidInfo[i];
    85              goto Cleanup;
    86          }
    87      }
    88  
    89      Call(WMP_errUnsupportedFormat);
    90  
    91  Cleanup:
    92      return err;
    93  }
    94  
    95  ERR GetTestEncodeIID(const char* szExt, const PKIID** ppIID)
    96  {
    97      ERR err = WMP_errSuccess;
    98  
    99      const PKIIDInfo* pInfo = NULL;
   100  
   101      Call(GetTestInfo(szExt, &pInfo));
   102      *ppIID = pInfo->pIIDEnc;
   103  
   104  Cleanup:
   105      return err;
   106  }
   107  
   108  ERR GetTestDecodeIID(const char* szExt, const PKIID** ppIID)
   109  {
   110      ERR err = WMP_errSuccess;
   111  
   112      const PKIIDInfo* pInfo = NULL;
   113  
   114      Call(GetTestInfo(szExt, &pInfo));
   115      *ppIID = pInfo->pIIDDec;
   116  
   117  Cleanup:
   118      return err;
   119  }
   120  
   121  
   122  //================================================================
   123  // PKTestFactory
   124  //================================================================
   125  ERR PKTestFactory_CreateCodec(const PKIID* iid, void** ppv)
   126  {
   127      ERR err = WMP_errSuccess;
   128  
   129      if (IID_PKImageBmpEncode == *iid)
   130      {
   131          Call(PKImageEncode_Create_BMP((PKImageEncode**)ppv));
   132      }
   133      else if (IID_PKImagePnmEncode == *iid)
   134      {
   135          Call(PKImageEncode_Create_PNM((PKImageEncode**)ppv));
   136      }
   137      else if (IID_PKImageTifEncode == *iid)
   138      {
   139          Call(PKImageEncode_Create_TIF((PKImageEncode**)ppv));
   140      }
   141      else if (IID_PKImageHdrEncode == *iid)
   142      {
   143          Call(PKImageEncode_Create_HDR((PKImageEncode**)ppv));
   144      }
   145      else if (IID_PKImageIyuvEncode == *iid)
   146      {
   147          Call(PKImageEncode_Create_IYUV((PKImageEncode**)ppv));
   148      }
   149      else if (IID_PKImageYuv422Encode == *iid)
   150      {
   151          Call(PKImageEncode_Create_YUV422((PKImageEncode**)ppv));
   152      }
   153      else if (IID_PKImageYuv444Encode == *iid)
   154      {
   155          Call(PKImageEncode_Create_YUV444((PKImageEncode**)ppv));
   156      }
   157  
   158      else if (IID_PKImageBmpDecode == *iid)
   159      {
   160          Call(PKImageDecode_Create_BMP((PKTestDecode**)ppv));
   161      }
   162      else if (IID_PKImagePnmDecode == *iid)
   163      {
   164          Call(PKImageDecode_Create_PNM((PKTestDecode**)ppv));
   165      }
   166      else if (IID_PKImageTifDecode == *iid)
   167      {
   168          Call(PKImageDecode_Create_TIF((PKTestDecode**)ppv));
   169      }
   170      else if (IID_PKImageHdrDecode == *iid)
   171      {
   172          Call(PKImageDecode_Create_HDR((PKTestDecode**)ppv));
   173      }
   174      else if (IID_PKImageIyuvDecode == *iid)
   175      {
   176          Call(PKImageDecode_Create_IYUV((PKTestDecode**)ppv));
   177      }
   178      else if (IID_PKImageYuv422Decode == *iid)
   179      {
   180          Call(PKImageDecode_Create_YUV422((PKTestDecode**)ppv));
   181      }
   182      else if (IID_PKImageYuv444Decode == *iid)
   183      {
   184          Call(PKImageDecode_Create_YUV444((PKTestDecode**)ppv));
   185      }
   186  
   187      else
   188      {
   189          Call(WMP_errUnsupportedFormat);
   190      }
   191  
   192  Cleanup:
   193      return err;
   194  }
   195  
   196  ERR PKTestFactory_CreateDecoderFromFile(const char* szFilename, PKImageDecode** ppDecoder)
   197  {
   198      ERR err = WMP_errSuccess;
   199  
   200      char *pExt = NULL;
   201      PKIID* pIID = NULL;
   202  
   203      struct WMPStream* pStream = NULL;
   204      PKImageDecode* pDecoder = NULL;
   205  
   206      // get file extension
   207      pExt = strrchr(szFilename, '.');
   208      FailIf(NULL == pExt, WMP_errUnsupportedFormat);
   209  
   210      // get decode PKIID
   211      Call(GetTestDecodeIID(pExt, &pIID));
   212  
   213      // create stream
   214      Call(CreateWS_File(&pStream, szFilename, "rb"));
   215  
   216      // Create decoder
   217      Call(PKTestFactory_CreateCodec(pIID, ppDecoder));
   218      pDecoder = *ppDecoder;
   219  
   220      // attach stream to decoder
   221      Call(pDecoder->Initialize(pDecoder, pStream));
   222      pDecoder->fStreamOwner = !0;
   223  
   224  Cleanup:
   225      return err;
   226  }
   227  
   228  ERR PKCreateTestFactory(PKCodecFactory** ppCFactory, U32 uVersion)
   229  {
   230      ERR err = WMP_errSuccess;
   231      PKCodecFactory* pCFactory = NULL;
   232  
   233      UNREFERENCED_PARAMETER( uVersion );
   234  
   235      Call(PKAlloc(ppCFactory, sizeof(**ppCFactory)));
   236      pCFactory = *ppCFactory;
   237  
   238      pCFactory->CreateCodec = PKTestFactory_CreateCodec;
   239      pCFactory->CreateDecoderFromFile = PKTestFactory_CreateDecoderFromFile;
   240      pCFactory->CreateFormatConverter = PKCodecFactory_CreateFormatConverter;
   241      pCFactory->Release = PKCreateCodecFactory_Release;
   242  
   243  Cleanup:
   244      return err;
   245  }
   246  
   247  
   248  //================================================================
   249  // PKTestDecode
   250  //================================================================
   251  ERR PKTestDecode_Initialize(
   252      PKTestDecode* pID,
   253      struct WMPStream* pStream)
   254  {
   255      ERR err = WMP_errSuccess;
   256  
   257      pID->pStream = pStream;
   258      pID->guidPixFormat = GUID_PKPixelFormatDontCare;
   259      pID->fResX = 96;
   260      pID->fResY = 96;
   261      pID->cFrame = 1;
   262  
   263      Call(pID->pStream->GetPos(pID->pStream, &pID->offStart));
   264  
   265  Cleanup:
   266      return WMP_errSuccess;
   267  }
   268  
   269  ERR PKTestDecode_Copy(
   270      PKTestDecode* pID,
   271      const PKRect* pRect,
   272      U8* pb,
   273      U32 cbStride)
   274  {
   275      UNREFERENCED_PARAMETER( pID );
   276      UNREFERENCED_PARAMETER( pRect );
   277      UNREFERENCED_PARAMETER( pb );
   278      UNREFERENCED_PARAMETER( cbStride );
   279  
   280      return WMP_errAbstractMethod;
   281  }
   282  
   283  ERR PKTestDecode_Release(
   284      PKTestDecode** ppID)
   285  {
   286      PKTestDecode* pID = *ppID;
   287  
   288      pID->fStreamOwner && pID->pStream->Close(&pID->pStream);
   289  
   290      return PKFree(ppID);
   291  }
   292  
   293  ERR PKTestDecode_Create(
   294      PKTestDecode** ppID)
   295  {
   296      ERR err = WMP_errSuccess;
   297      PKTestDecode* pID = NULL;
   298  
   299      Call(PKAlloc(ppID, sizeof(**ppID)));
   300  
   301      pID = *ppID;
   302      pID->Initialize = PKTestDecode_Initialize;
   303      pID->GetPixelFormat = PKImageDecode_GetPixelFormat;
   304      pID->GetSize = PKImageDecode_GetSize;
   305      pID->GetResolution = PKImageDecode_GetResolution;
   306      pID->GetColorContext = PKImageDecode_GetColorContext;
   307      pID->GetDescriptiveMetadata = PKImageDecode_GetDescriptiveMetadata;
   308      pID->Copy = PKTestDecode_Copy;
   309      pID->GetFrameCount = PKImageDecode_GetFrameCount;
   310      pID->SelectFrame = PKImageDecode_SelectFrame;
   311      pID->Release = PKTestDecode_Release;
   312  
   313  Cleanup:
   314      return err;
   315  }