Example #1
0
        protected IEnumerable <MediaImage> ResizeAndCopyImage(IStorageFile imageStream, string savePath, string imageName, string mimeType)
        {
            byte[] imageData = new byte[imageStream.Length];
            if (imageStream.Read(imageData, 0, imageData.Length) != imageData.Length)
            {
                return(null);
            }

            List <MediaImage> listImages = new List <MediaImage>();

            SkiaImageHelper.ResizeImage(imageData, new[]
            {
                new Tuple <int, int>(500, 500), new Tuple <int, int>(300, 300), new Tuple <int, int>(150, 150)
            }.AsEnumerable(),
                                        (s, w, h) =>
            {
                if (_mediaStorage.Store($"{savePath}{imageName}-{w}x{h}.jpeg", s, mimeType))
                {
                    listImages.Add(new MediaImage
                    {
                        Format    = mimeType,
                        ImageSize = (w == 500 ? MediaImageSize.Large : (w == 300 ? MediaImageSize.Medium : MediaImageSize.Small)),
                        Url       = $"{savePath}{imageName}-{w}x{h}.jpeg",
                        FileSize  = s.Length,
                    });
                }
            }, SKEncodedImageFormat.Jpeg, 85);
            return(listImages);
        }
Example #2
0
        private AttachedPicture ReadAttachedPicture(IStorageFile file, int size)
        {
            string mimeType = string.Empty, description = string.Empty;

            byte[] attachedPicture = new byte[size];
            int    numread         = file.Read(attachedPicture, 0, size);

            if (numread != size)
            {
                return(null);
            }

            int  offset       = 0;
            byte encodingType = attachedPicture[0];

            mimeType = ReadAsString(attachedPicture, 1, -1, out offset, encodingType);
            if (string.IsNullOrEmpty(mimeType))
            {
                return(null);
            }

            AttachedPictureType pictureType = (AttachedPictureType)attachedPicture[offset++];

            description = ReadAsString(attachedPicture, offset, -1, out offset, encodingType);

            byte[] pictureData = new byte[size - offset];
            Array.Copy(attachedPicture, offset, pictureData, 0, pictureData.Length);

            return(new AttachedPicture()
            {
                MimeType = mimeType,
                PictureData = pictureData,
                Type = pictureType
            });
        }
Example #3
0
        public bool ReadTags(IStorageFile file, int headerDataSize)
        {
            //TALB - album
            //TCOM - composer
            //TIT2 - song name.
            //TYER - year
            //TCON - genre
            //TRCK - track number
            //TPE1 - lead performer
            //TPE2 - band
            //APIC - Attached picture

            HashSet <string>            setFrameIds = new HashSet <string>(new[] { "TALB", "TCOM", "TIT2", "TYER", "TCON", "TRCK", "TPE1", "TPE2", "APIC" });
            Dictionary <string, string> dicAttributes = new Dictionary <string, string>();
            List <AttachedPicture>      attachedPictures = new List <AttachedPicture>();
            int offset = 0, offsetEnd = 0, numread = 0;

            byte[] tbuf = new byte[10];

            while (offset <= headerDataSize)
            {
                // Read frame id(4 bytes), size(4 bytes) and flags (2 bytes).
                numread = file.Read(tbuf, 0, 10);
                if (numread != 10)
                {
                    return(false);
                }

                var attrName = ReadAsString(tbuf, 0, 4, out offsetEnd);
                if (string.IsNullOrEmpty(attrName))
                {
                    break;
                }

                int size = (int)(BigEndianRead(tbuf, 4) & 0x7FFFFFFF);
                if (size == 0)
                {
                    break;
                }

                offset += (10 + size);

                if (setFrameIds.Contains(attrName))
                {
                    if (attrName == "APIC") // Attached picture.
                    {
                        var pict = ReadAttachedPicture(file, size);
                        if (pict != null)
                        {
                            attachedPictures.Add(pict);
                        }
                        else
                        {
                            break;
                        }
                        // _fileStream.Seek(size, SeekOrigin.Current); // Next attribute.
                    }
                    else
                    {
                        byte[] frameData = new byte[size];
                        numread = file.Read(frameData, 0, size);
                        if (numread != size)
                        {
                            return(false);
                        }
                        dicAttributes[attrName] = ReadAsString(frameData, 1, size - 1, out offsetEnd, frameData[0]);
                    }
                }
                else
                {
                    file.Seek(size, SeekOrigin.Current); // Next attribute.
                }
            }

            _attachedPictures = attachedPictures; // Attached pictures.
            _id3v2Tags        = dicAttributes;    // Set ID3v2 attributes.
            return(true);
        }
Example #4
0
        public int ReadFrame()
        {
            byte[] ssave = new byte[34];
            int    numread = 0, tryme = 0;

            byte[] hbuf     = new byte[8], headbuf = new byte[4];
            ulong  newhead  = 0;
            bool   checkVbr = false;

            if (!head_read(headbuf, ref newhead))
            {
                return(-2);
            }

            if (oldhead != newhead || oldhead == 0)
            {
                fr.header_change = 1;
init_resync:
                if (/*!firsthead && */ !head_check(newhead))
                {
                    if ((newhead & Constants.ID3) == Constants.ID3) // ID3
                    {
                        if (!SkipID3v2Tag(hbuf, ref newhead))
                        {
                            //LastError = peInputError;
                            //throw 1;
                        }
                        checkVbr = true;
                    }
                    int i;
                    {
                        for (i = 0; i < Constants.MAX_RESYNC; i++)
                        {
                            Array.Copy(hbuf, 0, hbuf, 1, 3);

                            numread = _fileStream.Read(hbuf, 3, 1);
                            if (numread == 0)
                            {
                                return(-2);
                            }
                            //if (numread != 1)
                            //    throw 1;
                            newhead <<= 8;
                            newhead  |= hbuf[3];
                            newhead  &= 0xffffffff;
                            if (head_check(newhead))
                            {
                                break;
                            }
                        }
                        if (i == Constants.MAX_RESYNC)
                        {
                            return(0);
                        }
                    }
                }
                if ((newhead & 0xffe00000) != 0xffe00000)
                {
                    if (Args.TryResync)
                    {
                        do
                        {
                            tryme++;
                            Array.Copy(hbuf, 0, hbuf, 1, 7);

                            numread = _fileStream.Read(hbuf, 3, 1);
                            if (numread == 0)
                            {
                                return(-2);
                            }
                            //if (numread != 1)
                            //    throw 1;
                            newhead = ((newhead << 8) | hbuf[3]) & 0xffffffff;
                            if (oldhead == 0)
                            {
                                goto init_resync;
                            }
                        }while ((newhead & Constants.HDRCMPMASK) != (oldhead & Constants.HDRCMPMASK) &&
                                (newhead & Constants.HDRCMPMASK) != (firsthead & Constants.HDRCMPMASK));
                    }
                    else
                    {
                        return(0);
                    }
                }

                if (firsthead == 0)
                {
                    firsthead = newhead;
                }

                if (!decode_header(newhead, checkVbr))
                {
                    //LastError = peInputError;
                    return(0);
                }
                checkVbr = false;
            }
            else
            {
                fr.header_change = 0;
            }

            fsizeold = fsize;
            bsbufold = bsbuf;
            if (bsnum == 1)
            {
                bsbuf = bsspace_2;
                bsnum = 0;
            }
            else
            {
                bsbuf = bsspace_1;
                bsnum = 1;
            }

            fsize   = framesize;
            numread = _fileStream.Read(bsbuf, 516, fsize);
            if (numread == 0)
            {
                return(-2);
            }

            // Copy header bytes.
            Array.Copy(headbuf, 0, bsbuf, 512, 4);
            if (numread != fsize)
            {
                Array.Clear(bsbuf, numread + 516, fsize - numread);
            }

            CurrentFrameFileOffset = _fileStream.Seek(0, SeekOrigin.Current) - framesize - 4 /*frame header*/;
            if (Args.CurrentPos == 0 && Args.FirstFrameOffset == 0)
            {
                Args.FirstFrameOffset = CurrentFrameFileOffset;
            }

            // Update sample rate value after reading the frame.
            if (Args.SampleRate == 0 || Args.IsVBR)
            {
                Args.SampleRate = Constants.freqs[fr.sampling_frequency];
            }

            this.framebuffIsEmpty = false;
            return(1);
        }