Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.1 KiB

  1. //
  2. // mxToolKit (c) 1999 by Mete Ciragan
  3. //
  4. // file: mxTga.cpp
  5. // implementation: all
  6. // last modified: Apr 15 1999, Mete Ciragan
  7. // copyright: The programs and associated files contained in this
  8. // distribution were developed by Mete Ciragan. The programs
  9. // are not in the public domain, but they are freely
  10. // distributable without licensing fees. These programs are
  11. // provided without guarantee or warrantee expressed or
  12. // implied.
  13. //
  14. #include "mxtk/mxTga.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. mxImage *
  18. mxTgaRead (const char *filename)
  19. {
  20. FILE *file;
  21. file = fopen (filename, "rb");
  22. if (!file)
  23. return 0;
  24. byte identFieldLength;
  25. byte colorMapType;
  26. byte imageTypeCode;
  27. fread (&identFieldLength, sizeof (byte), 1, file);
  28. fread (&colorMapType, sizeof (byte), 1, file);
  29. fread (&imageTypeCode, sizeof (byte), 1, file);
  30. fseek (file, 12, SEEK_SET);
  31. word width, height;
  32. byte pixelSize;
  33. fread (&width, sizeof (word), 1, file);
  34. fread (&height, sizeof (word), 1, file);
  35. fread (&pixelSize, sizeof (byte), 1, file);
  36. // only 24-bit RGB uncompressed
  37. if (colorMapType != 0 ||
  38. imageTypeCode != 2 ||
  39. pixelSize != 24)
  40. {
  41. fclose (file);
  42. return 0;
  43. }
  44. fseek (file, 18 + identFieldLength, SEEK_SET);
  45. mxImage *image = new mxImage ();
  46. if (!image->create (width, height, 24))
  47. {
  48. delete image;
  49. fclose (file);
  50. return 0;
  51. }
  52. byte *data = (byte *) image->data;
  53. for (int y = 0; y < height; y++)
  54. {
  55. byte *scanline = (byte *) &data[(height - y - 1) * width * 3];
  56. for (int x = 0; x < width; x++)
  57. {
  58. scanline[x * 3 + 2] = (byte) fgetc (file);
  59. scanline[x * 3 + 1] = (byte) fgetc (file);
  60. scanline[x * 3 + 0] = (byte) fgetc (file);
  61. //scanline[x * 4 + 3] = 0xff;
  62. }
  63. }
  64. fclose (file);
  65. return image;
  66. }
  67. bool
  68. mxTgaWrite (const char *filename, mxImage *image)
  69. {
  70. if (!image)
  71. return false;
  72. if (image->bpp != 24)
  73. return false;
  74. FILE *file = fopen (filename, "wb");
  75. if (!file)
  76. return false;
  77. //
  78. // write header
  79. //
  80. fputc (0, file); // identFieldLength
  81. fputc (0, file); // colorMapType == 0, no color map
  82. fputc (2, file); // imageTypeCode == 2, uncompressed RGB
  83. word w = 0;
  84. fwrite (&w, sizeof (word), 1, file); // colorMapOrigin
  85. fwrite (&w, sizeof (word), 1, file); // colorMapLength
  86. fputc (0, file); // colorMapEntrySize
  87. fwrite (&w, sizeof (word), 1, file); // imageOriginX
  88. fwrite (&w, sizeof (word), 1, file); // imageOriginY
  89. w = (word) image->width;
  90. fwrite (&w, sizeof (word), 1, file); // imageWidth
  91. w = (word) image->height;
  92. fwrite (&w, sizeof (word), 1, file); // imageHeight
  93. fputc (24, file); // imagePixelSize
  94. fputc (0, file); // imageDescriptorByte
  95. // write no ident field
  96. // write no color map
  97. // write imagedata
  98. byte *data = (byte *) image->data;
  99. for (int y = 0; y < image->height; y++)
  100. {
  101. byte *scanline = (byte *) &data[(image->height - y - 1) * image->width * 3];
  102. for (int x = 0; x < image->width; x++)
  103. {
  104. fputc ((byte) scanline[x * 3 + 2], file);
  105. fputc ((byte) scanline[x * 3 + 1], file);
  106. fputc ((byte) scanline[x * 3 + 0], file);
  107. }
  108. }
  109. fclose (file);
  110. return true;
  111. }