summaryrefslogtreecommitdiff
path: root/gpr/source/lib/common/private/gpr_buffer.c
blob: 083bc0d61113ffb5864781dc7b40a3a93d482713 (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
/*! @file gpr_buffer.c
 *
 *  @brief Implementation of gpr_buffer object and functions that work on buffer
 *
 *  @version 1.0.0
 *
 *  (C) Copyright 2018 GoPro Inc (http://gopro.com/).
 *
 *  Licensed under either:
 *  - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
 *  - MIT license, http://opensource.org/licenses/MIT
 *  at your option.
 *
 *  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 "gpr_buffer.h"
#include "macros.h"
#include "stdc_includes.h"

int read_from_file(gpr_buffer* buffer, const char* file_path, gpr_malloc malloc_function, gpr_free free_function)
{
    assert( buffer != NULL );
    
    FILE *fIN = fopen(file_path, "rb");
    if (fIN == NULL)
    {
        fprintf (stderr, "Error while reading file: %s", file_path);
        return -1;
    }
    
    fseek (fIN, 0, SEEK_END);
    buffer->size = (size_t) ftell(fIN);
    rewind (fIN);
    
    buffer->buffer = malloc_function(buffer->size);
    
    if ( buffer->buffer == NULL)
    {
        fputs ("Memory error", stderr);
        fclose(fIN);
        return -1;
    }
    
    if (fread(buffer->buffer, 1, buffer->size, fIN) != buffer->size)
    {
        free_function(buffer->buffer);
        fputs ("Reading error", stderr);
        fclose(fIN);
        return -1;
    }
    
    fclose(fIN);
    
    return 0;
}

int write_to_file(const gpr_buffer* buffer, const char* file_path)
{
    unsigned int bytes_written;

    FILE *fOUT = fopen(file_path, "wb");
    if (fOUT == NULL)
    {
        fprintf (stderr, "Error while writing file: %s", file_path);
        return -1;
    }
    
    bytes_written = fwrite(buffer->buffer, 1, buffer->size, fOUT);
    if( bytes_written != buffer->size ) {
        fputs("Could not write bytes \n", stderr);
        perror("fwrite()");
        fclose(fOUT);
        return -2;
    }

    fclose(fOUT);
    
    return 0;
}

#include "gpr_rgb_buffer.h"

void gpr_rgb_gain_set_defaults(gpr_rgb_gain* x)
{
    x->r_gain_num      = 30;
    x->r_gain_pow2_den = 4;
    
    x->g_gain_num      = 1;
    x->g_gain_pow2_den = 0;
    
    x->b_gain_num      = 7;
    x->b_gain_pow2_den = 2;
}