Advertisement
Guest User

Untitled

a guest
May 29th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <vips/vips.h>
  4. #include <stdlib.h>
  5. #include <glib-object.h>
  6.  
  7. gboolean use_copy_hack = FALSE;
  8. gboolean save_original = FALSE;
  9.  
  10. void process(VipsImage* src);
  11.  
  12. int main(int argc, char **argv) {
  13. if (argc != 3) {
  14. printf(
  15. "Demonstrates crash when saving a particular JPEG with libvips:\n"
  16. " %s <save original> <use copy>\n"
  17. "\n"
  18. " <save original>: + to save the original, - to skip\n"
  19. " <use copy>: + to use vips_copy(), - to use vips_extract_area()\n"
  20. "\n"
  21. "These cases crash:\n"
  22. " %s - +\n"
  23. " %s + +\n"
  24. " %s + -\n", argv[0], argv[0], argv[0], argv[0]);
  25. return 1;
  26. }
  27.  
  28. if (argv[1][0] == '+') {
  29. printf("Will save original.\n");
  30. save_original = TRUE;
  31. } else {
  32. printf("Skipping saving original.\n");
  33. }
  34.  
  35. if (argv[2][0] == '-') {
  36. printf("Using vips_extract_area() in place of vips_copy().\n");
  37. use_copy_hack = TRUE;
  38. } else {
  39. printf("Using vips_copy().\n");
  40. }
  41.  
  42. FILE *f = fopen("source.jpg", "rb");
  43. fseek(f, 0, SEEK_END);
  44. long fsize = ftell(f);
  45. fseek(f, 0, SEEK_SET);
  46.  
  47. char *buffer = malloc(fsize + 1);
  48. fread(buffer, fsize, 1, f);
  49. fclose(f);
  50.  
  51. VipsImage *img;
  52.  
  53. if (!(img = vips_image_new_from_buffer(buffer, fsize, NULL, NULL))) {
  54. printf("Failed to load file: %s\n", vips_error_buffer());
  55. vips_error_clear();
  56. }
  57.  
  58. if (save_original) {
  59. printf("Saving source image.\n");
  60. vips_jpegsave(img, "tmp.jpg", NULL);
  61. }
  62.  
  63. for (int i = 0; i < 8; i++) {
  64. printf("Starting conversion %d\n", i);
  65. process(img);
  66. }
  67.  
  68. vips_thread_shutdown();
  69.  
  70. return 0;
  71. }
  72.  
  73. void process(VipsImage* src) {
  74. VipsImage *dupe = NULL;
  75.  
  76. if (use_copy_hack) {
  77. if (vips_extract_area(src, &dupe, 0, 0, src->Xsize, src->Ysize, NULL) != 0) {
  78. printf("Failed to use hack to copy image: %s\n", vips_error_buffer());
  79. return;
  80. }
  81. } else {
  82. if (vips_copy(src, &dupe, NULL) != 0) {
  83. printf("Failed to copy image: %s\n", vips_error_buffer());
  84. return;
  85. }
  86. }
  87.  
  88. size_t length = 0;
  89. void *buffer = NULL;
  90. int result = vips_jpegsave_buffer(dupe, &buffer, &length, NULL);
  91.  
  92. if (result != 0) {
  93. printf("Failed to write JPEG: %s\n", vips_error_buffer());
  94. return;
  95. }
  96.  
  97. g_free(buffer);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement