<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using RawParserUWP.Model.Format.Base;
using RawParserUWP.Model.Format.Image;
using RawParserUWP.Model.Parser.Nikon;
using RawParserUWP.Model.Format.Reader;

namespace RawParserUWP.Model.Parser
{
    class NEFParser : Parser,IDisposable
    {
        protected BinaryReader fileStream;
        protected IFD ifd, subifd0, subifd1, exif;
        protected NikonMakerNote makerNote;
        protected Header header;

        public override RawImage parse(Stream s)
        {
            RawImage currentRawImage = new RawImage();
            //Set the stream
            setStream(s);

            //read the thumbnail
            currentRawImage.thumbnail = parseThumbnail();

            //read the preview
            currentRawImage.previewImage =parsePreview();

            //read the exif
            currentRawImage.exif = parseExif();
            //read the data
            currentRawImage.imageData = parseRAWImage();
            return currentRawImage;
        }

        public override void setStream(Stream file) 
        {
            //Open a binary stream on the file
            fileStream = new BinaryReader(file);

            //read the first bit to get the endianness of the file           
            if (fileStream.ReadUInt16() == 0x4D4D)
            {
                //File is in reverse bit order
                fileStream.Dispose();
                fileStream = new BinaryReaderBE(file);
            }

            //read the header
            header = new Header(fileStream, 0);

            //Read the IFD
            ifd = new IFD(fileStream, header.TIFFoffset, true, false);
        }

        public override byte[] parseThumbnail()
        {
            //get the Exif<commit_msg>Fix crash for normal byte order image<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using RawParserUWP.Model.Format.Base;
using RawParserUWP.Model.Format.Image;
using RawParserUWP.Model.Parser.Nikon;
using RawParserUWP.Model.Format.Reader;

namespace RawParserUWP.Model.Parser
{
    class NEFParser : Parser,IDisposable
    {
        protected BinaryReader fileStream;
        protected IFD ifd, subifd0, subifd1, exif;
        protected NikonMakerNote makerNote;
        protected Header header;

        public override RawImage parse(Stream s)
        {
            RawImage currentRawImage = new RawImage();
            //Set the stream
            setStream(s);

            //read the thumbnail
            currentRawImage.thumbnail = parseThumbnail();

            //read the preview
            currentRawImage.previewImage =parsePreview();

            //read the exif
            currentRawImage.exif = parseExif();
            //read the data
            currentRawImage.imageData = parseRAWImage();
            return currentRawImage;
        }

        public override void setStream(Stream file) 
        {
            //Open a binary stream on the file
            fileStream = new BinaryReader(file);

            //read the first bit to get the endianness of the file           
            if (fileStream.ReadUInt16() == 0x4D4D)
            {
                //File is in reverse bit order
               // fileStream.Dispose(); //DO NOT dispose, because it remove the filestream not the reader and crash the parse
                fileStream = new BinaryReaderBE(file);
            }

            //read the header
            header = new Header(fileStream, 0);

            //Read the IFD
            ifd = new IFD(fileStream, header.TIFFoffset, true, false);
        }

        public override byte[] parseThumbnail()
        {
            //get the Exif
{'fraction_non_alphanumeric': 0.07477381585949973, 'fraction_numerical': 0.004257583821181479, 'mean_word_length': 1.8434190620272315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3953161', 'n_tokens_mistral': 999, 'n_tokens_neox': 954, 'n_words': 286}