simple.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright (C) 2014 Francesc Alted
  3. http://blosc.org
  4. License: MIT (see LICENSE.txt)
  5. Example program demonstrating use of the Blosc filter from C code.
  6. To compile this program:
  7. $ gcc simple.c -o simple -lblosc
  8. or, if you don't have the blosc library installed:
  9. $ gcc -O3 -msse2 simple.c -I../blosc -o simple -L../build/blosc
  10. Using MSVC on Windows:
  11. $ cl /arch:SSE2 /Ox /Fesimple.exe /Iblosc examples\simple.c blosc\blosc.c blosc\blosclz.c blosc\shuffle.c blosc\shuffle-sse2.c blosc\shuffle-generic.c blosc\bitshuffle-generic.c blosc\bitshuffle-sse2.c
  12. To run:
  13. $ ./simple
  14. Blosc version info: 1.4.2.dev ($Date:: 2014-07-08 #$)
  15. Compression: 4000000 -> 158494 (25.2x)
  16. Decompression succesful!
  17. Succesful roundtrip!
  18. */
  19. #include <stdio.h>
  20. #include <blosc.h>
  21. #define SIZE 100*100*100
  22. int main(){
  23. static float data[SIZE];
  24. static float data_out[SIZE];
  25. static float data_dest[SIZE];
  26. int isize = SIZE*sizeof(float), osize = SIZE*sizeof(float);
  27. int dsize = SIZE*sizeof(float), csize;
  28. int i;
  29. for(i=0; i<SIZE; i++){
  30. data[i] = i;
  31. }
  32. /* Register the filter with the library */
  33. printf("Blosc version info: %s (%s)\n",
  34. BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
  35. /* Initialize the Blosc compressor */
  36. blosc_init();
  37. /* Compress with clevel=5 and shuffle active */
  38. csize = blosc_compress(5, 1, sizeof(float), isize, data, data_out, osize);
  39. if (csize == 0) {
  40. printf("Buffer is uncompressible. Giving up.\n");
  41. return 1;
  42. }
  43. else if (csize < 0) {
  44. printf("Compression error. Error code: %d\n", csize);
  45. return csize;
  46. }
  47. printf("Compression: %d -> %d (%.1fx)\n", isize, csize, (1.*isize) / csize);
  48. /* Decompress */
  49. dsize = blosc_decompress(data_out, data_dest, dsize);
  50. if (dsize < 0) {
  51. printf("Decompression error. Error code: %d\n", dsize);
  52. return dsize;
  53. }
  54. printf("Decompression succesful!\n");
  55. /* After using it, destroy the Blosc environment */
  56. blosc_destroy();
  57. for(i=0;i<SIZE;i++){
  58. if(data[i] != data_dest[i]) {
  59. printf("Decompressed data differs from original!\n");
  60. return -1;
  61. }
  62. }
  63. printf("Succesful roundtrip!\n");
  64. return 0;
  65. }