Skip to content

Commit

Permalink
make it compile with MS Visual Studio
Browse files Browse the repository at this point in the history
* add dll import/export declarations
* add casts to avoid compiler warnings
* fix long vs long long error, unearthed by compiling under Windows

Compiling with Visual Studio still needs some manual work that is
otherwise done by meson:
* create config.h
* compile and run gen-huffman.c and gen-reversebits.c to create the
  header files huffman-codes.h and reversebits.h
  • Loading branch information
Rupert committed Nov 16, 2024
1 parent 07497b7 commit c1f2df4
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 119 deletions.
9 changes: 4 additions & 5 deletions bmp-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <stdint.h>
#include <stdarg.h>

#define BMPLIB_LIB

#include "config.h"
#include "bmplib.h"
#include "logging.h"
Expand Down Expand Up @@ -187,9 +189,6 @@ int cm_count_bits(unsigned long v)
{
int bits = 0;

if (v < 0)
v = -v;

while (v) {
bits++;
v >>= 1;
Expand Down Expand Up @@ -309,12 +308,12 @@ int cm_is_one_of(int n, int candidate, ...)

int cm_align4padding(unsigned long long a)
{
return cm_align4size(a) - a;
return (int) (cm_align4size(a) - a);
}

int cm_align2padding(unsigned long long a)
{
return cm_align2size(a) - a;
return (int) (cm_align2size(a) - a);
}


Expand Down
27 changes: 16 additions & 11 deletions bmp-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
#define ATTR_CONST
#endif

#ifdef WIN32
#define API
#else
#define API __attribute__ ((visibility ("default")))
#endif

union Pixel {
unsigned int value[4];
struct {
Expand All @@ -48,7 +54,7 @@ union Pixel {

struct Colormask {
union {
unsigned long value[4];
unsigned long long value[4];
struct {
unsigned long long red;
unsigned long long green;
Expand All @@ -57,21 +63,21 @@ struct Colormask {
};
} mask;
union {
unsigned long value[4];
int value[4];
struct {
unsigned long red;
unsigned long green;
unsigned long blue;
unsigned long alpha;
int red;
int green;
int blue;
int alpha;
};
} shift;
union {
int value[4];
struct {
int red;
int green;
int blue;
int alpha;
int red;
int green;
int blue;
int alpha;
};
} bits;
union {
Expand Down Expand Up @@ -228,7 +234,6 @@ int write_s32_le(FILE *file, int32_t val);
int read_s16_le(FILE *file, int16_t *val);
int read_s32_le(FILE *file, int32_t *val);

#define API __attribute__ ((visibility ("default")))


#define HMAGIC_READ 0x44414552UL
Expand Down
28 changes: 15 additions & 13 deletions bmp-read-loadimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <stdint.h>
#include <math.h>

#define BMPLIB_LIB

#include "config.h"
#include "bmplib.h"
#include "logging.h"
Expand Down Expand Up @@ -226,7 +228,7 @@ static void s_read_whole_image(BMPREAD_R rp, unsigned char *restrict image)

linesize = (size_t) rp->width * rp->result_bytes_per_pixel;

for (y = 0; y < rp->height; y += yoff) {
for (y = 0; y < (int) rp->height; y += yoff) {
real_y = (rp->orientation == BMP_ORIENT_TOPDOWN) ? y : rp->height-1-y;
s_read_one_line(rp, image + real_y * linesize);
if (rp->rle_eof || s_stopping_error(rp))
Expand Down Expand Up @@ -263,7 +265,7 @@ static void s_read_one_line(BMPREAD_R rp, unsigned char *restrict line)
}

if (!(rp->rle_eof || s_stopping_error(rp))) {
if (yoff > rp->height - rp->lbl_file_y) {
if (yoff > (int) rp->height - rp->lbl_file_y) {
rp->invalid_delta = TRUE;
}
rp->lbl_file_y += yoff;
Expand All @@ -277,7 +279,7 @@ static void s_read_one_line(BMPREAD_R rp, unsigned char *restrict line)
}

rp->lbl_y++;
if (rp->lbl_y >= rp->height) {
if (rp->lbl_y >= (int) rp->height) {
rp->image_loaded = TRUE;
}
}
Expand Down Expand Up @@ -354,12 +356,12 @@ static int s_read_rgb_line(BMPREAD_R rp, unsigned char *restrict line)
d = s_s2_13_to_float(px.value[i]);
if (i < 3 && rp->conv64 == BMP_CONV64_SRGB)
d = s_srgb_gamma_float(d);
((float*)line)[offs+i] = d;
((float*)line)[offs+i] = (float) d;
}
} else {
for (i = 0; i < rp->result_channels; i++) {
d = s_int_to_float(px.value[i], rp->cmask.bits.value[i]);
((float*)line)[offs + i] = d;
((float*)line)[offs + i] = (float) d;
}
}
break;
Expand Down Expand Up @@ -490,11 +492,11 @@ static inline int s_read_rgb_pixel(BMPREAD_R rp, union Pixel *restrict px)
v |= ((unsigned long long)byte) << i;
}

px->red = (v & rp->cmask.mask.red) >> rp->cmask.shift.red;
px->green = (v & rp->cmask.mask.green) >> rp->cmask.shift.green;
px->blue = (v & rp->cmask.mask.blue) >> rp->cmask.shift.blue;
px->red = (unsigned int) ((v & rp->cmask.mask.red) >> rp->cmask.shift.red);
px->green = (unsigned int) ((v & rp->cmask.mask.green) >> rp->cmask.shift.green);
px->blue = (unsigned int) ((v & rp->cmask.mask.blue) >> rp->cmask.shift.blue);
if (rp->has_alpha)
px->alpha = (v & rp->cmask.mask.alpha) >> rp->cmask.shift.alpha;
px->alpha = (unsigned int) ((v & rp->cmask.mask.alpha) >> rp->cmask.shift.alpha);
else
px->alpha = (1<<rp->result_bitsperchannel) - 1;

Expand Down Expand Up @@ -784,8 +786,8 @@ static int s_huff_find_eol(BMPREAD_R rp);

static void s_read_huffman_line(BMPREAD_R rp, unsigned char *restrict line)
{
size_t x = 0, offs;
int runlen;
size_t offs;
int x = 0, runlen;
int black = 0;

while (x < rp->width) {
Expand Down Expand Up @@ -820,7 +822,7 @@ static void s_read_huffman_line(BMPREAD_R rp, unsigned char *restrict line)
}

for (int i = 0; i < runlen; i++, x++) {
offs = x * rp->result_bytes_per_pixel;
offs = (size_t) x * rp->result_bytes_per_pixel;
if (rp->result_indexed) {
line[offs] = black;
} else {
Expand Down Expand Up @@ -922,7 +924,7 @@ static inline void s_int_to_result_format(BMPREAD_R rp, int frombits, unsigned c
}
switch (rp->result_format) {
case BMP_FORMAT_FLOAT:
((float*)px)[c] = (double) v / ((1ULL<<frombits)-1);
((float*)px)[c] = (float) ((double) v / ((1ULL<<frombits)-1));
break;
case BMP_FORMAT_S2_13:
((uint16_t*)px)[c] = (uint16_t) ((double) v / ((1ULL<<frombits)-1) * 8192.0 + 0.5);
Expand Down
2 changes: 2 additions & 0 deletions bmp-read-loadindexed.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <string.h>
#include <stdint.h>

#define BMPLIB_LIB

#include "config.h"
#include "bmplib.h"
#include "logging.h"
Expand Down
16 changes: 9 additions & 7 deletions bmp-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <limits.h>
#include <stdarg.h>

#define BMPLIB_LIB

#include "config.h"
#include "bmplib.h"
#include "logging.h"
Expand Down Expand Up @@ -457,10 +459,10 @@ static int s_single_dim_val(BMPHANDLE h, enum Dimint dim)
ret = (int) rp->orientation;
break;
case DIM_XDPI:
ret = rp->ih->xpelspermeter / 39.37 + 0.5;
ret = (int) (rp->ih->xpelspermeter / (100.0 / 2.54) + 0.5);
break;
case DIM_YDPI:
ret = rp->ih->ypelspermeter / 39.37 + 0.5;
ret = (int) (rp->ih->ypelspermeter / (100.0 / 2.54) + 0.5);
break;
default:
return 0;
Expand Down Expand Up @@ -833,8 +835,8 @@ static struct Palette* s_read_palette(BMPREAD_R rp)
*****************************************************************************/
static int s_read_masks_from_bitfields(BMPREAD_R rp);
static int s_create_implicit_colormasks(BMPREAD_R rp);
static inline unsigned long s_calc_bits_for_mask(unsigned long long mask);
static inline unsigned long s_calc_shift_for_mask(unsigned long long mask);
static inline int s_calc_bits_for_mask(unsigned long long mask);
static inline int s_calc_shift_for_mask(unsigned long long mask);

static int s_read_colormasks(BMPREAD_R rp)
{
Expand Down Expand Up @@ -878,7 +880,7 @@ static int s_read_colormasks(BMPREAD_R rp)
return FALSE;
}
if (!(rp->cmask.mask.red | rp->cmask.mask.green | rp->cmask.mask.blue)) {
logerr(rp->log, "Empty color masks. Corrupted BMP?");
logerr(rp->log, "Empty color masks. Corrupt BMP?");
rp->lasterr = BMP_ERR_INVALID;
return FALSE;
}
Expand Down Expand Up @@ -1086,7 +1088,7 @@ static int s_create_implicit_colormasks(BMPREAD_R rp)
* s_calc_bits_for_mask
*****************************************************************************/

static inline unsigned long s_calc_bits_for_mask(unsigned long long mask)
static inline int s_calc_bits_for_mask(unsigned long long mask)
{
int bits = 0;

Expand All @@ -1110,7 +1112,7 @@ static inline unsigned long s_calc_bits_for_mask(unsigned long long mask)
* s_calc_shift_for_mask
*****************************************************************************/

static inline unsigned long s_calc_shift_for_mask(unsigned long long mask)
static inline int s_calc_shift_for_mask(unsigned long long mask)
{
int shift = 0;

Expand Down
Loading

0 comments on commit c1f2df4

Please sign in to comment.