comparison src/libgfx.c @ 1302:38614c07c2e2

Now with "almost" working 24bit PCX output. Almost.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 19 Aug 2017 11:46:49 +0300
parents e03f20d0f785
children be30466fbc47
comparison
equal deleted inserted replaced
1301:e03f20d0f785 1302:38614c07c2e2
740 typedef struct 740 typedef struct
741 { 741 {
742 DMPCXHeader *header; 742 DMPCXHeader *header;
743 Uint8 *buf; 743 Uint8 *buf;
744 size_t bufLen, bufOffs; 744 size_t bufLen, bufOffs;
745 int format;
746 FILE *fp; 745 FILE *fp;
747 } DMPCXData; 746 } DMPCXData;
748 747
749 748
750 // Returns one byte from row buffer (of length len) at offset soffs, 749 // Returns one byte from row buffer (of length len) at offset soffs,
834 833
835 return DMERR_OK; 834 return DMERR_OK;
836 } 835 }
837 836
838 837
839 int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *spec) 838 int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageConvSpec *pspec)
840 { 839 {
841 DMPCXData pcx; 840 DMPCXData pcx;
842 DMPCXHeader hdr; 841 DMPCXHeader hdr;
843 int res; 842 int res;
843 DMImageConvSpec spec;
844
845 memcpy(&spec, pspec, sizeof(DMImageConvSpec));
846 spec.format = spec.paletted ? DM_IFMT_PALETTE : DM_IFMT_RGB;
847 spec.planar = TRUE;
844 848
845 // Create output file 849 // Create output file
846 pcx.buf = NULL; 850 pcx.buf = NULL;
847 pcx.format = spec->paletted ? DM_IFMT_PALETTE : DM_IFMT_RGB;
848 pcx.header = &hdr; 851 pcx.header = &hdr;
849 pcx.fp = fp; 852 pcx.fp = fp;
850 853
851 // Create PCX header 854 // Create PCX header
852 dmMemset(&hdr, 0, sizeof(hdr)); 855 dmMemset(&hdr, 0, sizeof(hdr));
853 if (spec->paletted && img->pal != NULL) 856 if (spec.paletted && img->pal != NULL)
854 { 857 {
855 for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++) 858 for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++)
856 { 859 {
857 hdr.colorMap[i].r = img->pal[i].r; 860 hdr.colorMap[i].r = img->pal[i].r;
858 hdr.colorMap[i].g = img->pal[i].g; 861 hdr.colorMap[i].g = img->pal[i].g;
860 } 863 }
861 } 864 }
862 hdr.manufacturer = 10; 865 hdr.manufacturer = 10;
863 hdr.version = 5; 866 hdr.version = 5;
864 hdr.encoding = 1; 867 hdr.encoding = 1;
865 hdr.hres = img->width * spec->scaleX; 868 hdr.hres = img->width * spec.scaleX;
866 hdr.vres = img->height * spec->scaleY; 869 hdr.vres = img->height * spec.scaleY;
867 hdr.xmin = hdr.ymin = 0; 870 hdr.xmin = hdr.ymin = 0;
868 hdr.xmax = (img->width * spec->scaleX) - 1; 871 hdr.xmax = (img->width * spec.scaleX) - 1;
869 hdr.ymax = (img->height * spec->scaleY) - 1; 872 hdr.ymax = (img->height * spec.scaleY) - 1;
870 hdr.palInfo = 1; 873 hdr.palInfo = 1;
871 hdr.hScreenSize = hdr.hres; 874 hdr.hScreenSize = hdr.hres;
872 hdr.vScreenSize = hdr.vres; 875 hdr.vScreenSize = hdr.vres;
873 876
874 // TODO XXX .. maybe actually compute these asdf 877 // TODO XXX .. maybe actually compute these asdf
875 hdr.bitsPerPlane = 8; 878 hdr.bitsPerPlane = 8;
876 hdr.nplanes = dmImageGetBytesPerPixel(pcx.format); 879 hdr.nplanes = dmImageGetBytesPerPixel(spec.format);
877 880
878 res = img->width * spec->scaleX; 881 res = img->width * spec.scaleX;
879 hdr.bpl = res / 2; 882 hdr.bpl = res / 2;
880 if (res % 2) hdr.bpl++; 883 if (res % 2) hdr.bpl++;
881 hdr.bpl *= 2; 884 hdr.bpl *= 2;
882 885
883 dmMsg(2, 886 dmMsg(2,
884 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n", 887 "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n",
885 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax, 888 hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax,
886 hdr.hres, hdr.vres, 889 hdr.hres, hdr.vres,
887 hdr.hScreenSize, hdr.vScreenSize); 890 hdr.hScreenSize, hdr.vScreenSize);
888 891
889 dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n", 892 dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s, planar=%s\n",
890 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, spec->paletted ? "yes" : "no"); 893 hdr.nplanes, hdr.bitsPerPlane, hdr.bpl,
894 spec.paletted ? "yes" : "no",
895 spec.planar ? "yes" : "no"
896 );
891 897
892 // TODO XXX this is also bogus 898 // TODO XXX this is also bogus
893 pcx.bufLen = hdr.bpl * 4; 899 pcx.bufLen = hdr.bpl * 4;
894 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL) 900 if ((pcx.buf = dmMalloc(pcx.bufLen)) == NULL)
895 { 901 {
941 "PCX: Could not write header remainder.\n"); 947 "PCX: Could not write header remainder.\n");
942 goto error; 948 goto error;
943 } 949 }
944 950
945 // Write image data 951 // Write image data
946 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, spec); 952 res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, &spec);
947 953
948 // Write VGA palette 954 // Write VGA palette
949 if (spec->paletted) 955 if (spec.paletted)
950 { 956 {
951 int i; 957 int i;
952 dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors); 958 dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors);
953 959
954 dm_fwrite_byte(pcx.fp, 0x0C); 960 dm_fwrite_byte(pcx.fp, 0x0C);