1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_TexResource.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/lyt/lyt_TexResource.h>
21 
22 namespace nw
23 {
24 namespace lyt
25 {
26 
Set(void * pTexRes,u32 size)27 bool TexResource::Set(void* pTexRes, u32 size)
28 {
29     const int RESOURCE_ALIGNMENT = 128;
30     const int BLOCK_ALIGNMENT = 4;
31     const u32 MIN_SIZE =
32         sizeof(ut::BinaryFileHeader) +
33         sizeof(res::Image) +
34         sizeof(res::ImageSize);
35 
36     do
37     {
38         NW_NULL_ASSERT(pTexRes);
39         if (pTexRes == NULL)
40         {
41             break;
42         }
43 
44         NW_ASSERT((u32)(pTexRes) % RESOURCE_ALIGNMENT == 0);
45         if (!((u32)(pTexRes) % RESOURCE_ALIGNMENT == 0))
46         {
47             break;
48         }
49 
50         NW_ASSERT(size % BLOCK_ALIGNMENT == 0);
51         if (!(size % BLOCK_ALIGNMENT == 0))
52         {
53             break;
54         }
55 
56         NW_ASSERT(size > MIN_SIZE);
57         if (!(size > MIN_SIZE))
58         {
59             break;
60         }
61 
62         // ファイルの末尾 4 バイトがイメージサイズ。
63         res::ImageSize* pImageSize =
64             internal::ConvertOffsToPtr<res::ImageSize>(
65                 pTexRes,
66                 size - sizeof(res::ImageSize));
67 
68         u32 imageSize = pImageSize->imageSize;
69 
70         NW_ASSERT(imageSize < size);
71         if (!(imageSize < size))
72         {
73             break;
74         }
75 
76         ut::BinaryFileHeader* pFileHead =
77             internal::ConvertOffsToPtr<ut::BinaryFileHeader>(
78                 pTexRes,
79                 ut::RoundUp(imageSize, BLOCK_ALIGNMENT));
80 
81         if (!ut::IsValidBinaryFile(pFileHead, res::FILESIGNATURE_CLIM, res::BinaryFileFormatVersion))
82         {
83             break;
84         }
85 
86         res::Image* pImage = 0;
87 
88         ut::BinaryBlockHeader* pBlockHead = NULL;
89         for (int i = 0; i < pFileHead->dataBlocks; ++i)
90         {
91             pBlockHead = ut::GetNextBinaryBlockHeader(pFileHead, pBlockHead);
92             NW_NULL_ASSERT(pBlockHead);
93 
94             ut::SigWord kind = pBlockHead->kind;
95             switch (kind)
96             {
97             case res::DATABLOCKKIND_IMAGE:
98                 pImage = reinterpret_cast<res::Image *>(pBlockHead);
99                 break;
100             }
101         }
102 
103         NW_NULL_ASSERT(pImage);
104         if (pImage == NULL)
105         {
106             break;
107         }
108 
109         m_pTop = const_cast<void*>(pTexRes);
110         m_pImage = const_cast<res::Image*>(pImage);
111         m_pImageSize = const_cast<res::ImageSize*>(pImageSize);
112 
113         return true;
114 
115     } while (false);
116 
117     m_pTop = NULL;
118     m_pImage = NULL;
119     m_pImageSize = NULL;
120 
121     return false;
122 }
123 
124 } // namespace lyt
125 } // namespace nw
126