Commits

shuerhaaken  committed aecd3c6

add wavpack test for image reading/writing

  • Participants
  • Parent commits 473b2d7

Comments (0)

Files changed (3)

+commit 473b2d717e0928748f6e3ba54eee1ead62f1fe1b
+Author: Jörn Magens <shuerhaaken@googlemail.com>
+Date:   Thu Apr 4 17:36:39 2013 +0200
+
+    add missing wavpack test; fix crash with wavpack reading
+
 commit 18a367faa8629ea44e8bcb29c3b24c76ddbb22bf
 Author: Jörn Magens <shuerhaaken@googlemail.com>
 Date:   Tue Apr 2 11:31:14 2013 +0200

File tests/Makefile.am

     write_image_speex/test \
     write_image_tta/test \
     write_image_wav/test \
-    write_image_wma/test
+    write_image_wma/test \
+    write_image_wv/test
 
 TESTS = $(check_PROGRAMS)
 
 write_image_wma_test_SOURCES    = write_image_wma/main.cc
 write_image_wma_test_LDADD      = $(taginfo_ldadd)
 
+write_image_wv_test_SOURCES     = write_image_wv/main.cc
+write_image_wv_test_LDADD       = $(taginfo_ldadd)
 
 DISTCLEANFILES = \
     Makefile.in

File tests/write_image_wv/main.cc

+#include "taginfo.h"
+#include <stdio.h>
+#include <iostream>
+#include <fstream>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+using namespace TagInfo;
+//using namespace std;
+
+void clean_up(char* map, int size, int fd) {
+    if(munmap(map, size) == -1) {
+        perror("Error un-mmapping the file");
+    }
+    if(fd != 0)
+        close(fd);
+}
+
+int main( void ) {
+    Info * info;
+    std::string val   = TESTDIR "samples/sample.wv";
+    std::string image = TESTDIR "samples/test.jpg";
+    
+    //std::cout << std::endl << "val: " << val << std::endl;
+    std::string target = "/tmp/out_01.wv";
+    
+    std::ifstream  src(val.c_str());
+    std::ofstream  dst(target.c_str());
+    dst << src.rdbuf();
+    
+    char* data;
+    int data_length;
+    ImageType image_type = IMAGE_TYPE_JPEG;
+    
+    struct stat filestatus;
+    stat( image.c_str(), &filestatus );
+    //cout << filestatus.st_size << " bytes\n";
+    data_length = filestatus.st_size;
+    int fd;
+    
+    fd = open(image.c_str(), O_RDONLY);
+    if (fd == -1) {
+        perror("Error opening file for reading");
+        EXIT_FAILURE;
+    }
+    
+    data = (char*)mmap(0, data_length, PROT_READ, MAP_SHARED, fd, 0);
+    if(data == MAP_FAILED) {
+        close(fd);
+        perror("Error mmapping the file");
+        EXIT_FAILURE;
+    }
+    
+    info = Info::create_tag_info(target);
+    if(info) {
+        if(!info->set_image(data, data_length, image_type)) {
+            clean_up(data, data_length, fd);
+            delete info;
+            return EXIT_FAILURE;
+        }
+        info->write();
+    }
+    delete info;
+    info = NULL;
+    
+    info = Info::create_tag_info(target);
+    char* read;
+    int read_length;
+    ImageType read_type;
+    if(info) {
+        if(info->read()) {
+            if(!info->get_image(read, read_length, read_type)) {
+                delete info;
+                if(remove(target.c_str()) != 0 ) {
+                    clean_up(data, data_length, fd);
+                    return EXIT_FAILURE;
+                }
+            }
+            int i;
+            for(i = 0; i<data_length; i++) {
+                if(data[i] != read[i]) {
+                    clean_up(data, data_length, fd);
+                    delete [] read;
+                    return EXIT_FAILURE;
+                }
+            }
+            clean_up(data, data_length, fd);
+            delete [] read;
+            if(remove(target.c_str()) != 0 )
+                return EXIT_FAILURE;
+            return EXIT_SUCCESS;
+        }
+        delete info;
+        clean_up(data, data_length, fd);
+        if(remove(target.c_str()) != 0 )
+            return EXIT_FAILURE;
+        return EXIT_FAILURE;
+    }
+    else {
+       delete info;
+        clean_up(data, data_length, fd);
+        if(remove(target.c_str()) != 0 )
+            return EXIT_FAILURE;
+        return EXIT_FAILURE;
+    }
+    return EXIT_FAILURE;
+}
+