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.
 
 
 
 
 
 

155 lines
3.4 KiB

  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * oric.c: Oric Atmos import/export functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "pipi.h"
  23. #include "pipi_internals.h"
  24. static int load_data(const char *name, uint8_t *screen);
  25. pipi_image_t *pipi_load_oric(const char *name)
  26. {
  27. static uint8_t const pal[32] =
  28. {
  29. 0x00, 0x00, 0x00, 0xff,
  30. 0x00, 0x00, 0xff, 0xff,
  31. 0x00, 0xff, 0x00, 0xff,
  32. 0x00, 0xff, 0xff, 0xff,
  33. 0xff, 0x00, 0x00, 0xff,
  34. 0xff, 0x00, 0xff, 0xff,
  35. 0xff, 0xff, 0x00, 0xff,
  36. 0xff, 0xff, 0xff, 0xff,
  37. };
  38. uint8_t screen[8000];
  39. pipi_image_t *img;
  40. pipi_pixels_t *p;
  41. uint8_t *data;
  42. int x, y, i;
  43. if(load_data(name, screen) < 0)
  44. return NULL;
  45. img = pipi_new(240, 200);
  46. p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  47. data = p->pixels;
  48. for(y = 0; y < 200; y++)
  49. {
  50. int bg = 0, fg = 7;
  51. for(x = 0; x < 40; x++)
  52. {
  53. int col;
  54. uint8_t c = screen[y * 40 + x];
  55. if(c & 0x40)
  56. {
  57. for(i = 0; i < 6; i++)
  58. {
  59. col = (c & (1 << (5 - i))) ? (c & 0x80) ? 7 - fg : fg
  60. : (c & 0x80) ? 7 - bg : bg;
  61. memcpy(data + (y * 240 + x * 6 + i) * 4, pal + 4 * col, 4);
  62. }
  63. }
  64. else if((c & 0x60) == 0x00)
  65. {
  66. if(c & 0x10)
  67. bg = c & 0x7;
  68. else
  69. fg = c & 0x7;
  70. col = (c & 0x80) ? 7 - bg : bg;
  71. for(i = 0; i < 6; i++)
  72. memcpy(data + (y * 240 + x * 6 + i) * 4, pal + 4 * col, 4);
  73. }
  74. /* else: invalid sequence */
  75. }
  76. }
  77. img->codec_priv = NULL;
  78. img->wrap = 0;
  79. img->u8 = 1;
  80. return img;
  81. }
  82. int pipi_save_oric(pipi_image_t *img, const char *name)
  83. {
  84. return -1;
  85. }
  86. /*
  87. * XXX: The following functions are local.
  88. */
  89. static int load_data(const char *name, uint8_t *screen)
  90. {
  91. FILE *fp;
  92. int ch;
  93. fp = fopen(name, "r");
  94. if(!fp)
  95. return -1;
  96. /* Skip the sync bytes */
  97. ch = fgetc(fp);
  98. if(ch != 0x16)
  99. goto syntax_error;
  100. while((ch = fgetc(fp)) == 0x16)
  101. ;
  102. if(ch != 0x24)
  103. goto syntax_error;
  104. /* Skip the header */
  105. if(fgetc(fp) != 0x00 || fgetc(fp) != 0xff || fgetc(fp) != 0x80
  106. || fgetc(fp) != 0x00 || fgetc(fp) != 0xbf || fgetc(fp) != 0x3f
  107. || fgetc(fp) != 0xa0 || fgetc(fp) != 0x00 || fgetc(fp) != 0x00)
  108. goto syntax_error;
  109. /* Skip the file name */
  110. for(;;)
  111. {
  112. ch = fgetc(fp);
  113. if(ch == EOF)
  114. goto syntax_error;
  115. if(ch == 0x00)
  116. break;
  117. }
  118. /* Read screen data */
  119. if(fread(screen, 1, 8000, fp) != 8000)
  120. goto syntax_error;
  121. fclose(fp);
  122. return 0;
  123. syntax_error:
  124. fclose(fp);
  125. return -1;
  126. }