From 1da96e0dbe95f5078623e2b0469152692833a096 Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Tue, 22 Nov 2022 23:02:54 -0500 Subject: file io, compression --- createcalls.cpp | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 195 insertions(+), 7 deletions(-) (limited to 'createcalls.cpp') diff --git a/createcalls.cpp b/createcalls.cpp index 4c72c4e..12fb2de 100644 --- a/createcalls.cpp +++ b/createcalls.cpp @@ -5,6 +5,120 @@ PostMsg(project_state *State, char *msg) State->Msg = msg; } +/* +static bool32 +File_Open(project_state *State, memory *Memory, char *Filename) +{ + // uint16 ContextIndex = State->NumberOfOpenFiles; + // State->CurrentFileIndex = State->NumberOfOpenFiles; + uint16 ContextIndex = 0; + State->CurrentFileIndex = 0; + + if (!State->NumberOfOpenFiles) { + Memory_AddPartition(Memory, P_UndoBuffer); + Memory_AddPartition(Memory, F_Data); + Memory_AddPartition(Memory, F_BitmapData); + } + + state_file_context *Context = &State->Context[ContextIndex]; + file_data *File = (file_data *)Memory_PartitionAtIndex(Memory, F_Data, ContextIndex); + + SDL_RWops *FileHandle = SDL_RWFromFile(Filename, "r+b"); + + if (!FileHandle) { + return 0; + } + + int64 FileSize = SDL_RWsize(FileHandle); + + IO_ReadFromStream((void *)File, sizeof(file_data), FileHandle); + SDL_RWseek(FileHandle, 0, RW_SEEK_SET); + IO_ReadFromStream((void *)File, File->Bitmap_ByteOffset, FileHandle); + + void *CompressedLocation = Memory_PushScratch(Memory, File->CompressedSize); + void *BitmapLocation = Memory_PartitionAtIndex(Memory, F_BitmapData, ContextIndex); + + IO_ReadFromStream(CompressedLocation, File->CompressedSize, FileHandle); + + uint64 Bitmap_Size = Source_GetByteOffset(File, File->NumberOfSources); + + Data_Decompress(Memory, CompressedLocation, File->CompressedSize, BitmapLocation, Bitmap_Size); + + Memory_PopScratch(Memory, File->CompressedSize); + + SDL_RWclose(FileHandle); + + Context->UI.Initializing = 4; + + if (!State->NumberOfOpenFiles) { + History_Entry_Commit(Memory, ContextIndex, "Open file"); + History_Action_State_Swap(Memory, sizeof(State->NumberOfOpenFiles), &State->NumberOfOpenFiles); + State->NumberOfOpenFiles++; + History_Entry_End(Memory, ContextIndex); + } + + sprintf(State->Context[0].Filename, "%s", Filename); + Memory->IsFileSaved = true; + Memory->History[0].NumberOfEntries = 0; + Memory->History[0].EntryPlayhead = 0; + + return 1; +} +*/ + +static void +File_Open(project_data *File, project_state *State, memory *Memory, char *Filename) +{ + SDL_RWops *FileHandle = SDL_RWFromFile(Filename, "r+b"); + uint64 FileSize = SDL_RWseek(FileHandle, 0, RW_SEEK_END); + void *CompressedData = Memory_PushScratch(Memory, FileSize); + SDL_RWseek(FileHandle, 0, RW_SEEK_SET); + IO_ReadFromStream(CompressedData, FileSize, FileHandle); + SDL_RWclose(FileHandle); + Data_Decompress(Memory, CompressedData, FileSize, File, 0); + Memory_PopScratch(Memory, FileSize); +} + +static bool32 +File_SaveAs(project_data *File, project_state *State, memory *Memory, char *Filename) +{ + SDL_RWops *TestFile = SDL_RWFromFile(Filename, "wb"); + + if (!TestFile) + return 0; + + uint8 *FileEndAddress = (uint8 *)Memory->Slot[F_PrincipalBitmaps].Address; + int h = 0, c = 0, i = 0; + while (Block_Loop(Memory, F_Sources, File->Source_Count, &h, &c, &i)) { + block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, i); + if (Source->Type == source_type_principal) { + uint64 Size = Source->Width * Source->Height * Source->BytesPerPixel; + void *SourceBitmapAddress = Memory_Block_AddressAtIndex(Memory, F_PrincipalBitmaps, Source->Bitmap_Index, 0); + uint8 *BitmapEnd = (uint8 *)SourceBitmapAddress + Size; + if (BitmapEnd > FileEndAddress) + FileEndAddress = BitmapEnd; + } + } + + Assert(FileEndAddress); + uint64 FileSize = FileEndAddress - (uint8 *)File; + + void *CompressedLocation = Memory_PushScratch(Memory, FileSize); + + uint64 CompressedSize = Data_Compress(Memory, File, FileSize, CompressedLocation, FileSize, Z_BEST_COMPRESSION); + + Memory_PopScratch(Memory, FileSize); + + IO_WriteToStream(CompressedLocation, CompressedSize, TestFile); + SDL_RWclose(TestFile); + + Memory->IsFileSaved = true; + + return 1; +} + + + static uint16 Source_Generate_Blank(project_data *File, project_state *State, memory *Memory, uint16 Width, uint16 Height, uint16 BytesPerPixel) { @@ -44,7 +158,7 @@ static void Source_DumpThumbnail(memory *Memory, block_source *Source, uint32 T_Width, uint32 T_Height) { Assert(Source->Type == source_type_principal_temp); - void *BitmapAddress = Memory_Block_AddressAtIndex(Memory, F_PrincipalBitmaps, Source->Bitmap_Index); + void *BitmapAddress = Memory_Block_AddressAtIndex(Memory, F_PrincipalBitmaps, Source->Bitmap_Index, 0); uint32 Size = T_Height*T_Width*4; uint8 *Output = (uint8 *)Memory_PushScratch(Memory, Size); stbir_resize_uint8((uint8 *)BitmapAddress, Source->Width, Source->Height, 0, Output, T_Height, T_Width, 0, 4); @@ -162,7 +276,7 @@ Precomp_Init(project_data *File, memory *Memory) Comp->Occupied = 1; Comp->Name_String_Index = Memory_Block_AllocateNew(Memory, F_Strings); - block_string *String = (block_string *)Memory_Block_AddressAtIndex(Memory, F_Strings, Comp->Name_String_Index); + block_string *String = (block_string *)Memory_Block_AddressAtIndex(Memory, F_Strings, Comp->Name_String_Index, 0); sprintf(String->Char, "Comp %i", File->Comp_Count); String->Occupied = 1; @@ -220,7 +334,7 @@ Layer_Select(memory *Memory, project_state *State, int32 i) void Layer_DeselectAll(project_data *File, project_state *State, memory *Memory) { int h = 0, c = 0, i = 0; - while (Block_Loop(Memory, F_Sources, File->Layer_Count, &h, &c, &i)) { + while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i)) { block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i); Layer->IsSelected = false; } @@ -302,6 +416,16 @@ void Clipboard_Paste(project_data *File, project_state *State, memory *Memory, s } } +inline sorted_property_info * +Property_GetSortedInfo(sorted_property_info *SortedPropertyInfo, int i, int h) +{ + return SortedPropertyInfo + (i * 8) + h; +} +inline uint16 * +Property_GetSortedArray(uint16 *SortedPropertyArray, int i, int h) +{ + return SortedPropertyArray + (i * 8 * MAX_KEYFRAMES_PER_BLOCK) + (h * MAX_KEYFRAMES_PER_BLOCK); +} void Clipboard_Store(project_data *File, project_state *State, memory *Memory, sorted_comp_info *SortedCompInfo, sorted_layer *SortedLayerInfo, sorted_property_info *SortedPropertyInfo, uint16 *SortedPropertyArray) { @@ -317,8 +441,8 @@ void Clipboard_Store(project_data *File, project_state *State, memory *Memory, s for (int h = 0; h < AmountOf(Layer->Property); h++) { property_channel *Property = &Layer->Property[h]; if (Property->IsToggled || Layer->IsSelected) { - sorted_property_info *InfoLocation = SortedPropertyInfo + (i * 7) + h; - uint16 *ArrayLocation = SortedPropertyArray + (i * 7 * MAX_KEYFRAMES_PER_BLOCK) + (h * MAX_KEYFRAMES_PER_BLOCK); + sorted_property_info *InfoLocation = Property_GetSortedInfo(SortedPropertyInfo, i, h); + uint16 *ArrayLocation = Property_GetSortedArray(SortedPropertyArray, i, h); clipboard_channel *Channel = &Contents->Channel[Contents->ChannelCount]; bezier_point *FirstPoint = NULL; int TimeOffset = 0; @@ -596,9 +720,10 @@ void Layer_SortAll(project_data *File, project_state *State, memory *Memory, sor for (int h = 0; h < AmountOf(Layer->Property); h++) { property_channel *Property = &Layer->Property[h]; if (Property->Block_Bezier_Count) { - sorted_property_info *InfoLocation = SortedPropertyInfo + (i * 7) + h; - uint16 *ArrayLocation = SortedPropertyArray + (i * 7 * MAX_KEYFRAMES_PER_BLOCK) + (h * MAX_KEYFRAMES_PER_BLOCK); + sorted_property_info *InfoLocation = Property_GetSortedInfo(SortedPropertyInfo, i, h); + uint16 *ArrayLocation = Property_GetSortedArray(SortedPropertyArray, i, h); Property_SortAll(Memory, State, Property, InfoLocation, ArrayLocation); + int x = 0; } } } @@ -770,6 +895,10 @@ void Source_UICreateButton(project_data *File, project_state *State, memory *Mem History_Entry_Commit(Memory, "Create layer from source"); CommitAction = 1; } + if (Source->Type == source_type_principal_temp) { + History_Action_Swap(Memory, F_File, sizeof(Source->Type), &Source->Type); + Source->Type = source_type_principal; + } block_layer *Layer = Layer_Init(File, Memory); Layer->Block_Source_Index = i; Layer->x.CurrentValue = Source->Width / 2; @@ -826,6 +955,7 @@ void Precomp_UICreateButton(project_data *File, project_state *State, memory *Me Bezier_Add(Memory, &PrecompLayer->time, Point0); Bezier_Add(Memory, &PrecompLayer->time, Point1); PrecompLayer->IsPrecomp = true; + Layer_Select(Memory, State, Memory_Block_LazyIndexAtAddress(Memory, F_Layers, PrecompLayer)); PrecompLayer->Block_Source_Index = File->Comp_Count - 1; PrecompLayer->Block_Composition_Index = CompIndex; PrecompLayer->Vertical_Offset = TopOffset; @@ -961,6 +1091,64 @@ Brush_Info(brush_info *B, brush_state *Brush, block_source *Source, v2 LayerPos, B->BrushRow = (uint8 *)B->BrushBuffer; } +void Bitmap_SwapData(uint8 *Address_0, uint8 *Address_1, uint64 Size, uint16 BytesPerPixel) +{ + uint64 i = 0; + uint16 ByteOffset = Bitmap_ByteInfo(BytesPerPixel).ByteOffset; + uint64 RemainderBytes = Size % ByteOffset; + Assert(BytesPerPixel == 8); + +#if NEON + Assert(InstructionMode != instruction_mode_neon); +#endif + + if (BytesPerPixel == 4) { + uint32 Temp = 0; + while (i < Size) { + uint32 *Pixel_0 = (uint32 *)(Address_0 + i); + uint32 *Pixel_1 = (uint32 *)(Address_1 + i); + if (*Pixel_0 != 0x00000000) { + Temp = *Pixel_1; + *Pixel_1 = *Pixel_0; + *Pixel_0 = Temp; + } + i += sizeof(uint32); + } + } else if (BytesPerPixel == 8) { + uint64 Temp = 0; + __m256i Zero = _mm256_set1_epi64x(0); + __m256i Max32 = _mm256_set1_epi32(0xFFFFFFFF); + if (InstructionMode == instruction_mode_avx) { + while (i < Size - RemainderBytes) { + uint8 *Pixel_0_Address = (Address_0 + i); + __m256i Pixel_0 = _mm256_loadu_si256((__m256i *)Pixel_0_Address); + __m256i AlphaMask = _mm256_cmpeq_epi64(Pixel_0, Zero); + if (_mm256_movemask_epi8(AlphaMask) != 0xFFFFFFFF) { + uint8 *Pixel_1_Address = (Address_1 + i); + __m256i Pixel_1 = _mm256_loadu_si256((__m256i *)Pixel_1_Address); + AlphaMask = _mm256_andnot_si256(AlphaMask, Max32); + _mm256_maskstore_epi64((long long *)Pixel_1_Address, AlphaMask, Pixel_0); + _mm256_maskstore_epi64((long long *)Pixel_0_Address, AlphaMask, Pixel_1); + } + i += sizeof(uint64)*4; + } + } + while (i < Size) { + uint64 *Pixel_0 = (uint64 *)(Address_0 + i); + uint64 *Pixel_1 = (uint64 *)(Address_1 + i); + if (*Pixel_0 != 0x0000000000000000) { + Temp = *Pixel_1; + *Pixel_1 = *Pixel_0; + *Pixel_0 = Temp; + } + i += sizeof(uint64); + } + } else { + Assert(0); + } +} + + static void PaintTest(brush_info B, void *Buffer, rectangle RenderRegion) { -- cgit v1.2.3