@@ -72,9 +72,17 @@ void IWriter.CreateHeader(int pointCount)
7272 // create header data file
7373 // write header v2 : 34 bytes
7474 byte [ ] magic = new byte [ ] { 0x75 , 0x63 , 0x70 , 0x63 } ; // ucpc
75- writerHeaderV2 . Write ( magic ) ; // 4b
76- writerHeaderV2 . Write ( ( byte ) 2 ) ; // 1b
77- writerHeaderV2 . Write ( importSettings . readRGB ) ; // 1b
75+ writerHeaderV2 . Write ( magic ) ; // 4b magic
76+ if ( importSettings . packColors == true )
77+ {
78+ writerHeaderV2 . Write ( ( byte ) 3 ) ; // 1b version
79+ writerHeaderV2 . Write ( false ) ; // 1b contains RGB
80+ }
81+ else
82+ {
83+ writerHeaderV2 . Write ( ( byte ) 2 ) ; // 1b version
84+ writerHeaderV2 . Write ( true ) ; // 1b contains RGB
85+ }
7886
7987 if ( importSettings . skipPoints == true )
8088 {
@@ -98,18 +106,45 @@ void IWriter.CreateHeader(int pointCount)
98106 bsHeaderV2 . Dispose ( ) ;
99107 }
100108
109+ float prev_x , prev_y , prev_z ;
101110 void IWriter . WriteXYZ ( float x , float y , float z )
102111 {
103- writerPoints . Write ( x ) ;
104- writerPoints . Write ( y ) ;
105- writerPoints . Write ( z ) ;
112+ if ( importSettings . packColors == true )
113+ {
114+ prev_x = x ;
115+ prev_y = y ;
116+ prev_z = z ;
117+ }
118+ else
119+ {
120+ writerPoints . Write ( x ) ;
121+ writerPoints . Write ( y ) ;
122+ writerPoints . Write ( z ) ;
123+ }
124+
106125 }
107126
108127 void IWriter . WriteRGB ( float r , float g , float b )
109128 {
110- writerColorsV2 . Write ( r ) ;
111- writerColorsV2 . Write ( g ) ;
112- writerColorsV2 . Write ( b ) ;
129+ if ( importSettings . packColors == true )
130+ {
131+ // pack red and x
132+ r = Tools . SuperPacker ( r * 0.98f , prev_x , 1024 ) ; // NOTE fixed for now, until update shaders and header to contain packmagic value
133+ // pack green and y
134+ g = Tools . SuperPacker ( g * 0.98f , prev_y , 1024 ) ;
135+ // pack blue and z
136+ b = Tools . SuperPacker ( b * 0.98f , prev_z , 1024 ) ;
137+ writerPoints . Write ( r ) ;
138+ writerPoints . Write ( g ) ;
139+ writerPoints . Write ( b ) ;
140+ }
141+ else
142+ {
143+ writerColorsV2 . Write ( r ) ;
144+ writerColorsV2 . Write ( g ) ;
145+ writerColorsV2 . Write ( b ) ;
146+
147+ }
113148 }
114149
115150 void IWriter . Randomize ( )
@@ -150,42 +185,49 @@ void IWriter.Randomize()
150185 writerPoints . Write ( tempFloats [ i ] ) ;
151186 }
152187
153- // new files for colors
154- writerColorsV2 . Flush ( ) ;
155- bsColorsV2 . Flush ( ) ;
156- writerColorsV2 . Close ( ) ;
157- bsColorsV2 . Dispose ( ) ;
158-
159- Console . WriteLine ( "Randomizing " + pointCount + " colors..." ) ;
160-
161- tempBytes = null ;
162- using ( FileStream fs = File . Open ( colorsTempFile , FileMode . Open , FileAccess . Read , FileShare . None ) )
163- using ( BufferedStream bs = new BufferedStream ( fs ) )
164- using ( BinaryReader binaryReader = new BinaryReader ( bs ) )
188+ if ( importSettings . packColors == true )
165189 {
166- tempBytes = binaryReader . ReadBytes ( pointCount * 4 * 3 ) ;
167- }
168-
169- tempFloats = new float [ pointCount * 3 ] ;
170190
171- // convert to float array, TODO no need if can output writeallbytes
172- vectorPointer = GCHandle . Alloc ( tempFloats , GCHandleType . Pinned ) ;
173- pV = vectorPointer . AddrOfPinnedObject ( ) ;
174- Marshal . Copy ( tempBytes , 0 , pV , pointCount * 4 * 3 ) ;
175- vectorPointer . Free ( ) ;
176-
177- // actual point randomization
178- Tools . ShuffleXYZ ( Tools . rnd , ref tempFloats ) ;
179-
180- // create new file on top, seek didnt work?
181- bsColorsV2 = new BufferedStream ( new FileStream ( colorsTempFile , FileMode . Create , FileAccess . ReadWrite , FileShare . Read ) ) ;
182- writerColorsV2 = new BinaryWriter ( bsColorsV2 ) ;
191+ }
192+ else
193+ {
194+ // new files for colors
195+ writerColorsV2 . Flush ( ) ;
196+ bsColorsV2 . Flush ( ) ;
197+ writerColorsV2 . Close ( ) ;
198+ bsColorsV2 . Dispose ( ) ;
199+
200+ Console . WriteLine ( "Randomizing " + pointCount + " colors..." ) ;
201+
202+ tempBytes = null ;
203+ using ( FileStream fs = File . Open ( colorsTempFile , FileMode . Open , FileAccess . Read , FileShare . None ) )
204+ using ( BufferedStream bs = new BufferedStream ( fs ) )
205+ using ( BinaryReader binaryReader = new BinaryReader ( bs ) )
206+ {
207+ tempBytes = binaryReader . ReadBytes ( pointCount * 4 * 3 ) ;
208+ }
209+
210+ tempFloats = new float [ pointCount * 3 ] ;
211+
212+ // convert to float array, TODO no need if can output writeallbytes
213+ vectorPointer = GCHandle . Alloc ( tempFloats , GCHandleType . Pinned ) ;
214+ pV = vectorPointer . AddrOfPinnedObject ( ) ;
215+ Marshal . Copy ( tempBytes , 0 , pV , pointCount * 4 * 3 ) ;
216+ vectorPointer . Free ( ) ;
217+
218+ // actual point randomization
219+ Tools . ShuffleXYZ ( Tools . rnd , ref tempFloats ) ;
220+
221+ // create new file on top, seek didnt work?
222+ bsColorsV2 = new BufferedStream ( new FileStream ( colorsTempFile , FileMode . Create , FileAccess . ReadWrite , FileShare . Read ) ) ;
223+ writerColorsV2 = new BinaryWriter ( bsColorsV2 ) ;
183224
184- // TODO why not use writeallbytes? check 2gb file limit then use that
225+ // TODO why not use writeallbytes? check 2gb file limit then use that
185226
186- for ( int i = 0 ; i < pointCount * 3 ; i ++ )
187- {
188- writerColorsV2 . Write ( tempFloats [ i ] ) ;
227+ for ( int i = 0 ; i < pointCount * 3 ; i ++ )
228+ {
229+ writerColorsV2 . Write ( tempFloats [ i ] ) ;
230+ }
189231 }
190232 }
191233
@@ -219,7 +261,14 @@ void IWriter.Save(int fileIndex)
219261
220262 void IWriter . Cleanup ( int fileIndex )
221263 {
222- Console . WriteLine ( "Combining files: " + Path . GetFileName ( headerTempFile ) + "," + Path . GetFileName ( pointsTempFile ) + "m" + Path . GetFileName ( colorsTempFile ) ) ;
264+ if ( importSettings . packColors == true )
265+ {
266+ Console . WriteLine ( "Combining files: " + Path . GetFileName ( headerTempFile ) + "," + Path . GetFileName ( pointsTempFile ) ) ;
267+ }
268+ else
269+ {
270+ Console . WriteLine ( "Combining files: " + Path . GetFileName ( headerTempFile ) + "," + Path . GetFileName ( pointsTempFile ) + "," + Path . GetFileName ( colorsTempFile ) ) ;
271+ }
223272 Console . ForegroundColor = ConsoleColor . Green ;
224273 Console . WriteLine ( "Output: " + importSettings . outputFile ) ;
225274 Console . ForegroundColor = ConsoleColor . White ;
@@ -232,8 +281,33 @@ void IWriter.Cleanup(int fileIndex)
232281 colorsTempFile = colorsTempFile . Replace ( "/" , "\\ " ) ;
233282
234283 // combine files using commandline binary append
235- var outputFile = importSettings . outputFile + Path . GetFileNameWithoutExtension ( importSettings . inputFiles [ fileIndex ] ) + ".ucpc" ;
236- var args = "/C copy /b " + sep + headerTempFile + sep + "+" + sep + pointsTempFile + sep + "+" + sep + colorsTempFile + sep + " " + sep + outputFile + sep ;
284+ string outputFile = "" ;
285+ if ( Directory . Exists ( importSettings . outputFile ) ) // its output folder, take filename from source
286+ {
287+ outputFile = importSettings . outputFile + Path . GetFileNameWithoutExtension ( importSettings . inputFiles [ fileIndex ] ) + ".ucpc" ;
288+ }
289+ else // its not folder
290+ {
291+ // its filename with extension, use that
292+ if ( Path . GetExtension ( importSettings . outputFile ) . ToLower ( ) == ".ucpc" )
293+ {
294+ outputFile = importSettings . outputFile ;
295+ }
296+ else // its filename without extension
297+ {
298+ outputFile = importSettings . outputFile + ".ucpc" ;
299+ }
300+ }
301+
302+ string args = "" ;
303+ if ( importSettings . packColors == true )
304+ {
305+ args = "/C copy /b " + sep + headerTempFile + sep + "+" + sep + pointsTempFile + sep + " " + sep + outputFile + sep ;
306+ }
307+ else // non packed
308+ {
309+ args = "/C copy /b " + sep + headerTempFile + sep + "+" + sep + pointsTempFile + sep + "+" + sep + colorsTempFile + sep + " " + sep + outputFile + sep ;
310+ }
237311 Process proc = new Process ( ) ;
238312 proc . StartInfo . FileName = "CMD.exe" ;
239313 proc . StartInfo . Arguments = args ;
@@ -242,9 +316,9 @@ void IWriter.Cleanup(int fileIndex)
242316 proc . WaitForExit ( ) ;
243317
244318 Console . WriteLine ( "Deleting temporary files: " + Path . GetFileName ( headerTempFile ) + "," + Path . GetFileName ( pointsTempFile ) + "," + Path . GetFileName ( colorsTempFile ) ) ;
245- File . Delete ( headerTempFile ) ;
246- File . Delete ( pointsTempFile ) ;
247- File . Delete ( colorsTempFile ) ;
319+ if ( File . Exists ( headerTempFile ) ) File . Delete ( headerTempFile ) ;
320+ if ( File . Exists ( pointsTempFile ) ) File . Delete ( pointsTempFile ) ;
321+ if ( File . Exists ( colorsTempFile ) ) File . Delete ( colorsTempFile ) ;
248322 }
249323
250324 void IWriter . Close ( )
0 commit comments