Skip to content

Commit

Permalink
Support MSVC compiler using CMake
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Jul 1, 2021
1 parent adf5a1a commit d213f63
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 68 deletions.
48 changes: 48 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
cmake_minimum_required(VERSION 3.5)
project(giflib)

option(GIFLIB_UTILS "Build giflib utils" on)

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif()

add_library(giflib
dgif_lib.c
egif_lib.c
gif_err.c
gif_font.c
gif_hash.c
gifalloc.c
openbsd-reallocarray.c
quantize.c)

target_include_directories(giflib
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib)

if(GIFLIB_UTILS)
add_library(getarg getarg.c qprintf.c)
target_link_libraries(getarg giflib)

function(add_giflib_util name)
add_executable(${name} ${name}.c)
target_link_libraries(${name} giflib getarg)
endfunction()

add_giflib_util(gif2rgb)
add_giflib_util(gifbg)
add_giflib_util(gifbuild)
add_giflib_util(gifclrmp)
add_giflib_util(gifcolor)
add_giflib_util(gifecho)
add_giflib_util(giffilter)
add_giflib_util(giffix)
add_giflib_util(gifhisto)
add_giflib_util(gifinto)
add_giflib_util(gifsponge)
add_giflib_util(giftext)
if(HAVE_GETOPT_H)
add_giflib_util(giftool)
endif()
add_giflib_util(gifwedge)
endif()
38 changes: 16 additions & 22 deletions dgif_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ SPDX-License-Identifier: MIT
#include <stdio.h>
#include <string.h>

#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif /* _WIN32 */

#include "gif_lib.h"
#include "gif_lib_private.h"

Expand All @@ -32,9 +26,9 @@ SPDX-License-Identifier: MIT
/* avoid extra function call in case we use fread (TVT) */
static int InternalRead(GifFileType *gif, GifByteType *buf, int len) {
//fprintf(stderr, "### Read: %d\n", len);
return
return
(((GifFilePrivateType*)gif->Private)->Read ?
((GifFilePrivateType*)gif->Private)->Read(gif,buf,len) :
((GifFilePrivateType*)gif->Private)->Read(gif,buf,len) :
fread(buf,1,len,((GifFilePrivateType*)gif->Private)->File));
}

Expand All @@ -58,7 +52,7 @@ DGifOpenFileName(const char *FileName, int *Error)
int FileHandle;
GifFileType *GifFile;

if ((FileHandle = open(FileName, O_RDONLY)) == -1) {
if ((FileHandle = posix_open(FileName, O_RDONLY)) == -1) {
if (Error != NULL)
*Error = D_GIF_ERR_OPEN_FAILED;
return NULL;
Expand All @@ -85,7 +79,7 @@ DGifOpenFileHandle(int FileHandle, int *Error)
if (GifFile == NULL) {
if (Error != NULL)
*Error = D_GIF_ERR_NOT_ENOUGH_MEM;
(void)close(FileHandle);
(void)posix_close(FileHandle);
return NULL;
}

Expand All @@ -99,7 +93,7 @@ DGifOpenFileHandle(int FileHandle, int *Error)
if (Private == NULL) {
if (Error != NULL)
*Error = D_GIF_ERR_NOT_ENOUGH_MEM;
(void)close(FileHandle);
(void)posix_close(FileHandle);
free((char *)GifFile);
return NULL;
}
Expand All @@ -110,7 +104,7 @@ DGifOpenFileHandle(int FileHandle, int *Error)
_setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
#endif /* _WIN32 */

f = fdopen(FileHandle, "rb"); /* Make it into a stream: */
f = posix_fdopen(FileHandle, "rb"); /* Make it into a stream: */

/*@-mustfreeonly@*/
GifFile->Private = (void *)Private;
Expand Down Expand Up @@ -267,7 +261,7 @@ DGifGetScreenDesc(GifFileType *GifFile)
SortFlag = (Buf[0] & 0x08) != 0;
BitsPerPixel = (Buf[0] & 0x07) + 1;
GifFile->SBackGroundColor = Buf[1];
GifFile->AspectByte = Buf[2];
GifFile->AspectByte = Buf[2];
if (Buf[0] & 0x80) { /* Do we have global color map? */
int i;

Expand Down Expand Up @@ -299,7 +293,7 @@ DGifGetScreenDesc(GifFileType *GifFile)
* No check here for whether the background color is in range for the
* screen color map. Possibly there should be.
*/

return GIF_OK;
}

Expand Down Expand Up @@ -1050,7 +1044,7 @@ DGifDecompressInput(GifFileType *GifFile, int *Code)
GifFile->Error = D_GIF_ERR_IMAGE_DEFECT;
return GIF_ERROR;
}

while (Private->CrntShiftState < Private->RunningBits) {
/* Needs to get more bytes from input stream for next code: */
if (DGifBufferedInput(GifFile, Private->Buf, &NextByte) == GIF_ERROR) {
Expand Down Expand Up @@ -1164,19 +1158,19 @@ DGifSlurp(GifFileType *GifFile)

if (sp->ImageDesc.Interlace) {
int i, j;
/*
* The way an interlaced image should be read -
/*
* The way an interlaced image should be read -
* offsets and jumps...
*/
int InterlacedOffset[] = { 0, 4, 2, 1 };
int InterlacedJumps[] = { 8, 8, 4, 2 };
/* Need to perform 4 passes on the image */
for (i = 0; i < 4; i++)
for (j = InterlacedOffset[i];
for (j = InterlacedOffset[i];
j < sp->ImageDesc.Height;
j += InterlacedJumps[i]) {
if (DGifGetLine(GifFile,
sp->RasterBits+j*sp->ImageDesc.Width,
if (DGifGetLine(GifFile,
sp->RasterBits+j*sp->ImageDesc.Width,
sp->ImageDesc.Width) == GIF_ERROR)
return GIF_ERROR;
}
Expand All @@ -1201,7 +1195,7 @@ DGifSlurp(GifFileType *GifFile)
/* Create an extension block with our data */
if (ExtData != NULL) {
if (GifAddExtensionBlock(&GifFile->ExtensionBlockCount,
&GifFile->ExtensionBlocks,
&GifFile->ExtensionBlocks,
ExtFunction, ExtData[0], &ExtData[1])
== GIF_ERROR)
return (GIF_ERROR);
Expand All @@ -1215,7 +1209,7 @@ DGifSlurp(GifFileType *GifFile)
if (ExtData != NULL)
if (GifAddExtensionBlock(&GifFile->ExtensionBlockCount,
&GifFile->ExtensionBlocks,
CONTINUE_EXT_FUNC_CODE,
CONTINUE_EXT_FUNC_CODE,
ExtData[0], &ExtData[1]) == GIF_ERROR)
return (GIF_ERROR);
}
Expand Down
58 changes: 25 additions & 33 deletions egif_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ SPDX-License-Identifier: MIT
#include <string.h>
#include <fcntl.h>

#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#include <sys/types.h>
#endif /* _WIN32 */
#include <sys/stat.h>

#include "gif_lib.h"
#include "gif_lib_private.h"

Expand Down Expand Up @@ -67,11 +59,11 @@ EGifOpenFileName(const char *FileName, const bool TestExistence, int *Error)
GifFileType *GifFile;

if (TestExistence)
FileHandle = open(FileName, O_WRONLY | O_CREAT | O_EXCL,
S_IREAD | S_IWRITE);
FileHandle = posix_open(FileName, O_WRONLY | O_CREAT | O_EXCL,
S_IREAD | S_IWRITE);
else
FileHandle = open(FileName, O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE);
FileHandle = posix_open(FileName, O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE);

if (FileHandle == -1) {
if (Error != NULL)
Expand All @@ -80,7 +72,7 @@ EGifOpenFileName(const char *FileName, const bool TestExistence, int *Error)
}
GifFile = EGifOpenFileHandle(FileHandle, Error);
if (GifFile == (GifFileType *) NULL)
(void)close(FileHandle);
(void)posix_close(FileHandle);
return GifFile;
}

Expand Down Expand Up @@ -125,7 +117,7 @@ EGifOpenFileHandle(const int FileHandle, int *Error)
_setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
#endif /* _WIN32 */

f = fdopen(FileHandle, "wb"); /* Make it into a stream: */
f = posix_fdopen(FileHandle, "wb"); /* Make it into a stream: */

GifFile->Private = (void *)Private;
Private->FileHandle = FileHandle;
Expand Down Expand Up @@ -203,7 +195,7 @@ EGifGetGifVersion(GifFileType *GifFile)
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
int i, j;

/*
/*
* Bulletproofing - always write GIF89 if we need to.
* Note, we don't clear the gif89 flag here because
* users of the sequential API might have called EGifSetGifVersion()
Expand All @@ -230,7 +222,7 @@ EGifGetGifVersion(GifFileType *GifFile)
|| function == APPLICATION_EXT_FUNC_CODE)
Private->gif89 = true;
}

if (Private->gif89)
return GIF89_STAMP;
else
Expand All @@ -239,7 +231,7 @@ EGifGetGifVersion(GifFileType *GifFile)

/******************************************************************************
Set the GIF version. In the extremely unlikely event that there is ever
another version, replace the bool argument with an enum in which the
another version, replace the bool argument with an enum in which the
GIF87 value is 0 (numerically the same as bool false) and the GIF89 value
is 1 (numerically the same as bool true). That way we'll even preserve
object-file compatibility!
Expand All @@ -254,7 +246,7 @@ void EGifSetGifVersion(GifFileType *GifFile, const bool gif89)
/******************************************************************************
All writes to the GIF should go through this.
******************************************************************************/
static int InternalWrite(GifFileType *GifFileOut,
static int InternalWrite(GifFileType *GifFileOut,
const unsigned char *buf, size_t len)
{
GifFilePrivateType *Private = (GifFilePrivateType*)GifFileOut->Private;
Expand Down Expand Up @@ -577,7 +569,7 @@ EGifPutExtensionLeader(GifFileType *GifFile, const int ExtCode)
Put extension block data (see GIF manual) into a GIF file.
******************************************************************************/
int
EGifPutExtensionBlock(GifFileType *GifFile,
EGifPutExtensionBlock(GifFileType *GifFile,
const int ExtLen,
const void *Extension)
{
Expand Down Expand Up @@ -676,7 +668,7 @@ size_t EGifGCBToExtension(const GraphicsControlBlock *GCB,
Replace the Graphics Control Block for a saved image, if it exists.
******************************************************************************/

int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB,
int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB,
GifFileType *GifFile, int ImageIndex)
{
int i;
Expand Down Expand Up @@ -724,7 +716,7 @@ EGifPutCode(GifFileType *GifFile, int CodeSize, const GifByteType *CodeBlock)
}

/* No need to dump code size as Compression set up does any for us: */
/*
/*
* Buf = CodeSize;
* if (InternalWrite(GifFile, &Buf, 1) != 1) {
* GifFile->Error = E_GIF_ERR_WRITE_FAILED;
Expand Down Expand Up @@ -906,7 +898,7 @@ EGifCompressLine(GifFileType *GifFile,

while (i < LineLen) { /* Decode LineLen items. */
GifPixelType Pixel = Line[i++]; /* Get next pixel from stream. */
/* Form a new unique key to search hash table for the code combines
/* Form a new unique key to search hash table for the code combines
* CrntCode as Prefix string with Pixel as postfix char.
*/
int NewCode;
Expand Down Expand Up @@ -1063,9 +1055,9 @@ EGifBufferedOutput(GifFileType *GifFile,
******************************************************************************/

static int
EGifWriteExtensions(GifFileType *GifFileOut,
ExtensionBlock *ExtensionBlocks,
int ExtensionBlockCount)
EGifWriteExtensions(GifFileType *GifFileOut,
ExtensionBlock *ExtensionBlocks,
int ExtensionBlockCount)
{
if (ExtensionBlocks) {
int j;
Expand All @@ -1087,9 +1079,9 @@ EGifWriteExtensions(GifFileType *GifFileOut,
}

int
EGifSpew(GifFileType *GifFileOut)
EGifSpew(GifFileType *GifFileOut)
{
int i, j;
int i, j;

if (EGifPutScreenDesc(GifFileOut,
GifFileOut->SWidth,
Expand All @@ -1109,7 +1101,7 @@ EGifSpew(GifFileType *GifFileOut)
if (sp->RasterBits == NULL)
continue;

if (EGifWriteExtensions(GifFileOut,
if (EGifWriteExtensions(GifFileOut,
sp->ExtensionBlocks,
sp->ExtensionBlockCount) == GIF_ERROR)
return (GIF_ERROR);
Expand All @@ -1124,20 +1116,20 @@ EGifSpew(GifFileType *GifFileOut)
return (GIF_ERROR);

if (sp->ImageDesc.Interlace) {
/*
* The way an interlaced image should be written -
/*
* The way an interlaced image should be written -
* offsets and jumps...
*/
int InterlacedOffset[] = { 0, 4, 2, 1 };
int InterlacedJumps[] = { 8, 8, 4, 2 };
int k;
/* Need to perform 4 passes on the images: */
for (k = 0; k < 4; k++)
for (j = InterlacedOffset[k];
for (j = InterlacedOffset[k];
j < SavedHeight;
j += InterlacedJumps[k]) {
if (EGifPutLine(GifFileOut,
sp->RasterBits + j * SavedWidth,
if (EGifPutLine(GifFileOut,
sp->RasterBits + j * SavedWidth,
SavedWidth) == GIF_ERROR)
return (GIF_ERROR);
}
Expand Down
4 changes: 4 additions & 0 deletions gif_font.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ SPDX-License-Identifier: MIT

#include "gif_lib.h"

#ifdef _MSC_VER
#define strtok_r strtok_s
#endif

/*****************************************************************************
Ascii 8 by 8 regular font - only first 128 characters are supported.
*****************************************************************************/
Expand Down
1 change: 0 additions & 1 deletion gif_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ SPDX-License-Identifier: MIT
#ifndef _GIF_HASH_H_
#define _GIF_HASH_H_

#include <unistd.h>
#include <stdint.h>

#define HT_SIZE 8192 /* 12bits = 4096 or twice as big! */
Expand Down

0 comments on commit d213f63

Please sign in to comment.