; Copyright 1996 Acorn Computers Ltd
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
; Sources.CFSI-JPEG
; This makes an image of assembler code that can be loaded in by ChangeFSI
; and then called, to decode a JPEG file.

; At the start of the image are defined entrypoints.
; Conditions for entering these are:
;   lr = return address
;   sp -> a useable stack
;   r0..r3 = arguments (could be more on stack)
; On exit:
;   r0 will contain a returned result, if defined
;   r1..r3, lr will be trashed
;   all other registers will be preserved

        B       cfsi_jpeg_scan_file
        B       cfsi_jpeg_find_line
        B       cfsi_find_image_dims
        B       cfsi_jpeg_scan_file_16
        DCD     3                        ; version number.

R0 RN 0
R1 RN 1
R2 RN 2
R3 RN 3
R4 RN 4
R5 RN 5
R6 RN 6
R7 RN 7
R8 RN 8
R9 RN 9
R10 RN 10
R11 RN 11
R12 RN 12
SP RN 13
LR RN 14
PC RN 15

r0 RN 0
r1 RN 1
r2 RN 2
r3 RN 3
r4 RN 4
r5 RN 5
r6 RN 6
r7 RN 7
r8 RN 8
r9 RN 9
r10 RN 10
r11 RN 11
r12 RN 12
r14 RN 14
sl RN 10
fp RN 11
ip RN 12
sp RN 13
lr RN 14
pc RN 15
        GET     sources.csupport

cfsi_jpeg_scan_file
; Entry conditions: r0 -> space (word-aligned)
;                   r1 = size of space (bytes)
;                   r2 -> file image in store
;                   r3 = file image length
        STMDB   sp!,{r1-r12,lr}
        MOV     sl,sp                 ; used to remember panic exit point
        BL      do_jpeg_scan_file
        LDMIA   sp!,{r1-r12,pc}^
; All registers preserved except r0. r0=error code, 0 for OK or other for some error.
; On exit the workspace has some interesting fields at the front:
;struct decompress_info_struct {
;        int error_code;         /* Returned err code, 0 if no error */
;        int error_argument1;    /* Supplementary error data */
;        int error_argument2;    /* Supplementary error data */
;        int a;                        /* don't touch this */
;        int b;                        /* don't touch this */
;        int /*long*/ image_width;     /* overall image width */
;        int /*long*/ image_height;    /* overall image height */
;        COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
; ... never mind about the rest
; If all is well on exit then error_code will be 0.
; Otherwise it (and r0) is one of:
;#define E_PRE_NOT_8         1            /* Data precision not 8 */
;#define E_RESTART           2            /* Restart interval not 0 */
;#define E_MULTI_SCAN        3            /* Multi-scan file */
;#define E_TOO_HIGH          4            /* Image too high, max is %i pixels */
;#define E_BAD_SAMPLE        5            /* Bad sample factor */
;#define E_COLOUR            8            /* Bad colour space (%i), not grey or YUV */
;#define E_COMPONENTS        9            /* Bad number (%i) of components, only 1 or 3 allowed */
;#define E_TOO_WIDE          10           /* Image too wide, max is %i pixels */
; The colour space will be 1 for greyscale, 3 for YUV.

cfsi_jpeg_scan_file_16
; Entry conditions: r0 -> space (word-aligned)
;                   r1 = size of space (bytes)
;                   r2 -> file image in store
;                   r3 = file image length
        STMDB   sp!,{r1-r12,lr}
        MOV     sl,sp                 ; used to remember panic exit point
        BL      do_jpeg_scan_file_16
        LDMIA   sp!,{r1-r12,pc}^
; Simply call this instead of cfsi_jpeg_scan_file, and subsequent calls to
; cfsi_jpeg_find_line will generate pointers to lines of 16bpp pixels, instead
; of 32bpp ones.

cfsi_jpeg_find_line
; Entry conditions: r0 -> workspace
;                   r1 = required y coordinate (in 0..image_size-1)
        STMDB   sp!,{r1-r12,lr}
        MOV     sl,sp                 ; used to remember panic exit point
        BL      jpeg_find_line
        LDMIA   sp!,{r1-r12,pc}^
; On exit r0 will point to the required scan line, one word per pixel, as VIDC20 RGB pixels.

cfsi_find_image_dims
; Entry conditions: r0 -> JPEG data
;                   r1 -> word in which to put image width
;                   r2 -> word in which to put image height
;                   r3 -> word in which to put image type - 1=mono, 3=YUV
;                   r4 -> word in which to put recommended size of workspace for jpeg_scan_file
; On exit, r0=0 if everything is OK, r0=1 if this data does not look
; like JPEG.
        STMDB   sp!,{r1-r12,lr}
        MOV     sl,sp                 ; used to remember panic exit point
        STMDB   sp!,{r4}              ; fifth arg to C, placed on stack
        BL      find_image_dims
        ADD     sp,sp,#4
        LDMIA   sp!,{r1-r12,pc}^

; Panic exit from C code. we rely on sl being preserved through
; the c code, and use it to fetch back our call registers.
; On entry here r0 has a panic code.
exit
dividebyzero
        MOV     sp,sl
        LDMIA   sp!,{r1-r12,pc}

; Hope the asserts are OK!
newline
assert
        MOVS    pc,lr

; now the actual included stuff
        GBLL    cfsi_jpeg
cfsi_jpeg SETL  {TRUE}

        GET     sources.jdhuff
        GET     sources.jrevdct
        GET     sources.jcconv
XOS_CLI                  * &5     + (1:SHL:17)
        GET     sources.diffuse
        GET     s.rojpeg

        END