tgalib.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <X11/Xlib.h>
  4. #include <X11/Xutil.h>
  5. #include <X11/Xos.h>
  6. #include <X11/Xatom.h>
  7. #include <X11/keysym.h>
  8. #include <X11/Intrinsic.h>
  9. #include <zlib.h>
  10. #include "xinit.h"
  11. XImage *loadtga(gzFile gf)
  12. {
  13. XImage *ximage;
  14. unsigned char header[18];
  15. unsigned char c,c2;
  16. int i,j,k,ibr,ibg, ibb;
  17. int picx,picy;
  18. long newpixval;
  19. char *data, *destptr;
  20. ximage=NULL;
  21. if(gf!=NULL){
  22. #if DEBUG==TRUE
  23. printf("Loading picture...\n");
  24. #endif
  25. //read tga file
  26. gzread(gf,&header,18);
  27. if (header[1]!=1 || //color map type
  28. (header[2]!=1 && header[2]!=9) || //raw or RLE color mapped image
  29. header[7]!=24 || //number of bits/color (8*3)
  30. header[16]!=8){ //bits per pixel
  31. return(NULL);
  32. }
  33. picx=header[12]+header[13]*256;
  34. picy=header[14]+header[15]*256;
  35. #if DEBUG==TRUE
  36. printf("File info: '");
  37. #endif
  38. for(i=0;i<header[0];i++){
  39. gzread(gf,&c,1);
  40. #if DEBUG==TRUE
  41. printf("%c",c);
  42. #endif
  43. }; //skip file info
  44. #if DEBUG==TRUE
  45. printf("'\n");
  46. #endif
  47. ximage = XCreateImage(dis, vsl, DefaultDepth(dis,scr), ZPixmap, 0, NULL, picx, (picy+1), 8, picx * 4);
  48. data= (byte *) malloc(((picx * (picy+1))) * 4);
  49. if(ximage==NULL||data==NULL) {
  50. printf("Unable to create ximage\n");
  51. exit(2);
  52. }
  53. ximage->data= (char *)data;
  54. destptr= data;
  55. ximage->byte_order= LSBFirst;
  56. ximage->bits_per_pixel=32;
  57. #if DEBUG==TRUE
  58. printf("XImage created\n");
  59. #endif
  60. //read palette
  61. for(i=0;i<header[5]+header[6]*256;i++){
  62. j=header[3]+header[4]*256+i;
  63. j=j*3;
  64. gzread(gf,&pal[j+2],1);
  65. gzread(gf,&pal[j+1],1);
  66. gzread(gf,&pal[j+0],1);
  67. }
  68. #if DEBUG==TRUE
  69. printf("Palette loaded\n");
  70. #endif
  71. if(header[2]==1){
  72. //raw image
  73. #if DEBUG==TRUE
  74. printf("Raw image\n");
  75. #endif
  76. for(i=0;i<picy*2;i++){
  77. for(j=0;j<picx*2;j++){
  78. gzread(gf,&c,1);
  79. k=c*3;
  80. newpixval= MkColorTGA(pal[k+0], pal[k+1], pal[k+2]);
  81. valToMem(newpixval, destptr, 3);
  82. destptr += 4;
  83. }
  84. }
  85. } else {
  86. // rle compressed
  87. #if DEBUG==TRUE
  88. printf("RLE image\n");
  89. #endif
  90. i=0; j=0;
  91. while(i<picy*picx){
  92. gzread(gf,&c2,1);
  93. if(c2 & 0x80){
  94. //compressed package
  95. c2&=0x7f; c2++;
  96. gzread(gf,&c,1);
  97. k=c*3;
  98. newpixval= MkColorTGA(pal[k+0], pal[k+1], pal[k+2]);
  99. while(c2>0){
  100. valToMem(newpixval, destptr, 3);
  101. destptr+=4;
  102. i++;
  103. c2--;
  104. }
  105. } else {
  106. //raw package
  107. c2++;
  108. while(c2>0){
  109. gzread(gf,&c,1);
  110. k=c*3;
  111. newpixval= MkColorTGA(pal[k+0], pal[k+1], pal[k+2]);
  112. valToMem(newpixval, destptr, 3);
  113. destptr+=4;
  114. i++;
  115. c2--;
  116. }
  117. }
  118. }
  119. }
  120. #if DEBUG==TRUE
  121. printf("File loaded, %dx%d data: %d bytes\n---\n",picx,picy,i);
  122. #endif
  123. }
  124. return(ximage);
  125. }