diff options
-rw-r--r-- | lib/exif_wrapper.cpp | 30 | ||||
-rw-r--r-- | lib/exif_wrapper.h | 3 | ||||
-rw-r--r-- | main.scm | 23 | ||||
-rw-r--r-- | makefile | 5 |
4 files changed, 50 insertions, 11 deletions
diff --git a/lib/exif_wrapper.cpp b/lib/exif_wrapper.cpp index eab5598..21ee6c6 100644 --- a/lib/exif_wrapper.cpp +++ b/lib/exif_wrapper.cpp @@ -35,10 +35,38 @@ extern "C" unsigned int getExifOrientation(uint8_t* imgBytes, unsigned int size) } } + +extern "C" void setExifOrientation(char* fileName, unsigned int orientation) { + try { + // Exiv init + Exiv2::XmpParser::initialize(); + ::atexit(Exiv2::XmpParser::terminate); + Exiv2::enableBMFF(); + + // using blob works, except that exiv2 won't write back to the blob :( + // std::unique_ptr<Exiv2::Image> image = Exiv2::ImageFactory::open(imgBytes, size); + // So instead we load from the temp file + std::unique_ptr<Exiv2::Image> image = Exiv2::ImageFactory::open(fileName); + assert(image.get() != 0); + image->readMetadata(); + Exiv2::ExifData &exifData = image->exifData(); + + exifData[ORIENTATION_KEY] = orientation; + + // Save exif to image object & save image back to file + image->setExifData(exifData); + image->writeMetadata(); + } + catch (Exiv2::Error& e) { + std::cout << "Caught Exiv2 exception '" << e.what() << "'\n"; + } +} +/* extern "C" void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigned int orientation) { try { // Hack to work around Exiv2 seeming to refuse to write back to blob, but will write // back to a file. Create temp file -> operate on it -> copy back to blob, unlink file. + // tmpFileName gets overwritten with the actual filename by mkstemp char tmpFileName[] = "/tmp/image-temp.XXXXXX"; int fd = mkstemp(tmpFileName); assert(fd != -1); @@ -74,4 +102,4 @@ extern "C" void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigne catch (Exiv2::Error& e) { std::cout << "Caught Exiv2 exception '" << e.what() << "'\n"; } -} +}*/ diff --git a/lib/exif_wrapper.h b/lib/exif_wrapper.h index ec51406..ab649c8 100644 --- a/lib/exif_wrapper.h +++ b/lib/exif_wrapper.h @@ -1,4 +1,5 @@ #include <stdint.h> unsigned int getExifOrientation(uint8_t* imgBytes, unsigned int size); -void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigned int orientation); +//void setExifOrientation(uint8_t* imgBytes, unsigned int size, unsigned int orientation); +void setExifOrientation(char* fileName, unsigned int orientation); @@ -1,5 +1,5 @@ (import - scheme (chicken base) (chicken foreign) + scheme (chicken base) (chicken foreign) (chicken file posix) (chicken format) (chicken port) (chicken io) (chicken random) (chicken time) (chicken process-context) (chicken irregex) (chicken condition) (chicken blob) spiffy intarweb uri-common html-parser spiffy-request-vars multipart-form-data @@ -145,11 +145,18 @@ raw-img-vec (u8vector-length raw-img-vec))) -(define (set-exif-orientation! raw-img orientation) - ((foreign-lambda void "setExifOrientation" blob unsigned-int unsigned-int) - raw-img - (blob-size raw-img) - orientation)) +(define (set-exif-orientation raw-img orientation) + (let-values + ([(fd file-path) (file-mkstemp "/tmp/image-temp.XXXXXX")]) + (file-write fd raw-img) + (file-close fd) + ((foreign-lambda void "setExifOrientation" nonnull-c-string unsigned-int) + file-path + orientation) + (let + ([port (open-input-file file-path #:binary)]) + (u8vector->blob/shared (read-u8vector #f port))) + )) (define dim-max 512) @@ -185,11 +192,9 @@ target-height channels) )))]) - (set-exif-orientation! resized-jpg orientation) - resized-jpg + (set-exif-orientation resized-jpg orientation) ))) - (define users (alist->hash-table '(("lawrence" . "pw") ("demo" . "pw") ("dan" . "pw")))) (define apikeys (make-hash-table)) @@ -1,5 +1,9 @@ build: ./main.scm build-cpp mkdir -p ./build +# chicken-csc -static ./main.scm \ +# -L -lsqlite3 \ +# -L lib/exif_wrapper.o \ +# -C -Ilib/ chicken-csc -static ./main.scm \ -L -lsqlite3 \ -L lib/exif_wrapper.o \ @@ -9,6 +13,7 @@ build: ./main.scm build-cpp mv ./main ./build/main build-cpp: g++ -g -c lib/exif_wrapper.cpp -o lib/exif_wrapper.o +# g++ -static -g -c lib/exif_wrapper.cpp -o lib/exif_wrapper.o run: build ./build/main |