Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. void SDGF_Image::load_pcx(const char *name,SDGF_Canvas &Canvas)
  2. {
  3. FILE *target;
  4. unsigned long int width,heigth,index,position,length,uncompressed_length;
  5. unsigned char repeat;
  6. unsigned char *original;
  7. unsigned char *uncompressed;
  8. PCX_head head;
  9. target=fopen(name,"rb");
  10. if(target==NULL)
  11. {
  12. puts("Can't open a image file");
  13. exit(EXIT_FAILURE);
  14. }
  15. fseek(target,0,SEEK_END);
  16. length=ftell(target)-128;
  17. rewind(target);
  18. fread(&head,128,1,target);
  19. if(head.color*head.planes!=24)
  20. {
  21. puts("Incorrect image format");
  22. exit(EXIT_FAILURE);
  23. }
  24. if(head.compress!=1)
  25. {
  26. puts("Incorrect image format");
  27. exit(EXIT_FAILURE);
  28. }
  29. width=head.max_x-head.min_x+1;
  30. heigth=head.max_y-head.min_y+1;
  31. uncompressed_length=3*width*heigth;
  32. index=0;
  33. position=0;
  34. original=(unsigned char*)calloc(length,1);
  35. if(original==NULL)
  36. {
  37. puts("Can't allocate memory for image buffer");
  38. exit(EXIT_FAILURE);
  39. }
  40. uncompressed=(unsigned char*)calloc(uncompressed_length,1);
  41. if(uncompressed==NULL)
  42. {
  43. puts("Can't allocate memory for image buffer");
  44. exit(EXIT_FAILURE);
  45. }
  46. fread(original,length,1,target);
  47. fclose(target);
  48. while (index<length)
  49. {
  50. if (original[index]<192)
  51. {
  52. uncompressed[position]=original[index];
  53. position++;
  54. index++;
  55. }
  56. else
  57. {
  58. for (repeat=original[index]-192;repeat>0;repeat--)
  59. {
  60. uncompressed[position]=original[index+1];
  61. position++;
  62. }
  63. index+=2;
  64. }
  65.  
  66. }
  67. free(original);
  68. Canvas.load_image(uncompressed,width,heigth);
  69. free(uncompressed);
  70. }
  71. void SDGF_Canvas::load_image(const unsigned char *buffer,const unsigned long int image_width,const unsigned long int image_height)
  72. {
  73. unsigned long int length;
  74. length=image_width*image_height*3;
  75. width=image_width;
  76. height=image_height;
  77. if(image!=NULL) free(image);
  78. image=(SDGF_Color*)calloc(length,1);
  79. if (image==NULL)
  80. {
  81. puts("Can't allocate memory for image buffer");
  82. exit(EXIT_FAILURE);
  83. }
  84. memmove(image,buffer,length);
  85. }
  86. struct SDGF_Color
  87. {
  88. unsigned char blue:8;
  89. unsigned char green:8;
  90. unsigned char red:8;
  91. };