Welcome to mirror list, hosted at ThFree Co, Russian Federation.

averagecolorasset.cpp « Asset « Asset « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6446a6caa9fee7f48794c0a237f10adf657feec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//-----------------------------------------------------------------------------
//           Name: averagecolorasset.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "averagecolorasset.h"

#include <Internal/integer.h>
#include <Internal/cachefile.h>
#include <Internal/error.h>

#include <Asset/AssetLoader/fallbackassetloader.h>
#include <Compat/fileio.h>
#include <Graphics/bytecolor.h>

#include <string>
#include <cstdio>
#include <cstdlib>

using std::string;

AverageColor::AverageColor( AssetManager* owner, uint32_t asset_id ) : Asset( owner, asset_id )
{

}

int AverageColor::Load( const string& rel_path, uint32_t load_flags ) {
    string load_path;
    string suffix = "_avg_color_cache";
    uint16_t checksum;
    ModID cached_modsource;
    if(CacheFile::CheckForCache(path_, suffix, &load_path,&cached_modsource,&checksum)){
        if(ReadCacheFile(load_path, checksum)){
            modsource_ = cached_modsource;
            return kLoadOk;
        }
    }
    
    char abs_path[kPathSize];
    ModID modsource;
    if(FindImagePath(rel_path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths, true, NULL, true, true, &modsource) == -1){
        //FatalError("Error", "Could not get average color of %s", rel_path.c_str());
        return kLoadErrorMissingFile;
    }
    modsource_ = modsource;
    
    vec4 byte_color = GetAverageColor(abs_path);
    color_ = vec4(byte_color[2]/255.0f,
                  byte_color[1]/255.0f,
                  byte_color[0]/255.0f,
                  byte_color[3]/255.0f);
    
    //TODO: this checksum is never set, calculate it!
    WriteCacheFile(GetWritePath(modsource)+rel_path + suffix, checksum);

    return kLoadOk;
}

const char* AverageColor::GetLoadErrorString() {
    return "";
}

void AverageColor::Unload() {

}

void AverageColor::Reload() {

}

void AverageColor::ReportLoad() {

}

bool AverageColor::ReadCacheFile(const string& path, uint16_t checksum) {
    FILE* file = my_fopen(path.c_str(),"rb");
    if(!file){
        return false;
    }
    CacheFile::ScopedFileCloser file_closer(file);
    unsigned char version;
    fread(&version, sizeof(version), 1, file);
    if(version != kAverageColorCacheVersion){
        return false;
    }
    uint16_t file_checksum;
    fread(&file_checksum, sizeof(file_checksum), 1, file);
    if(checksum != 0 && checksum != file_checksum){
        return false;
    }
    fread(&color_, sizeof(color_), 1, file);
    return true;  
}

void AverageColor::WriteCacheFile(const string& path, uint16_t checksum) {
    FILE* file = my_fopen(path.c_str(),"wb");
    CacheFile::ScopedFileCloser file_closer(file);
    unsigned char version = kAverageColorCacheVersion;
    fwrite(&version, sizeof(version), 1, file);
    fwrite(&checksum, sizeof(checksum), 1, file);
    fwrite(&color_, sizeof(color_), 1, file);
}

AssetLoaderBase* AverageColor::NewLoader() {
    return new FallbackAssetLoader<AverageColor>();
}