Skip to content

Commit 50b3978

Browse files
committed
tidy up some functions in nsimage
- no need to allocate - remove global variable usage - remove some dead code
1 parent 6c9cef7 commit 50b3978

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

src/nsimage.mm

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ Please see the included GNU General Public License (GPL) for
2323
#include "AtomicParsley.h"
2424
#import <Cocoa/Cocoa.h>
2525

26-
bool isJPEG = false;
27-
bool isPNG = false;
26+
static void DetermineType(const char *picfilePath, bool &isJPEG, bool &isPNG) {
27+
char picHeader[20];
2828

29-
void DetermineType(const char *picfilePath) {
30-
char *picHeader = (char *)calloc(1, sizeof(char) * 20);
31-
u_int64_t r;
32-
33-
FILE *pic_file = NULL;
34-
pic_file = fopen(picfilePath, "rb");
35-
r = fread(picHeader, 8, 1, pic_file);
29+
FILE *pic_file = fopen(picfilePath, "rb");
30+
u_int64_t r = fread(picHeader, 8, 1, pic_file);
3631
fclose(pic_file);
3732

3833
if (memcmp(picHeader, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8) == 0) {
@@ -41,13 +36,14 @@ void DetermineType(const char *picfilePath) {
4136
} else if (memcmp(picHeader, "\xFF\xD8\xFF", 3) == 0) {
4237
isJPEG = true;
4338
isPNG = false;
39+
} else {
40+
isPNG = false;
41+
isJPEG = false;
4442
}
45-
free(picHeader);
46-
picHeader = NULL;
47-
return;
4843
}
4944

50-
char *DeriveNewPath(const char *filePath, PicPrefs myPicPrefs, char *newpath) {
45+
static char *
46+
DeriveNewPath(const char *filePath, PicPrefs myPicPrefs, char *newpath) {
5147
const char *suffix = strrchr(filePath, '.');
5248

5349
size_t filepath_len = strlen(filePath);
@@ -73,14 +69,6 @@ void DetermineType(const char *picfilePath) {
7369
strcat(newpath, suffix);
7470
}
7571

76-
if ((strncmp(suffix, ".jpg", 4) == 0) || (strncmp(suffix, ".jpeg", 5) == 0) ||
77-
(strncmp(suffix, ".JPG", 4) == 0) || (strncmp(suffix, ".JPEG", 5) == 0)) {
78-
isJPEG = true;
79-
} else if ((strncmp(suffix, ".png", 4) == 0) ||
80-
(strncmp(suffix, ".PNG", 4) == 0)) {
81-
isPNG = true;
82-
}
83-
8472
free(randstring);
8573
randstring = NULL;
8674
return newpath;
@@ -181,7 +169,8 @@ bool ResizeGivenImage(const char *filePath,
181169
resize = true;
182170
}
183171

184-
DetermineType(filePath);
172+
bool isJPEG, isPNG;
173+
DetermineType(filePath, isJPEG, isPNG);
185174
if ((isJPEG && myPicPrefs.allPNG) ||
186175
(isPNG && myPicPrefs.allJPEG)) { // handle jpeg->png & png->jpg conversion
187176
resize = true;
@@ -248,8 +237,6 @@ bool ResizeGivenImage(const char *filePath,
248237

249238
[image unlockFocus];
250239
[image release];
251-
isJPEG = false;
252-
isPNG = false;
253240
memcpy(resized_path,
254241
[outFile cStringUsingEncoding:NSUTF8StringEncoding],
255242
[outFile lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);

0 commit comments

Comments
 (0)