From 90c0e78a66627eeeaf92d046f332f462586d7ea9 Mon Sep 17 00:00:00 2001 From: "meffi@lab313.ru" Date: Thu, 10 Apr 2014 23:22:23 +0000 Subject: [PATCH] Added ability to extract all TIMs to PNG; Now PNG's name includes CLUT index. --- tim2view.dproj | 2 +- tim2view.res | Bin 298600 -> 298744 bytes units/uCDIMAGE.pas | 15 +++-- units/uCommon.pas | 7 ++- units/uMain.dfm | 57 +++++++++++++---- units/uMain.pas | 111 +++++++++++++++++++++++++-------- units/uScanThread.pas | 139 ++++++++++++++++++++++-------------------- units/uSettings.pas | 24 ++++---- units/uTIM.pas | 92 +++++++++++++++------------- 9 files changed, 278 insertions(+), 169 deletions(-) diff --git a/tim2view.dproj b/tim2view.dproj index 0733094..c4e3ce8 100644 --- a/tim2view.dproj +++ b/tim2view.dproj @@ -6,7 +6,7 @@ tim2view.dpr True Release - Win64 + Win32 3 Application diff --git a/tim2view.res b/tim2view.res index 2a0bfa4ca548313e8fb687616a4c53e28b66b959..acce4cd4d9f379aeb31c69fd547e369f065c28b0 100644 GIT binary patch delta 207 zcmW-Zy$%6U5QV?H6bjY03QHl;t2 Ts8NKYjjf5T6WheoCa 0); lvList.Enabled := cbbFiles.Enabled; - actExtractList.Enabled := cbbFiles.Enabled; + actExtractTIMs.Enabled := cbbFiles.Enabled; + actExtractPNGs.Enabled := cbbFiles.Enabled; actCloseFile.Enabled := cbbFiles.Enabled; actCloseFiles.Enabled := cbbFiles.Enabled; actReplaceTim.Enabled := (lvList.SelCount = 1); @@ -587,9 +643,14 @@ begin Result^.HEAD^.bBPP := NewBitMode; end; -function TfrmMain.SelTimName: string; +function TfrmMain.FormatTimName(const FileName: string; ListIdx_, BitMode: Integer): string; begin - Result := Format(cAutoExtractionTimFormat, [ExtractJustName(ScanRes.ScanFile), ListIdx + 1, TimIdx(ListIdx).BitMode]); + Result := Format(cAutoExtractionTimFormat, [ExtractJustName(FileName), ListIdx_ + 1, BitMode]); +end; + +function TfrmMain.FormatPngName(const FileName: string; ListIdx_, BitMode, Clut: Integer): string; +begin + Result := Format(cAutoExtractionPngFormat, [ExtractJustName(FileName), ListIdx_ + 1, BitMode, Clut + 1]); end; procedure TfrmMain.DrawSelClut; @@ -647,10 +708,10 @@ begin PNG^ := nil; end; - if cbbCLUT.Text = sThisTimHasNoClut then + (*if cbbCLUT.Text = sThisTimHasNoClut then Index := -1 else - Index := cbbCLUT.ItemIndex; + *)Index := cbbCLUT.ItemIndex; TimToPNG(TIM, Index, PNG, cbbTranspMode.ItemIndex); PNG.AssignTo(pbTim.Picture.Bitmap); diff --git a/units/uScanThread.pas b/units/uScanThread.pas index 5691d49..69ff85e 100644 --- a/units/uScanThread.pas +++ b/units/uScanThread.pas @@ -89,87 +89,91 @@ var begin Synchronize(StartScan); - pSrcFileStream := TFileStream.Create(pScanResult.ScanFile, fmOpenRead or - fmShareDenyWrite); - pSrcFileStream.Position := 0; + try + if pScanResult.IsImage then + pSectorBufferSize := cSectorBufferSize + else + pSectorBufferSize := cClearBufferSize; - if pScanResult.IsImage then - pSectorBufferSize := cSectorBufferSize - else - pSectorBufferSize := cClearBufferSize; + pClearBufferSize := cClearBufferSize; - pClearBufferSize := cClearBufferSize; + SectorBuffer := GetMemory(pSectorBufferSize); + ClearBuffer := GetMemory(pClearBufferSize); - SectorBuffer := GetMemory(pSectorBufferSize); - ClearBuffer := GetMemory(pClearBufferSize); + pSrcFileStream := TFileStream.Create(pScanResult.ScanFile, fmOpenRead or + fmShareDenyWrite); - TIM := CreateTIM; + pSrcFileStream.Position := 0; - pStatusText := sStatusBarScanningFile; - Synchronize(SetStatusText); + TIM := CreateTIM; - pRealBufSize := pSrcFileStream.Read(SectorBuffer^[0], pSectorBufferSize); - Inc(pFilePos, pRealBufSize); - ClearSectorBuffer(SectorBuffer, ClearBuffer); + pStatusText := sStatusBarScanningFile; + Synchronize(SetStatusText); - pScanFinished := False; - pTIMNumber := 0; + pRealBufSize := pSrcFileStream.Read(SectorBuffer^[0], pSectorBufferSize); + Inc(pFilePos, pRealBufSize); + ClearSectorBuffer(SectorBuffer, ClearBuffer); - while not pStopScan do - begin - if LoadTimFromBuf(ClearBuffer, TIM, pClearBufferPosition) then + pScanFinished := False; + pTIMNumber := 0; + + while not pStopScan do begin - if pScanResult.IsImage then - pTimPosition := pFilePos - pRealBufSize + - ((pClearBufferPosition - 1) div cSectorDataSize) * cSectorSize + - ((pClearBufferPosition - 1) mod cSectorDataSize) + cSectorInfoSize - else - pTimPosition := pFilePos - pRealBufSize + (pClearBufferPosition - 1); - - if pTimPosition >= pFileSize then Break; - - TIM^.dwTimPosition := pTimPosition; - Inc(pTIMNumber); - TIM^.dwTimNumber := pTIMNumber; - AddResult(TIM); - end; - - if pClearBufferPosition = (pClearBufferSize div 2) then - begin - if pScanFinished then Break; - - pScanFinished := (pFilePos = pFileSize); - pClearBufferPosition := 0; - Move(SectorBuffer^[pSectorBufferSize div 2], SectorBuffer^[0], pSectorBufferSize div 2); - - if pScanFinished then + if LoadTimFromBuf(ClearBuffer, TIM, pClearBufferPosition) then begin - if pRealBufSize >= (pSectorBufferSize div 2) then - // Need to check file size - pRealBufSize := pRealBufSize - (pSectorBufferSize div 2); - end - else - begin - pRealBufSize := pSrcFileStream.Read(SectorBuffer^[pSectorBufferSize div 2], pSectorBufferSize div 2); - Inc(pFilePos, pRealBufSize); - pRealBufSize := pRealBufSize + (pSectorBufferSize div 2); + if pScanResult.IsImage then + pTimPosition := pFilePos - pRealBufSize + + ((pClearBufferPosition - 1) div cSectorDataSize) * cSectorSize + + ((pClearBufferPosition - 1) mod cSectorDataSize) + cSectorInfoSize + else + pTimPosition := pFilePos - pRealBufSize + (pClearBufferPosition - 1); + + if pTimPosition >= pFileSize then Break; + + TIM^.dwTimPosition := pTimPosition; + Inc(pTIMNumber); + TIM^.dwTimNumber := pTIMNumber; + AddResult(TIM); end; - Synchronize(UpdateProgressBar); + if pClearBufferPosition = (pClearBufferSize div 2) then + begin + if pScanFinished then Break; - ClearSectorBuffer(SectorBuffer, ClearBuffer); + pScanFinished := (pFilePos = pFileSize); + pClearBufferPosition := 0; + Move(SectorBuffer^[pSectorBufferSize div 2], SectorBuffer^[0], pSectorBufferSize div 2); + + if pScanFinished then + begin + if pRealBufSize >= (pSectorBufferSize div 2) then + // Need to check file size + pRealBufSize := pRealBufSize - (pSectorBufferSize div 2); + end + else + begin + pRealBufSize := pSrcFileStream.Read(SectorBuffer^[pSectorBufferSize div 2], pSectorBufferSize div 2); + Inc(pFilePos, pRealBufSize); + pRealBufSize := pRealBufSize + (pSectorBufferSize div 2); + end; + + Synchronize(UpdateProgressBar); + + ClearSectorBuffer(SectorBuffer, ClearBuffer); + end; end; + finally + FreeTIM(TIM); + FreeMemory(SectorBuffer); + FreeMemory(ClearBuffer); + + pSrcFileStream.Free; + pStopScan := True; + pFilePos := 0; + pStatusText := ''; + + Synchronize(FinishScan); end; - FreeTIM(TIM); - FreeMemory(SectorBuffer); - FreeMemory(ClearBuffer); - - pSrcFileStream.Free; - pStopScan := True; - pFilePos := 0; - pStatusText := ''; - - Synchronize(FinishScan); end; procedure TScanThread.FinishScan; @@ -201,7 +205,8 @@ procedure TScanThread.StartScan; begin frmMain.cbbFiles.Enabled := False; frmMain.lvList.Enabled := False; - frmMain.actExtractList.Enabled := False; + frmMain.actExtractTIMs.Enabled := False; + frmMain.actExtractPNGs.Enabled := False; frmMain.pbProgress.Max := GetFileSizeAPI(pScanResult.ScanFile); frmMain.pbProgress.Position := 0; diff --git a/units/uSettings.pas b/units/uSettings.pas index 0623c6f..6ac9c24 100644 --- a/units/uSettings.pas +++ b/units/uSettings.pas @@ -35,19 +35,19 @@ unit uSettings; { TSettings } - procedure TCustomSettings.LoadFromFile() ; - var - Stream: TStream; - begin - if not FileExists(FFileName) then Exit; +procedure TCustomSettings.LoadFromFile() ; +var + Stream: TStream; +begin + if not FileExists(FFileName) then Exit; - Stream := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite) ; - try - LoadFromStream(Stream) ; - finally - Stream.Free; - end; - end; + try + Stream := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite) ; + LoadFromStream(Stream) ; + finally + Stream.Free; + end; +end; procedure TCustomSettings.LoadFromStream(const Stream: TStream) ; var diff --git a/units/uTIM.pas b/units/uTIM.pas index 8802b05..0cf0d76 100644 --- a/units/uTIM.pas +++ b/units/uTIM.pas @@ -372,56 +372,58 @@ var Sector: TCDSector; P, TIM_FULL_SECTORS: Integer; begin - sImageStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); + try + sImageStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); - TimSectorNumber := Position div cSectorSize + 1; - TimOffsetInSector := Position mod cSectorSize - cSectorInfoSize; - TimStartSectorPos := (TimSectorNumber - 1) * cSectorSize; - FirstPartSize := cSectorDataSize - TimOffsetInSector; + TimSectorNumber := Position div cSectorSize + 1; + TimOffsetInSector := Position mod cSectorSize - cSectorInfoSize; + TimStartSectorPos := (TimSectorNumber - 1) * cSectorSize; + FirstPartSize := cSectorDataSize - TimOffsetInSector; - New(TIM_BUF); - P := 0; + New(TIM_BUF); + P := 0; - if SIZE < FirstPartSize then - FirstPartSize := SIZE; + if SIZE < FirstPartSize then + FirstPartSize := SIZE; - sImageStream.Seek(TimStartSectorPos, soBeginning); - sImageStream.Read(Sector, cSectorSize); - - Move(Sector.dwData[TimOffsetInSector], TIM_BUF^[P], FirstPartSize); - Inc(P, FirstPartSize); - - Inc(TimStartSectorPos, cSectorSize); - sImageStream.Seek(TimStartSectorPos, soBeginning); - - TIM_FULL_SECTORS := (SIZE - P) div cSectorDataSize; - - while TIM_FULL_SECTORS > 0 do - begin + sImageStream.Seek(TimStartSectorPos, soBeginning); sImageStream.Read(Sector, cSectorSize); - Move(Sector.dwData[0], TIM_BUF^[P], cSectorDataSize); - Inc(P, cSectorDataSize); + Move(Sector.dwData[TimOffsetInSector], TIM_BUF^[P], FirstPartSize); + Inc(P, FirstPartSize); Inc(TimStartSectorPos, cSectorSize); sImageStream.Seek(TimStartSectorPos, soBeginning); - Dec(TIM_FULL_SECTORS); + TIM_FULL_SECTORS := (SIZE - P) div cSectorDataSize; + + while TIM_FULL_SECTORS > 0 do + begin + sImageStream.Read(Sector, cSectorSize); + + Move(Sector.dwData[0], TIM_BUF^[P], cSectorDataSize); + Inc(P, cSectorDataSize); + + Inc(TimStartSectorPos, cSectorSize); + sImageStream.Seek(TimStartSectorPos, soBeginning); + + Dec(TIM_FULL_SECTORS); + end; + + sImageStream.Read(Sector, cSectorSize); + + if SIZE > P then + begin + LastPartSize := SIZE - P; + Move(Sector.dwData[0], TIM_BUF^[P], LastPartSize); + end; + finally + P := 0; + Result := nil; + LoadTimFromBuf(TIM_BUF, Result, P); + sImageStream.Free; + Dispose(TIM_BUF); end; - - sImageStream.Read(Sector, cSectorSize); - - if SIZE > P then - begin - LastPartSize := SIZE - P; - Move(Sector.dwData[0], TIM_BUF^[P], LastPartSize); - end; - - P := 0; - Result := nil; - LoadTimFromBuf(TIM_BUF, Result, P); - sImageStream.Free; - Dispose(TIM_BUF); end; function LoadTimFromStream(Stream: TStream; var Position: Integer; @@ -455,9 +457,12 @@ var begin if not ImageScan then begin - sTIM := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); - Result := LoadTimFromStream(sTIM, Position, dwSize); - sTIM.Free; + try + sTIM := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); + Result := LoadTimFromStream(sTIM, Position, dwSize); + finally + sTIM.Free; + end; Exit; end; @@ -468,8 +473,7 @@ procedure SaveTimToFile(const FileName: string; TIM: PTIM); var tmp: TMemoryStream; begin - if TIM = nil then - Exit; + if TIM = nil then Exit; tmp := TMemoryStream.Create; tmp.Write(TIM^.DATA^[0], TIM^.dwSize);