Skip to content

Commit

Permalink
Added injection to webp.
Browse files Browse the repository at this point in the history
  • Loading branch information
jklmnn committed Nov 18, 2014
1 parent b4fe67e commit f6d963a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
Added ability to inject gif files
0.6.0:
Added injecting to bmp files.
0.7.0:
Added injecting to webp files.
Fixed crash on not existing gif file.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Code can now be embedded into existing gif files by using the flag `-i` with a g
Example:
`$ ./imagejs gif code.js -i giffile.gif`

This is currently available for gif, bmp, webp, where webp is still beta.

For more information and to view an example image visit [jklmnn.de](http://jklmnn.de/imagejs/).

Supported output files are: gif, bmp, webp, pnm, pgf
Expand Down
33 changes: 30 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ int main(int argc, char *argv[]){
fprintf(out, "%c", outbuf[i]);
}
free(imbuf);
}
}else{
_help(argv[4], 3);
return 2;
}
}else if(strcmp(argv[3], "-i") == 0){
_help(argv[3], 2);
return 2;
Expand All @@ -146,8 +149,32 @@ int main(int argc, char *argv[]){
}
}else if(strcmp(argv[1], "webp") == 0){
if(argc > 3){
_help(argv[3], 1);
return 3;
if(argc > 4){
FILE *im = fopen(argv[4], "rb");
if(im){
fseek(im, 0 , SEEK_END);
int imsize = ftell(im);
rewind(im);
char *imbuf = (char*)malloc(imsize + sizeof(char));
fread(imbuf, 1, imsize, im);
fclose(im);
outbuf = webp_js_i(buf, filesize, imbuf, imsize);
out = fopen(webp_filename(argv[2], getlen(argv[2])), "wb");
for(int i = 0; i < (filesize + imsize + WEBP_JS_HEADER_I); i++){
fprintf(out, "%c", outbuf[i]);
}
free(imbuf);
}else{
_help(argv[4], 3);
return 3;
}
}else if(strcmp(argv[3], "-i") == 0){
_help(argv[3], 2);
return 2;
}else{
_help(argv[3], 1);
return 3;
}
}else{
outbuf = webp_js(buf, filesize);
out = fopen(webp_filename(argv[2], getlen(argv[2])), "wb");
Expand Down
2 changes: 1 addition & 1 deletion meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ License: GNU GENERAL PUBLIC LICENSE Version 3


#define VERSION_MAJ 0
#define VERSION_MIN 6
#define VERSION_MIN 7
#define VERSION_FIX 0
#define AUTHOR "jklmnn"
#define HOMEPAGE "http://jklmnn.de/imagejs"
Expand Down
11 changes: 11 additions & 0 deletions webp.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ char* webp_js(char *content, int size){
return buffer;
}

char *webp_js_i(char *content, int csize, char* image, int isize){
const char _webp_header[12] = {0x52, 0x49, 0x46, 0x46, 0x2f, 0x2a, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50};
const char _webp_header_end[WEBP_JS_HEADER_I] = {0x2a, 0x2f, 0x3d, 0x30, 0x3b, 0x0a};
char *buffer = (char*)malloc(isize + csize + WEBP_JS_HEADER_I);
memcpy(buffer, _webp_header, 12);
memcpy(&buffer[12], &image[12], isize - 12);
memcpy(&buffer[isize], _webp_header_end, WEBP_JS_HEADER_I);
memcpy(&buffer[isize + WEBP_JS_HEADER_I], content, csize);
return buffer;
}

char *webp_filename(char *fn, int size){
const char _webp_ending[WEBP_ENDING] = {0x2e, 0x77, 0x65, 0x62, 0x70, 0x00};
char *newfile = (char*)malloc(size + WEBP_ENDING);
Expand Down
2 changes: 2 additions & 0 deletions webp.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ License: GNU GENERAL PUBLIC LICENSE Version 3
#include <string.h>

#define WEBP_JS_HEADER 18
#define WEBP_JS_HEADER_I 6
#define WEBP_ENDING 6

char *webp_js(char*, int);
char *webp_js_i(char*, int, char*, int);
char *webp_filename(char*, int);

#endif

0 comments on commit f6d963a

Please sign in to comment.