Skip to content

Commit 9871353

Browse files
committed
gpujpeg_exif: small improvements
make this prettier (IYWU+enum) + use localtime_s (since we are an library we should not call thread-unsafe functions)
1 parent 2f8369f commit 9871353

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

src/compat/time.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @file compat/time.h
3+
* @author Martin Pulec <[email protected]>
4+
*
5+
* time-related compatibility header
6+
*/
7+
/*
8+
* Copyright (c) 2024-2025 CESNET, zájmové sdružení právnických osob
9+
* All rights reserved.
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, is permitted provided that the following conditions
13+
* are met:
14+
*
15+
* 1. Redistributions of source code must retain the above copyright
16+
* notice, this list of conditions and the following disclaimer.
17+
*
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
*
22+
* 3. Neither the name of CESNET nor the names of its contributors may be
23+
* used to endorse or promote products derived from this software without
24+
* specific prior written permission.
25+
*
26+
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
27+
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
28+
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30+
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36+
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37+
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38+
*/
39+
40+
#ifndef COMPAT_TIME_H_7C0E2D4B_7D55_4107_918B_7BF2BEB92428
41+
#define COMPAT_TIME_H_7C0E2D4B_7D55_4107_918B_7BF2BEB92428
42+
43+
#include <time.h>
44+
45+
#ifndef __STDC_LIB_EXT1__
46+
#ifdef _WIN32
47+
#define gmtime_s(timer, buf) gmtime_s(buf, timer)
48+
#define localtime_s(timer, buf) localtime_s(buf, timer)
49+
#else
50+
#define gmtime_s gmtime_r
51+
#define localtime_s localtime_r
52+
#endif
53+
#endif // ! defined __STDC_LIB_EXT1__
54+
55+
#endif // !defined COMPAT_TIME_H_7C0E2D4B_7D55_4107_918B_7BF2BEB92428

src/gpujpeg_exif.c

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,32 @@
2929

3030
#include "gpujpeg_exif.h"
3131

32-
#include <assert.h>
33-
#include <ctype.h>
34-
#include <stdint.h>
35-
#include <stdlib.h>
36-
#include <string.h>
37-
#include <time.h> // for strftime
38-
32+
#include <assert.h> // for assert
33+
#include <ctype.h> // for isdigit
34+
#include <stdint.h> // for uint8_t, uint32_t, uint16_t
35+
#include <stdio.h> // for printf
36+
#include <stdlib.h> // for size_t, NULL, free, abort, calloc
37+
#include <string.h> // for memcpy, strlen, memmove, strcmp
38+
#include <time.h> // for strftime, time, time_t
39+
// IWYU pragma: no_include <endian.h> # via compat/endian.h.
40+
41+
// for strncasecmp
3942
#ifdef _WIN32
4043
#define strncasecmp _strnicmp
44+
#else
45+
#include <strings.h>
4146
#endif
4247

43-
#include "compat/endian.h"
44-
#include "gpujpeg_common_internal.h"
45-
#include "gpujpeg_encoder_internal.h"
46-
#include "gpujpeg_marker.h"
47-
#include "gpujpeg_writer.h"
48+
#include "../libgpujpeg/gpujpeg_common.h" // for gpujpeg_image_parameters, gpuj...
49+
#include "../libgpujpeg/gpujpeg_encoder.h" // for GPUJPEG_ENC_OPT_EXIF_TAG
50+
#include "../libgpujpeg/gpujpeg_type.h" // for gpujpeg_color_space
51+
#include "compat/endian.h" // IWYU pragma: keep for htobe32
52+
#include "compat/time.h" // IWYU pragma: keep for localtime_s
53+
#include "gpujpeg_common_internal.h" // for gpujpeg_coder, ERROR_MSG, WARN...
54+
#include "gpujpeg_encoder_internal.h" // for gpujpeg_encoder
55+
#include "gpujpeg_marker.h" // for gpujpeg_marker_code
56+
#include "gpujpeg_util.h" // for ARR_SIZE
57+
#include "gpujpeg_writer.h" // for gpujpeg_writer, gpujpeg_writer...
4858

4959
enum exif_tag_type {
5060
ET_NONE = 0,
@@ -129,6 +139,7 @@ enum {
129139
ETIFF_INCHES = 2,
130140
NEXT_IFD_PTR_SZ = 4,
131141
IFD_ITEM_SZ = 12,
142+
DPI_DEFAULT = 72,
132143
EEXIF_FIRST = 0x827A, // (Exposure time) first tag id of Exif Private Tags
133144
};
134145

@@ -321,11 +332,12 @@ gpujpeg_write_0th(struct gpujpeg_encoder* encoder, const uint8_t* start)
321332
{
322333
char date_time[] = " : : : : "; // unknown val by Exif 2.3
323334
time_t now = time(NULL);
324-
(void) strftime(date_time, sizeof date_time, "%Y:%m:%d %H:%M:%S", localtime(&now));
335+
struct tm buf;
336+
(void) strftime(date_time, sizeof date_time, "%Y:%m:%d %H:%M:%S", localtime_s(&now, &buf));
325337
struct tag_value tags[] = {
326338
{ETIFF_ORIENTATION, {.uvalue = ETIFF_ORIENT_HORIZONTAL}},
327-
{ETIFF_XRESOLUTION, {.urational = {72, 1}} },
328-
{ETIFF_YRESOLUTION, {.urational = {72, 1}} },
339+
{ETIFF_XRESOLUTION, {.urational = {DPI_DEFAULT, 1}} },
340+
{ETIFF_YRESOLUTION, {.urational = {DPI_DEFAULT, 1}} },
329341
{ETIFF_RESOLUTION_UNIT, {.uvalue = ETIFF_INCHES} },
330342
{ETIFF_SOFTWARE, {.csvalue = "GPUJPEG"} },
331343
{ETIFF_DATE_TIME , {.csvalue = date_time} },

0 commit comments

Comments
 (0)