Subversion Repositories Games.Carmageddon

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "pmfile.h"
  2. #include "harness/trace.h"
  3.  
  4. #include "CORE/FW/datafile.h"
  5. #include "CORE/FW/diag.h"
  6. #include "CORE/FW/genfile.h"
  7. #include "CORE/FW/resource.h"
  8. #include "CORE/PIXELMAP/pmmem.h"
  9. #include "CORE/PIXELMAP/pmdsptch.h"
  10. #include "CORE/V1DB/chunkids.h"
  11. #include "CORE/V1DB/stackids.h"
  12.  
  13. #include <stddef.h>
  14. #include <string.h>
  15.  
  16. br_file_enum_member pixelmap_type_FM[15] = {
  17.     { BR_PMT_INDEX_1, "BR_PMT_INDEX_1", },
  18.     { BR_PMT_INDEX_2, "BR_PMT_INDEX_2", },
  19.     { BR_PMT_INDEX_4, "BR_PMT_INDEX_4", },
  20.     { BR_PMT_INDEX_8, "BR_PMT_INDEX_8", },
  21.     { BR_PMT_RGB_555, "BR_PMT_RGB_555", },
  22.     { BR_PMT_RGB_565, "BR_PMT_RGB_565", },
  23.     { BR_PMT_RGB_888, "BR_PMT_RGB_888", },
  24.     { BR_PMT_RGBX_888, "BR_PMT_RGBX_888", },
  25.     { BR_PMT_RGBA_8888, "BR_PMT_RGBA_8888", },
  26.     { BR_PMT_YUYV_8888, "BR_PMT_YUYV_8888", },
  27.     { BR_PMT_YUV_888, "BR_PMT_YUV_888", },
  28.     { BR_PMT_DEPTH_16, "BR_PMT_DEPTH_16", },
  29.     { BR_PMT_DEPTH_32, "BR_PMT_DEPTH_32", },
  30.     { BR_PMT_ALPHA_8, "BR_PMT_ALPHA_8", },
  31.     { BR_PMT_INDEXA_88, "BR_PMT_INDEXA_88", },
  32. };
  33. br_file_enum pixelmap_type_F = { BR_ASIZE(pixelmap_type_FM), pixelmap_type_FM };
  34.  
  35. br_file_struct_member br_old_pixelmap_FM[7] = {
  36.     { DF_TYPE_ENUM_8, offsetof(br_pixelmap, type), "type", &pixelmap_type_F },
  37.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, row_bytes), "row_bytes", NULL },
  38.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, width), "width", NULL },
  39.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, height), "height", NULL },
  40.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, origin_x), "origin_x", NULL },
  41.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, origin_y), "origin_y", NULL },
  42.     { DF_TYPE_ASCIZ, offsetof(br_pixelmap, identifier), "identifier", NULL },
  43. };
  44.  
  45. br_file_struct_member br_pixelmap_FM[8] = {
  46.     { DF_TYPE_ENUM_8, offsetof(br_pixelmap, type), "type", &pixelmap_type_F },
  47.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, row_bytes), "row_bytes", NULL },
  48.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, width), "width", NULL },
  49.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, height), "height", NULL },
  50.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, origin_x), "origin_x", NULL },
  51.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, origin_y), "origin_y", NULL },
  52.     { DF_TYPE_BR_UINT_16, offsetof(br_pixelmap, mip_offset), "mip_offset", NULL },
  53.     { DF_TYPE_ASCIZ, offsetof(br_pixelmap, identifier), "identifier", NULL },
  54. };
  55.  
  56. br_file_struct br_pixelmap_F = { "br_pixelmap", BR_ASIZE(br_pixelmap_FM), br_pixelmap_FM, sizeof(br_pixelmap) };
  57. br_file_struct br_old_pixelmap_F = { "br_old_pixelmap", BR_ASIZE(br_old_pixelmap_FM), br_old_pixelmap_FM, sizeof(br_pixelmap) };
  58.  
  59. br_chunks_table_entry PixelmapLoadEntries[5] = {
  60.     { CHUNKID_END, 0u, FopRead_END },
  61.     { CHUNKID_PIXELMAP, 0u, FopRead_PIXELMAP },
  62.     { CHUNKID_PIXELMAP_PIXELS, 0u, FopRead_PIXELS },
  63.     { CHUNKID_PIXELMAP_ADD_MAP, 0u, FopRead_ADD_MAP },
  64.     { CHUNKID_OLD_PIXELMAP, 0u, FopRead_OLD_PIXELMAP },
  65. };
  66. br_chunks_table PixelmapLoadTable = { BR_ASIZE(PixelmapLoadEntries), PixelmapLoadEntries };
  67.  
  68. // IDA: int __usercall FopWrite_PIXELMAP@<EAX>(br_datafile *df@<EAX>, br_pixelmap *pixelmap@<EDX>)
  69. int FopWrite_PIXELMAP(br_datafile* df, br_pixelmap* pixelmap) {
  70.     br_pixelmap pmap;
  71.     LOG_TRACE("(%p, %p)", df, pixelmap);
  72.  
  73.     memcpy(&pmap, pixelmap, sizeof(br_pixelmap));
  74.     pmap.row_bytes = (pmTypeInfo[pmap.type].bits >> 3) * pmap.width;
  75.     df->prims->chunk_write(df, CHUNKID_PIXELMAP, df->prims->struct_size(df, &br_pixelmap_F, &pmap));
  76.     df->prims->struct_write(df, &br_pixelmap_F, &pmap);
  77.     return 0;
  78. }
  79.  
  80. // IDA: int __usercall FopRead_OLD_PIXELMAP@<EAX>(br_datafile *df@<EAX>, br_uint_32 id@<EDX>, br_uint_32 length@<EBX>, br_uint_32 count@<ECX>)
  81. int FopRead_OLD_PIXELMAP(br_datafile* df, br_uint_32 id, br_uint_32 length, br_uint_32 count) {
  82.     br_pixelmap* pp;
  83.     LOG_TRACE("(%p, %d, %d, %d)", df, id, length, count);
  84.  
  85.     pp = (br_pixelmap*)DevicePixelmapMemAllocate(BR_PMAF_NO_PIXELS | BR_PMAF_INVERTED, 0, 0, 0, 2);
  86.     df->res = pp;
  87.     df->prims->struct_read(df, &br_old_pixelmap_F, pp);
  88.     df->res = NULL;
  89. #if !defined(BRENDER_FIX_BUGS)
  90.     pp->row_bytes = (pmTypeInfo[pp->type].bits >> 3) * pp->width;
  91. #endif
  92.     DfPush(DF_PIXELMAP, pp, 1);
  93.     return 0;
  94. }
  95.  
  96. // IDA: int __usercall FopRead_PIXELMAP@<EAX>(br_datafile *df@<EAX>, br_uint_32 id@<EDX>, br_uint_32 length@<EBX>, br_uint_32 count@<ECX>)
  97. int FopRead_PIXELMAP(br_datafile* df, br_uint_32 id, br_uint_32 length, br_uint_32 count) {
  98.     br_pixelmap* pp;
  99.     LOG_TRACE("(%p, %d, %d, %d)", df, id, length, count);
  100.  
  101.     pp = (br_pixelmap*)DevicePixelmapMemAllocate(BR_PMAF_NO_PIXELS | BR_PMAF_INVERTED, 0, 0, 0, 2);
  102.     df->res = pp;
  103.     df->prims->struct_read(df, &br_pixelmap_F, pp);
  104.     df->res = NULL;
  105. #if !defined(BRENDER_FIX_BUGS)
  106.     pp->row_bytes = (pmTypeInfo[pp->type].bits >> 3) * pp->width;
  107. #endif
  108.     DfPush(DF_PIXELMAP, pp, 1);
  109.     return 0;
  110. }
  111.  
  112. // IDA: int __usercall FopWrite_PIXELS@<EAX>(br_datafile *df@<EAX>, br_pixelmap *pixelmap@<EDX>)
  113. int FopWrite_PIXELS(br_datafile* df, br_pixelmap* pixelmap) {
  114.     int size;
  115.     int bytes;
  116.     int block_count;
  117.     char* pixels;
  118.     LOG_TRACE("(%p, %p)", df, pixelmap);
  119.  
  120.     pixels = (char*)pixelmap->pixels + pixelmap->base_x * pixelmap->width + pixelmap->base_y;
  121.     if (pixelmap->mip_offset == 0) {
  122.         bytes = df->prims->block_size(df, pixels, (pmTypeInfo[pixelmap->type].bits >> 3) * pixelmap->width / pmTypeInfo[pixelmap->type].file_size, pixelmap->row_bytes, pixelmap->height, pmTypeInfo[pixelmap->type].file_size);
  123.         df->prims->chunk_write(df, CHUNKID_PIXELMAP_PIXELS, bytes);
  124.         bytes = (pmTypeInfo[pixelmap->type].bits >> 3) * pixelmap->width / pmTypeInfo[pixelmap->type].file_size;
  125.         size = pixelmap->row_bytes / pmTypeInfo[pixelmap->type].file_size;
  126.         block_count = pixelmap->height;
  127.     } else {
  128.         size = 0;
  129.         for (bytes = pixelmap->width; bytes != 0; bytes = bytes >> 1) {
  130.             size += bytes * bytes * (pmTypeInfo[pixelmap->type].bits >> 3);
  131.         }
  132.         df->prims->chunk_write(df, CHUNKID_PIXELMAP_PIXELS, df-> prims->block_size(df, pixels, size / pmTypeInfo[pixelmap->type].file_size, size, 1, pmTypeInfo[pixelmap->type].file_size));
  133.         bytes = size;
  134.         block_count = 1;
  135.     }
  136.     df->prims->block_write(df, pixels, bytes, size, block_count, pmTypeInfo[pixelmap->type].file_size);
  137.     return 0;
  138. }
  139.  
  140. // IDA: int __usercall FopRead_PIXELS@<EAX>(br_datafile *df@<EAX>, br_uint_32 id@<EDX>, br_uint_32 length@<EBX>, br_uint_32 count@<ECX>)
  141. int FopRead_PIXELS(br_datafile* df, br_uint_32 id, br_uint_32 length, br_uint_32 count) {
  142.     int icount = 0;
  143.     br_pixelmap* pp;
  144.     int size;
  145.     LOG_TRACE9("(%p, %d, %d, %d)", df, id, length, count);
  146.  
  147.     pp = DfTop(DF_PIXELMAP, NULL);
  148.     size = pmTypeInfo[pp->type].file_size;
  149.     df->res = pp;
  150.     pp->pixels = df->prims->block_read(df, NULL, &icount, size, BR_MEMORY_PIXELS);
  151.     pp->flags |= BR_PMF_LINEAR;
  152.     df->res = NULL;
  153.     return 0;
  154. }
  155.  
  156. // IDA: int __usercall FopWrite_ADD_MAP@<EAX>(br_datafile *df@<EAX>)
  157. int FopWrite_ADD_MAP(br_datafile* df) {
  158.     LOG_TRACE("(%p)", df);
  159.  
  160.     df->prims->chunk_write(df, CHUNKID_PIXELMAP_ADD_MAP, 0);
  161.     return 0;
  162. }
  163.  
  164. // IDA: int __usercall FopRead_ADD_MAP@<EAX>(br_datafile *df@<EAX>, br_uint_32 id@<EDX>, br_uint_32 length@<EBX>, br_uint_32 count@<ECX>)
  165. int FopRead_ADD_MAP(br_datafile* df, br_uint_32 id, br_uint_32 length, br_uint_32 count) {
  166.     br_pixelmap* pp;
  167.     br_pixelmap* map;
  168.     LOG_TRACE("(%p, %d, %d, %d)", df, id, length, count);
  169.  
  170.     map = DfPop(DF_PIXELMAP, 0);
  171.     pp = DfTop(DF_PIXELMAP, 0);
  172.     pp->map = map;
  173.     BrResAdd(pp, map);
  174.     return 0;
  175. }
  176.  
  177. // IDA: br_uint_32 __cdecl BrPixelmapLoadMany(char *filename, br_pixelmap **pixelmaps, br_uint_16 num)
  178. br_uint_32 BrPixelmapLoadMany(char* filename, br_pixelmap** pixelmaps, br_uint_16 num) {
  179.     br_datafile* df;
  180.     int count;
  181.     int r;
  182.     LOG_TRACE("(\"%s\", %p, %d)", filename, pixelmaps, num);
  183.  
  184.     df = DfOpen(filename, 0, BRT_FIXED);
  185.     if (df == NULL) {
  186.         return 0;
  187.     }
  188.     count = 0;
  189.     do {
  190.         if (count >= num) {
  191.             break;
  192.         }
  193.         r = DfChunksInterpret(df, &PixelmapLoadTable);
  194.         if (DfTopType() == DF_PIXELMAP) {
  195.             pixelmaps[count] = DfPop(DF_PIXELMAP, 0);
  196.             count++;
  197.         }
  198.     } while (r != 0);
  199.     DfClose(df);
  200.     return count;
  201. }
  202.  
  203. // IDA: int __usercall WritePixelmap@<EAX>(br_pixelmap *pp@<EAX>, br_datafile *df@<EDX>)
  204. int WritePixelmap(br_pixelmap* pp, br_datafile* df) {
  205.     LOG_TRACE("(%p, %p)", pp, df);
  206.  
  207.     BrPixelmapDirectLock(pp, 1);
  208.     if (pp->pixels == NULL) {
  209.         _BrAssert("pp->pixels", "pmfile.c", 291);
  210.     }
  211.     FopWrite_PIXELMAP(df, pp);
  212.     if (pp->map != NULL) {
  213.         WritePixelmap(pp->map, df);
  214.         FopWrite_ADD_MAP(df);
  215.     }
  216.     FopWrite_PIXELS(df, pp);
  217.     BrPixelmapDirectUnlock(pp);
  218.     return 0;
  219. }
  220.  
  221. // IDA: br_uint_32 __cdecl BrPixelmapSaveMany(char *filename, br_pixelmap **pixelmaps, br_uint_16 num)
  222. br_uint_32 BrPixelmapSaveMany(char* filename, br_pixelmap** pixelmaps, br_uint_16 num) {
  223.     br_datafile* df;
  224.     int i;
  225.     LOG_TRACE("(\"%s\", %p, %d)", filename, pixelmaps, num);
  226.  
  227.     if (filename == NULL) {
  228.         _BrAssert("filename != NULL", "pmfile.c", 324);
  229.     }
  230.     if (pixelmaps == NULL) {
  231.         _BrAssert("pixelmaps != NULL", "pmfile.c", 325);
  232.     }
  233.     df = DfOpen(filename, 1, BRT_FIXED);
  234.     if (df == NULL) {
  235.         return 0;
  236.     }
  237.     FopWrite_FILE_INFO(df, 2);
  238.     for (i = 0; i < num; i++) {
  239.         WritePixelmap(pixelmaps[i], df);
  240.         FopWrite_END(df);
  241.     }
  242.     DfClose(df);
  243.     return num;
  244. }
  245.  
  246. // IDA: br_pixelmap* __cdecl BrPixelmapLoad(char *filename)
  247. br_pixelmap* BrPixelmapLoad(char* filename) {
  248.     br_pixelmap* ptr;
  249.  
  250.     if (BrPixelmapLoadMany(filename, &ptr, 1) == 1) {
  251.         return ptr;
  252.     }
  253.     return NULL;
  254. }
  255.  
  256. // IDA: br_uint_32 __cdecl BrPixelmapSave(char *filename, br_pixelmap *ptr)
  257. br_uint_32 BrPixelmapSave(char* filename, br_pixelmap* ptr) {
  258.     LOG_TRACE("(\"%s\", %p)", filename, ptr);
  259.  
  260.     return BrPixelmapSaveMany(filename, &ptr, 1);
  261. }
  262.